home *** CD-ROM | disk | FTP | other *** search
/ 1,001 Nights of Doom / 1001NightsOfDoom1995wickedSensations.iso / nodebild / idbsp10.zip / WADFILE.C < prev    next >
Text File  |  1994-06-03  |  10KB  |  409 lines

  1. #include "idbsp.h"
  2. #include <ctype.h>
  3.  
  4. typedef struct
  5. {
  6.         char            identification[4];              /* should be IWAD */
  7.                 int             numlumps;
  8.                 int             infotableofs;
  9. } wadinfo_t;
  10.  
  11.  
  12. typedef struct
  13. {
  14.                 int             filepos;
  15.         int             size;
  16.         char            name[8];
  17. } lumpinfo_t;
  18.  
  19.  
  20. /*
  21. =============================================================================
  22. */
  23.  
  24. /*
  25. ============
  26. =
  27. = initFromFile:
  28. =
  29. ============
  30. */
  31.  
  32. /* WADFILE *initFromFile(WADFILE *self,char const *path) */
  33. void initFromFile(void)
  34. {
  35.                 wadinfo_t       wad;
  36.                 lumpinfo_t      *lumps;
  37.                 int                     i;
  38. /*
  39.                 self->pathname = malloc(strlen(path)+1);
  40.                 strcpy (self->pathname, path);
  41.                 self->dirty = false;
  42.                 self->handle = open (self->pathname, O_RDWR, 0666);
  43.                 if (self->handle== -1)
  44.                 {
  45. /*              [self free]; */
  46. /*    return nil; */
  47. /*
  48.                                 WadfileFree(self);
  49.                                 return NULL;
  50.                 }
  51. */
  52. /*
  53.  read in the header
  54. */
  55. /*                read (self->handle, &wad, sizeof(wad)); */
  56.                 fread(&wad, sizeof(wad), 1, wad_i.handle);
  57.                 if (strncmp(wad.identification,"PWAD",4))
  58.                 {
  59. /*
  60.                                 close (self->handle);
  61.                                 WadfileFree(self);
  62.                                 return NULL;
  63. */
  64.                 Error("\ninitFromFile: specified WAD is not a PWAD");
  65.                 }
  66. /*
  67.                 wad.numlumps = LONG (wad.numlumps);
  68.                 wad.infotableofs = LONG (wad.infotableofs);
  69. */
  70. /*
  71.  read in the lumpinfo
  72. */
  73. /*                lseek (self->handle, wad.infotableofs, SEEK_SET); */
  74.                 fseek(wad_i.handle, wad.infotableofs, SEEK_SET);
  75. /*
  76.                 info = [[Storage alloc] initCount: wad.numlumps elementSize: sizeof(lumpinfo_t) description: ""];
  77.                 lumps = [info elementAt: 0];
  78. */
  79.                 wad_i.info = (STORAGE *)SafeMalloc(sizeof(STORAGE));
  80.                 wad_i.info->data = (lumpinfo_t *)SafeCalloc(wad.numlumps,sizeof(lumpinfo_t));
  81.                 wad_i.info->count = wad.numlumps;
  82.                 wad_i.info->size = sizeof(lumpinfo_t);
  83.                 lumps = wad_i.info->data;
  84.  
  85. /*                read (self->handle, lumps, wad.numlumps*sizeof(lumpinfo_t)); */
  86.                 fread(lumps, sizeof(lumpinfo_t), wad.numlumps, wad_i.handle);
  87. /*
  88.                 for (i=0 ; i<wad.numlumps ; i++, lumps++)
  89.                 {
  90.                                 lumps->filepos = LONG (lumps->filepos);
  91.                                 lumps->size = LONG (lumps->size);
  92.                 }
  93. */
  94.                 return;
  95. }
  96.  
  97.  
  98. /*
  99. ============
  100. =
  101. = initNew:
  102. =
  103. ============
  104. */
  105.  
  106. void initNew(void)
  107. {
  108.         wadinfo_t       wad;
  109.  
  110. /*
  111.         self->pathname = malloc(strlen(path)+1);
  112.         strcpy (self->pathname, path);
  113. */
  114.  
  115. /*      info = [[Storage alloc] initCount: 0 elementSize: sizeof(lumpinfo_t) description: ""]; */
  116.                 wad_i.info = (STORAGE *)SafeMalloc(sizeof(STORAGE));
  117.         wad_i.info->data = (lumpinfo_t *)SafeMalloc(sizeof(lumpinfo_t));
  118.         wad_i.info->size = sizeof(lumpinfo_t);
  119.         wad_i.info->count = 0;
  120.  
  121.         wad_i.dirty = true;
  122. /*
  123.                 self->handle = open (self->pathname, O_CREAT | O_TRUNC | O_RDWR, 0666);
  124.         if (self->handle== -1)
  125.                 return NULL;
  126. */
  127. /* leave space for wad header */
  128. /*      write (self->handle, &wad, sizeof(wad)); */
  129.         fwrite(&wad, sizeof(wad), 1, wad_i.handle);
  130.  
  131.         return;
  132. }
  133.  
  134. /* WADFILE *WadfileClose(WADFILE *self) */
  135. void WadfileClose(void)
  136. {
  137. /* close (self->handle); */
  138.         fclose(wad_i.handle);
  139.         return;
  140. }
  141.  
  142. /* void WadfileFree(WADFILE *self) */
  143. void WadfileFree(void)
  144. {
  145. /*
  146.         close (handle);
  147.         [info free];
  148.         free (self->pathname);
  149.         return [super free];
  150. */
  151.         fclose(wad_i.handle);
  152.         free(wad_i.info->data);
  153.         free(wad_i.info);
  154.         free(wad_i.pathname);
  155. }
  156.  
  157. /*
  158. =============================================================================
  159. */
  160.  
  161. /* int numLumps(WADFILE *self) */
  162. int numLumps(void)
  163. {
  164. /*      return [info count]; */
  165.         return wad_i.info->count;
  166. }
  167.  
  168. /* int lumpsize(WADFILE *self,int lump) */
  169. int lumpsize(int lump)
  170. {
  171.         lumpinfo_t      *inf;
  172. /*
  173.         inf = [info elementAt: lump];
  174.                 return inf->size;
  175. */
  176.         inf = wad_i.info->data;
  177.         inf += lump;
  178.         return inf->size;
  179. }
  180.  
  181. /* int lumpstart(WADFILE *self,int lump) */
  182. int lumpstart(int lump)
  183. {
  184.         lumpinfo_t      *inf;
  185. /*
  186.         inf = [info elementAt: lump];
  187.         return inf->filepos;
  188. */
  189.         inf = wad_i.info->data;
  190.         inf += lump;
  191.                 return inf->filepos;
  192. }
  193.  
  194. /* char const *lumpname(WADFILE *self,int lump) */
  195. char const *lumpname(int lump)
  196. {
  197.         lumpinfo_t      *inf;
  198. /*
  199.         inf = [info elementAt: lump];
  200.         return inf->name;
  201. */
  202.         inf = wad_i.info->data;
  203.         inf += lump;
  204.         return inf->name;
  205. }
  206.  
  207. /*
  208. ================
  209. =
  210. = lumpNamed:
  211. =
  212. ================
  213. */
  214.  
  215. /*int lumpNamed(WADFILE *self,char const *name) */
  216. int lumpNamed(char const *name)
  217. {
  218.         lumpinfo_t      *inf;
  219.         int                     i, count;
  220.         char                    name8[9];
  221.         int                     v1,v2;
  222.  
  223. /* make the name into two integers for easy compares */
  224.  
  225.                 memset(name8,0,9);
  226.         if (strlen(name) < 9)
  227.                 strncpy (name8,name,9);
  228.         for (i=0 ; i<9 ; i++)
  229.                 name8[i] = toupper(name8[i]);   /* case insensitive */
  230.  
  231.         v1 = *(int *)name8;
  232.         v2 = *(int *)&name8[4];
  233.  
  234.  
  235. /* scan backwards so patch lump files take precedence */
  236.  
  237. /*      count = [info count]; */
  238.         count = wad_i.info->count;
  239.         for (i=count-1 ; i>=0 ; i--)
  240.         {
  241. /*              inf = [info elementAt: i]; */
  242.                                 inf = wad_i.info->data;
  243.                 inf += i;
  244.                 if ( *(int *)inf->name == v1 && *(int *)&inf->name[4] == v2)
  245.                         return i;
  246.         }
  247.         return  -1;
  248. }
  249.  
  250. /*
  251. ================
  252. =
  253. = loadLump:
  254. =
  255. ================
  256. */
  257.  
  258. /* void *loadLump(WADFILE *self,int lump) */
  259. void *loadLump(int lump)
  260. {
  261.         lumpinfo_t      *inf;
  262.         byte                    *buf;
  263. /*
  264.         inf = [info elementAt: lump];
  265.         buf = malloc (inf->size);
  266.  
  267.         lseek (handle, inf->filepos, L_SET);
  268.         read (handle, buf, inf->size);
  269. */
  270.         inf = wad_i.info->data;
  271.         inf += lump;
  272.         buf = (byte *)malloc(inf->size);
  273. /*
  274.         lseek(self->handle, inf->filepos, SEEK_SET);
  275.         read(self->handle, buf, inf->size);
  276. */
  277.         fseek(wad_i.handle, inf->filepos, SEEK_SET);
  278.         fread(buf, inf->size, 1, wad_i.handle);
  279.  
  280.         return buf;
  281. }
  282.  
  283. /* void *loadLumpNamed(WADFILE *self,char const *name)*/
  284. void *loadLumpNamed(char const *name)
  285. {
  286. /*      return [self loadLump:[self lumpNamed: name]]; */
  287.          return loadLump(lumpNamed(name));
  288. }
  289.  
  290. /*
  291. ============================================================================
  292. */
  293.  
  294. /*
  295. ================
  296. =
  297. = addName:data:size:
  298. =
  299. ================
  300. */
  301.  
  302. /* WADFILE *addName(WADFILE *self,char const *name, void *data, int size) */
  303. void addName(char const *name, void *data, int size)
  304. {
  305.         int             i;
  306.         lumpinfo_t      *newlump;
  307.  
  308. /*
  309.         if (wad_i.info->count > 0)
  310.                                 {
  311.                 wad_i.info->count += 1;
  312.                 wad_i.info->data = (lumpinfo_t *)realloc(wad_i.info->data,
  313.                                                                                 (wad_i.info->count + 1) * sizeof(lumpinfo_t));
  314.                 }
  315. */
  316.  
  317.         newlump = (lumpinfo_t *)wad_i.info->data + wad_i.info->count;
  318.  
  319.         wad_i.dirty = true;
  320.         memset (newlump->name,0,sizeof(newlump->name));
  321.         strncpy (newlump->name, name, 8);
  322.         for (i=0 ; i<8 ; i++)
  323.                 newlump->name[i] = toupper(newlump->name[i]);
  324. /*      newlump->filepos = lseek(self->handle,0, L_XTND); */
  325. /*      newlump->filepos = lseek(self->handle,0,SEEK_END); */
  326.         fseek(wad_i.handle, 0, SEEK_END);
  327.                 newlump->filepos = ftell(wad_i.handle);
  328.         newlump->size = size;
  329.  
  330. /*      [info addElement: &new]; */
  331.  
  332. /*      write (self->handle, data, size); */
  333.         fwrite(data, size, 1, wad_i.handle);
  334.  
  335.         wad_i.info->count += 1;
  336.         wad_i.info->data = (lumpinfo_t *)realloc(wad_i.info->data,
  337.                                                                                 (wad_i.info->count + 1) * sizeof(lumpinfo_t));
  338.  
  339.         return;
  340. }
  341.  
  342.  
  343. /*
  344. ================
  345. =
  346. = writeDirectory:
  347. =
  348.         char            identification[4];              // should be IWAD
  349.         int             numlumps;
  350.         int             infotableofs;
  351. ================
  352. */
  353.  
  354. /* WADFILE *writeDirectory(WADFILE *self) */
  355. void writeDirectory(void)
  356. {
  357.         wadinfo_t       wad;
  358.         int                     i,count;
  359.         lumpinfo_t      *inf;
  360.  
  361. /*
  362.  write the directory
  363. */
  364. /*
  365.         count = [info count];
  366.         inf = [info elementAt:0];
  367. */
  368.         count = wad_i.info->count;
  369.         inf = wad_i.info->data;
  370. /*
  371.         for (i=0 ; i<count ; i++)
  372.         {
  373.                 inf[i].filepos = LONG (inf[i].filepos);
  374.                 inf[i].size = LONG (inf[i].size);
  375.         }
  376. */
  377. /*      wad.infotableofs = LONG (lseek(self->handle,0, L_XTND)); */
  378. /*      wad.infotableofs = LONG(lseek(self->handle,0,SEEK_END)); */
  379.         fseek(wad_i.handle,0,SEEK_END);
  380.         wad.infotableofs = ftell(wad_i.handle);
  381.  
  382. /*      write (self->handle, inf, count*sizeof(lumpinfo_t)); */
  383.         fwrite(inf, sizeof(lumpinfo_t), count, wad_i.handle);
  384. /*
  385.         for (i=0 ; i<count ; i++)
  386.         {
  387.                 inf[i].filepos = LONG (inf[i].filepos);
  388.                 inf[i].size = LONG (inf[i].size);
  389.         }
  390. */
  391.  
  392. /*
  393.  write the header
  394. */
  395.                 strncpy (wad.identification, "PWAD",4);
  396. /*      wad.numlumps = LONG ([info count]); */
  397.         wad.numlumps = wad_i.info->count;
  398. /*
  399.         lseek (self->handle, 0, SEEK_SET);
  400.         write (self->handle, &wad, sizeof(wad));
  401. */
  402.         fseek(wad_i.handle, 0, SEEK_SET);
  403.         fwrite(&wad, sizeof(wad), 1, wad_i.handle);
  404.  
  405.         return;
  406. }
  407.  
  408.  
  409.