diff options
Diffstat (limited to 'src/heretic/w_wad.c')
-rw-r--r-- | src/heretic/w_wad.c | 353 |
1 files changed, 188 insertions, 165 deletions
diff --git a/src/heretic/w_wad.c b/src/heretic/w_wad.c index 2be53793..b252735c 100644 --- a/src/heretic/w_wad.c +++ b/src/heretic/w_wad.c @@ -1,3 +1,25 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 1993-2008 Raven Software +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +//----------------------------------------------------------------------------- // W_wad.c #ifdef NeXT @@ -24,17 +46,17 @@ typedef struct { - char identification[4]; // should be IWAD - int numlumps; - int infotableofs; + char identification[4]; // should be IWAD + int numlumps; + int infotableofs; } wadinfo_t; typedef struct { - int filepos; - int size; - char name[8]; + int filepos; + int size; + char name[8]; } filelump_t; @@ -42,10 +64,10 @@ typedef struct // GLOBALS //============= -lumpinfo_t *lumpinfo; // location of each lump on disk -int numlumps; +lumpinfo_t *lumpinfo; // location of each lump on disk +int numlumps; -void **lumpcache; +void **lumpcache; //=================== @@ -54,10 +76,10 @@ void **lumpcache; #define strcmpi strcasecmp -void strupr (char *s) +void strupr(char *s) { while (*s) - *s++ = toupper(*s); + *s++ = toupper(*s); } /* @@ -68,12 +90,12 @@ void strupr (char *s) ================ */ -int filelength (int handle)
-{
- struct stat fileinfo; - - if (fstat (handle,&fileinfo) == -1) - I_Error ("Error fstating"); +int filelength(int handle) +{ + struct stat fileinfo; + + if (fstat(handle, &fileinfo) == -1) + I_Error("Error fstating"); return fileinfo.st_size; } @@ -81,30 +103,30 @@ int filelength (int handle) #endif -void ExtractFileBase (char *path, char *dest) +void ExtractFileBase(char *path, char *dest) { - char *src; - int length; + char *src; + int length; - src = path + strlen(path) - 1; + src = path + strlen(path) - 1; // // back up until a \ or the start // - while (src != path && *(src-1) != '\\' && *(src-1) != '/') - src--; + while (src != path && *(src - 1) != '\\' && *(src - 1) != '/') + src--; // // copy up to eight characters // - memset (dest,0,8); - length = 0; - while (*src && *src != '.') - { - if (++length == 9) - I_Error ("Filename base of %s >8 chars",path); - *dest++ = toupper((int)*src++); - } + memset(dest, 0, 8); + length = 0; + while (*src && *src != '.') + { + if (++length == 9) + I_Error("Filename base of %s >8 chars", path); + *dest++ = toupper((int) *src++); + } } /* @@ -127,66 +149,66 @@ void ExtractFileBase (char *path, char *dest) ==================== */ -void W_AddFile (char *filename) +void W_AddFile(char *filename) { - wadinfo_t header; - lumpinfo_t *lump_p; - unsigned i; - int handle, length; - int startlump; - filelump_t *fileinfo, singleinfo; - + wadinfo_t header; + lumpinfo_t *lump_p; + unsigned i; + int handle, length; + int startlump; + filelump_t *fileinfo, singleinfo; + // // open the file and add to directory -// - if ( (handle = open (filename,O_RDONLY | O_BINARY)) == -1) - return; - - startlump = numlumps; - - if (strcmpi (filename+strlen(filename)-3 , "wad" ) ) - { - // single lump file - fileinfo = &singleinfo; - singleinfo.filepos = 0; - singleinfo.size = LONG(filelength(handle)); - ExtractFileBase (filename, singleinfo.name); - numlumps++; - } - else - { - // WAD file - read (handle, &header, sizeof(header)); - if (strncmp(header.identification,"IWAD",4)) - { - if (strncmp(header.identification,"PWAD",4)) - I_Error ("Wad file %s doesn't have IWAD or PWAD id\n" - ,filename); - } - header.numlumps = LONG(header.numlumps); - header.infotableofs = LONG(header.infotableofs); - length = header.numlumps*sizeof(filelump_t); - fileinfo = alloca (length); - lseek (handle, header.infotableofs, SEEK_SET); - read (handle, fileinfo, length); - numlumps += header.numlumps; - } +// + if ((handle = open(filename, O_RDONLY | O_BINARY)) == -1) + return; + + startlump = numlumps; + + if (strcmpi(filename + strlen(filename) - 3, "wad")) + { + // single lump file + fileinfo = &singleinfo; + singleinfo.filepos = 0; + singleinfo.size = LONG(filelength(handle)); + ExtractFileBase(filename, singleinfo.name); + numlumps++; + } + else + { + // WAD file + read(handle, &header, sizeof(header)); + if (strncmp(header.identification, "IWAD", 4)) + { + if (strncmp(header.identification, "PWAD", 4)) + I_Error("Wad file %s doesn't have IWAD or PWAD id\n", + filename); + } + header.numlumps = LONG(header.numlumps); + header.infotableofs = LONG(header.infotableofs); + length = header.numlumps * sizeof(filelump_t); + fileinfo = alloca(length); + lseek(handle, header.infotableofs, SEEK_SET); + read(handle, fileinfo, length); + numlumps += header.numlumps; + } // // Fill in lumpinfo // - lumpinfo = realloc (lumpinfo, numlumps*sizeof(lumpinfo_t)); - if (!lumpinfo) - I_Error ("Couldn't realloc lumpinfo"); - lump_p = &lumpinfo[startlump]; - - for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++) - { - lump_p->handle = handle; - lump_p->position = LONG(fileinfo->filepos); - lump_p->size = LONG(fileinfo->size); - strncpy (lump_p->name, fileinfo->name, 8); - } + lumpinfo = realloc(lumpinfo, numlumps * sizeof(lumpinfo_t)); + if (!lumpinfo) + I_Error("Couldn't realloc lumpinfo"); + lump_p = &lumpinfo[startlump]; + + for (i = startlump; i < numlumps; i++, lump_p++, fileinfo++) + { + lump_p->handle = handle; + lump_p->position = LONG(fileinfo->filepos); + lump_p->size = LONG(fileinfo->size); + strncpy(lump_p->name, fileinfo->name, 8); + } } @@ -212,30 +234,30 @@ void W_AddFile (char *filename) ==================== */ -void W_InitMultipleFiles (char **filenames) -{ - int size; - +void W_InitMultipleFiles(char **filenames) +{ + int size; + // // open all the files, load headers, and count lumps // - numlumps = 0; - lumpinfo = malloc(1); // will be realloced as lumps are added + numlumps = 0; + lumpinfo = malloc(1); // will be realloced as lumps are added + + for (; *filenames; filenames++) + W_AddFile(*filenames); - for ( ; *filenames ; filenames++) - W_AddFile (*filenames); + if (!numlumps) + I_Error("W_InitFiles: no files found"); - if (!numlumps) - I_Error ("W_InitFiles: no files found"); - // // set up caching // - size = numlumps * sizeof(*lumpcache); - lumpcache = malloc (size); - if (!lumpcache) - I_Error ("Couldn't allocate lumpcache"); - memset (lumpcache,0, size); + size = numlumps * sizeof(*lumpcache); + lumpcache = malloc(size); + if (!lumpcache) + I_Error("Couldn't allocate lumpcache"); + memset(lumpcache, 0, size); } @@ -250,13 +272,13 @@ void W_InitMultipleFiles (char **filenames) ==================== */ -void W_InitFile (char *filename) +void W_InitFile(char *filename) { - char *names[2]; + char *names[2]; - names[0] = filename; - names[1] = NULL; - W_InitMultipleFiles (names); + names[0] = filename; + names[1] = NULL; + W_InitMultipleFiles(names); } @@ -269,9 +291,9 @@ void W_InitFile (char *filename) ==================== */ -int W_NumLumps (void) +int W_NumLumps(void) { - return numlumps; + return numlumps; } @@ -286,32 +308,32 @@ int W_NumLumps (void) ==================== */ -int W_CheckNumForName (char *name) +int W_CheckNumForName(char *name) { - char name8[9]; - int v1,v2; - lumpinfo_t *lump_p; + char name8[9]; + int v1, v2; + lumpinfo_t *lump_p; // make the name into two integers for easy compares - strncpy (name8,name,8); - name8[8] = 0; // in case the name was a fill 8 chars - strupr (name8); // case insensitive + strncpy(name8, name, 8); + name8[8] = 0; // in case the name was a fill 8 chars + strupr(name8); // case insensitive - v1 = *(int *)name8; - v2 = *(int *)&name8[4]; + v1 = *(int *) name8; + v2 = *(int *) &name8[4]; // scan backwards so patch lump files take precedence - lump_p = lumpinfo + numlumps; + lump_p = lumpinfo + numlumps; - while (lump_p-- != lumpinfo) - if ( *(int *)lump_p->name == v1 && *(int *)&lump_p->name[4] == v2) - return lump_p - lumpinfo; + while (lump_p-- != lumpinfo) + if (*(int *) lump_p->name == v1 && *(int *) &lump_p->name[4] == v2) + return lump_p - lumpinfo; - return -1; + return -1; } @@ -325,16 +347,16 @@ int W_CheckNumForName (char *name) ==================== */ -int W_GetNumForName (char *name) +int W_GetNumForName(char *name) { - int i; + int i; - i = W_CheckNumForName (name); - if (i != -1) - return i; + i = W_CheckNumForName(name); + if (i != -1) + return i; - I_Error ("W_GetNumForName: %s not found!",name); - return -1; + I_Error("W_GetNumForName: %s not found!", name); + return -1; } @@ -348,11 +370,11 @@ int W_GetNumForName (char *name) ==================== */ -int W_LumpLength (int lump) +int W_LumpLength(int lump) { - if (lump >= numlumps) - I_Error ("W_LumpLength: %i >= numlumps",lump); - return lumpinfo[lump].size; + if (lump >= numlumps) + I_Error("W_LumpLength: %i >= numlumps", lump); + return lumpinfo[lump].size; } @@ -366,22 +388,23 @@ int W_LumpLength (int lump) ==================== */ -void W_ReadLump (int lump, void *dest) +void W_ReadLump(int lump, void *dest) { - int c; - lumpinfo_t *l; - - if (lump >= numlumps) - I_Error ("W_ReadLump: %i >= numlumps",lump); - l = lumpinfo+lump; - - I_BeginRead (); - - lseek (l->handle, l->position, SEEK_SET); - c = read (l->handle, dest, l->size); - if (c < l->size) - I_Error ("W_ReadLump: only read %i of %i on lump %i",c,l->size,lump); - I_EndRead (); + int c; + lumpinfo_t *l; + + if (lump >= numlumps) + I_Error("W_ReadLump: %i >= numlumps", lump); + l = lumpinfo + lump; + + I_BeginRead(); + + lseek(l->handle, l->position, SEEK_SET); + c = read(l->handle, dest, l->size); + if (c < l->size) + I_Error("W_ReadLump: only read %i of %i on lump %i", c, l->size, + lump); + I_EndRead(); } @@ -394,26 +417,26 @@ void W_ReadLump (int lump, void *dest) ==================== */ -void *W_CacheLumpNum (int lump, int tag) +void *W_CacheLumpNum(int lump, int tag) { -byte *ptr; + byte *ptr; - if ((unsigned)lump >= numlumps) - I_Error ("W_CacheLumpNum: %i >= numlumps",lump); - - if (!lumpcache[lump]) - { // read the lump in + if ((unsigned) lump >= numlumps) + I_Error("W_CacheLumpNum: %i >= numlumps", lump); + + if (!lumpcache[lump]) + { // read the lump in //printf ("cache miss on lump %i\n",lump); - ptr = Z_Malloc (W_LumpLength (lump), tag, &lumpcache[lump]); - W_ReadLump (lump, lumpcache[lump]); - } - else - { + ptr = Z_Malloc(W_LumpLength(lump), tag, &lumpcache[lump]); + W_ReadLump(lump, lumpcache[lump]); + } + else + { //printf ("cache hit on lump %i\n",lump); - Z_ChangeTag (lumpcache[lump],tag); - } - - return lumpcache[lump]; + Z_ChangeTag(lumpcache[lump], tag); + } + + return lumpcache[lump]; } @@ -425,9 +448,9 @@ byte *ptr; ==================== */ -void *W_CacheLumpName (char *name, int tag) +void *W_CacheLumpName(char *name, int tag) { - return W_CacheLumpNum (W_GetNumForName(name), tag); + return W_CacheLumpNum(W_GetNumForName(name), tag); } |