8 wadFile_t *wadCleanup(wadFile_t *wf)
\r
12 if (wf->fin) fclose(wf->fin);
\r
13 if (wf->lpHeader) free(wf->lpHeader);
\r
14 if (wf->lpLump) free(wf->lpLump);
\r
15 if (wf->lpMip) free(wf->lpMip);
\r
16 if (wf->wadfilename) free(wf->wadfilename);
\r
23 int wadGetCurrentFileInfo ( wadFile_t *wf, char *szFileName, unsigned long fileNameBufferSize, unsigned long *filesize)
\r
25 /* returns 0 if error, or 1 for sucess */
\r
26 // if this fails you'll need to re-position the fileposition
\r
27 // before attempting any other calls. e.g. call wadGoToFirstFile()
\r
29 if (fread(wf->lpLump,sizeof(WAD3_LUMP),1,wf->fin)!=1)
\r
31 strncpy(szFileName, wf->lpLump->name, fileNameBufferSize);
\r
32 szFileName[fileNameBufferSize-1] = 0; // null terminate
\r
34 *filesize = wf->lpLump->size;
\r
39 int wadGoToFile(wadFile_t *wf, unsigned long filenum)
\r
47 if (filenum >= wf->lpHeader->numlumps)
\r
50 if (fseek(wf->fin,wf->lpHeader->infotableofs + (filenum * sizeof(WAD3_LUMP)),SEEK_SET) != 0)
\r
53 wf->currentfile = filenum;
\r
58 int wadGoToNextFile(wadFile_t *wf)
\r
60 return(wadGoToFile(wf, wf->currentfile + 1));
\r
63 int wadGoToFirstFile(wadFile_t *wf)
\r
65 /* returns 0 if error, or 1 for sucess */
\r
73 if (fseek(wf->fin,wf->lpHeader->infotableofs,SEEK_SET) != 0)
\r
76 wf->currentfile = 0;
\r
81 wadFile_t *wadOpen(const char* path)
\r
84 wadFile_t *wf = NULL;
\r
90 memset (wf, 0, sizeof(*wf));
\r
95 wf->fin=fopen(path,"rb");
\r
97 return wadCleanup(wf);
\r
99 // get the file size
\r
100 if (fseek(wf->fin,0,SEEK_END) != 0)
\r
101 return wadCleanup(wf);
\r
103 wf->FileSize = ftell( wf->fin );
\r
105 // Make sure it's at least big enough to manipulate the header
\r
106 if (wf->FileSize < sizeof(WAD3_HEADER))
\r
108 // WAD3 file is malformed.
\r
109 return wadCleanup(wf);
\r
112 // go back to the start
\r
113 if (fseek(wf->fin,0,SEEK_SET)!=0)
\r
114 return wadCleanup(wf);
\r
116 // allocate buffers
\r
117 wf->lpHeader = (LPWAD3_HEADER) malloc(sizeof(WAD3_HEADER));
\r
118 wf->lpLump = (LPWAD3_LUMP) malloc(sizeof(WAD3_LUMP));
\r
119 wf->lpMip = (LPWAD3_MIP) malloc(sizeof(WAD3_MIP));
\r
121 if (!(wf->lpHeader) || !(wf->lpLump) || !(wf->lpMip))
\r
122 return wadCleanup(wf);
\r
124 // read the header.
\r
125 if (fread(wf->lpHeader,sizeof(WAD3_HEADER),1,wf->fin)!=1)
\r
126 return wadCleanup(wf);
\r
128 if (wf->lpHeader->identification != WAD2_ID && wf->lpHeader->identification != WAD3_ID)
\r
130 // Invalid WAD3 header id.
\r
131 return wadCleanup(wf);
\r
134 // Make sure our table is really there
\r
135 if ( ((wf->lpHeader->numlumps * sizeof(WAD3_LUMP)) + wf->lpHeader->infotableofs) > wf->FileSize)
\r
137 // WAD3 file is malformed.
\r
138 return wadCleanup(wf);
\r
141 // Store the name of the wadfile
\r
142 if (!(wf->wadfilename = strdup(path)))
\r
143 return wadCleanup(wf);
\r
148 int wadOpenCurrentFileByNum (wadFile_t *wf, unsigned long filenumber)
\r
150 /* returns 0 if error, or 1 for sucess */
\r
151 return(wadGoToFile(wf, filenumber));
\r
154 void wadCloseCurrentFile (wadFile_t *wf)
\r
156 // nothing to do really...
\r
159 unsigned long wadReadCurrentFile (wadFile_t *wf , char *bufferptr, unsigned long size)
\r
161 // returns 0 if error, or the amount of data read into the buffer
\r
162 if (fread(wf->lpLump,sizeof(WAD3_LUMP),1,wf->fin)!=1)
\r
165 // dunno how to handle any other image types but this (yet)
\r
166 if (wf->lpLump->type != WAD2_TYPE_MIP && wf->lpLump->type != WAD3_TYPE_MIP)
\r
170 if (fseek(wf->fin, wf->lpLump->filepos, SEEK_SET) != 0)
\r
173 if (fread(bufferptr,size,1,wf->fin) == 1)
\r
181 .. or we could do it the long way, and process the file as we go..
\r
186 unsigned long wadReadCurrentFile (wadFile_t *wf , char *bufferptr, unsigned long size)
\r
188 // returns 0 if error, or the amount of data read into the buffer
\r
189 unsigned long bufferpos;
\r
190 unsigned long mipdatasize;
\r
193 if (fread(wf->lpLump,sizeof(WAD3_LUMP),1,wf->fin)!=1)
\r
196 if (wf->lpLump->type == WAD3_TYPE_MIP) // can we handle it ?
\r
200 if (wf->lpLump->filepos >= wf->FileSize)
\r
201 return 0; // malformed wad3
\r
204 if (fseek(wf->fin, wf->lpLump->filepos, SEEK_SET) != 0)
\r
208 if (fread(wf->lpMip,sizeof(WAD3_MIP),1,wf->fin)!=1)
\r
211 // store in buffer.
\r
212 memcpy(bufferptr, wf->lpMip, sizeof(WAD3_MIP));
\r
213 bufferpos = sizeof(WAD3_MIP);
\r
215 // now read the MIP data.
\r
217 if (fseek(wf->fin, wf->lpLump->filepos + wf->lpMip->offsets[0], SEEK_SET) != 0)
\r
220 mipdatasize = GET_MIP_DATA_SIZE(wf->lpMip->width,wf->lpMip->height);
\r
222 if (fread(bufferptr+bufferpos, mipdatasize, 1, wf->fin)!=1)
\r
225 bufferpos += mipdatasize;
\r
227 // ok, that's the mip data itself, now grab the palette size.
\r
228 if (fread(bufferptr+bufferpos,sizeof(WORD),1,wf->fin)!=1)
\r
231 palettesize = *(WORD *)(bufferptr+bufferpos);
\r
233 bufferpos += sizeof(WORD);
\r
235 // grab the palette itself
\r
236 if (fread(bufferptr+bufferpos,palettesize*3,1,wf->fin)!=1)
\r
239 bufferpos += palettesize*3;
\r
241 // and finally the one-word padding.
\r
242 if (fread(bufferptr+bufferpos,sizeof(WORD),1,wf->fin)!=1)
\r
245 bufferpos += sizeof(WORD);
\r
247 return(bufferpos); // return the amount of bytes read.
\r