home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / sharewar / quake106 / utils / common / wadlib.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  5KB  |  329 lines

  1. // wad2lib.c
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <ctype.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. //#include <sys/file.h>
  11. #include <stdarg.h>
  12.  
  13. #ifdef NeXT
  14. #include <libc.h>
  15. #endif
  16. #include "cmdlib.h"
  17. #include "wadlib.h"
  18.  
  19. /*
  20. ============================================================================
  21.  
  22.                             WAD READING
  23.  
  24. ============================================================================
  25. */
  26.  
  27.  
  28. lumpinfo_t        *lumpinfo;        // location of each lump on disk
  29. int                numlumps;
  30.  
  31. wadinfo_t        header;
  32. FILE            *wadhandle;
  33.  
  34.  
  35. /*
  36. ====================
  37. W_OpenWad
  38. ====================
  39. */
  40. void W_OpenWad (char *filename)
  41. {
  42.     lumpinfo_t        *lump_p;
  43.     unsigned        i;
  44.     int                length;
  45.     
  46. //
  47. // open the file and add to directory
  48. //    
  49.     wadhandle = SafeOpenRead (filename);
  50.     SafeRead (wadhandle, &header, sizeof(header));
  51.  
  52.     if (strncmp(header.identification,"WAD2",4))
  53.         Error ("Wad file %s doesn't have WAD2 id\n",filename);
  54.         
  55.     header.numlumps = LittleLong(header.numlumps);
  56.     header.infotableofs = LittleLong(header.infotableofs);
  57.  
  58.     numlumps = header.numlumps;
  59.  
  60.     length = numlumps*sizeof(lumpinfo_t);
  61.     lumpinfo = malloc (length);
  62.     lump_p = lumpinfo;
  63.     
  64.     fseek (wadhandle, header.infotableofs, SEEK_SET);
  65.     SafeRead (wadhandle, lumpinfo, length);
  66.  
  67. //
  68. // Fill in lumpinfo
  69. //
  70.     
  71.     for (i=0 ; i<numlumps ; i++,lump_p++)
  72.     {
  73.         lump_p->filepos = LittleLong(lump_p->filepos);
  74.         lump_p->size = LittleLong(lump_p->size);
  75.     }
  76. }
  77.  
  78.  
  79.  
  80. void CleanupName (char *in, char *out)
  81. {
  82.     int        i;
  83.     
  84.     for (i=0 ; i<sizeof( ((lumpinfo_t *)0)->name ) ; i++ )
  85.     {
  86.         if (!in[i])
  87.             break;
  88.             
  89.         out[i] = toupper(in[i]);
  90.     }
  91.     
  92.     for ( ; i<sizeof( ((lumpinfo_t *)0)->name ); i++ )
  93.         out[i] = 0;
  94. }
  95.  
  96.  
  97. /*
  98. ====================
  99. W_CheckNumForName
  100.  
  101. Returns -1 if name not found
  102. ====================
  103. */
  104. int    W_CheckNumForName (char *name)
  105. {
  106.     char    cleanname[16];
  107.     int        v1,v2, v3, v4;
  108.     int        i;
  109.     lumpinfo_t    *lump_p;
  110.     
  111.     CleanupName (name, cleanname);
  112.     
  113. // make the name into four integers for easy compares
  114.  
  115.     v1 = *(int *)cleanname;
  116.     v2 = *(int *)&cleanname[4];
  117.     v3 = *(int *)&cleanname[8];
  118.     v4 = *(int *)&cleanname[12];
  119.  
  120. // find it
  121.  
  122.     lump_p = lumpinfo;
  123.     for (i=0 ; i<numlumps ; i++, lump_p++)
  124.     {
  125.         if ( *(int *)lump_p->name == v1
  126.         && *(int *)&lump_p->name[4] == v2
  127.         && *(int *)&lump_p->name[8] == v3
  128.         && *(int *)&lump_p->name[12] == v4)
  129.             return i;
  130.     }
  131.  
  132.     return -1;
  133. }
  134.  
  135.  
  136. /*
  137. ====================
  138. W_GetNumForName
  139.  
  140. Calls W_CheckNumForName, but bombs out if not found
  141. ====================
  142. */
  143. int    W_GetNumForName (char *name)
  144. {
  145.     int    i;
  146.  
  147.     i = W_CheckNumForName (name);
  148.     if (i != -1)
  149.         return i;
  150.  
  151.     Error ("W_GetNumForName: %s not found!",name);
  152.     return -1;
  153. }
  154.  
  155.  
  156. /*
  157. ====================
  158. W_LumpLength
  159.  
  160. Returns the buffer size needed to load the given lump
  161. ====================
  162. */
  163. int W_LumpLength (int lump)
  164. {
  165.     if (lump >= numlumps)
  166.         Error ("W_LumpLength: %i >= numlumps",lump);
  167.     return lumpinfo[lump].size;
  168. }
  169.  
  170.  
  171. /*
  172. ====================
  173. W_ReadLumpNum
  174.  
  175. Loads the lump into the given buffer, which must be >= W_LumpLength()
  176. ====================
  177. */
  178. void W_ReadLumpNum (int lump, void *dest)
  179. {
  180.     lumpinfo_t    *l;
  181.     
  182.     if (lump >= numlumps)
  183.         Error ("W_ReadLump: %i >= numlumps",lump);
  184.     l = lumpinfo+lump;
  185.     
  186.     fseek (wadhandle, l->filepos, SEEK_SET);
  187.     SafeRead (wadhandle, dest, l->size);
  188. }
  189.  
  190.  
  191.  
  192. /*
  193. ====================
  194. W_LoadLumpNum
  195. ====================
  196. */
  197. void    *W_LoadLumpNum (int lump)
  198. {
  199.     void    *buf;
  200.     
  201.     if ((unsigned)lump >= numlumps)
  202.         Error ("W_CacheLumpNum: %i >= numlumps",lump);
  203.         
  204.     buf = malloc (W_LumpLength (lump));
  205.     W_ReadLumpNum (lump, buf);
  206.     
  207.     return buf;
  208. }
  209.  
  210.  
  211. /*
  212. ====================
  213. W_LoadLumpName
  214. ====================
  215. */
  216. void    *W_LoadLumpName (char *name)
  217. {
  218.     return W_LoadLumpNum (W_GetNumForName(name));
  219. }
  220.  
  221.  
  222. /*
  223. ===============================================================================
  224.  
  225.                         WAD CREATION
  226.  
  227. ===============================================================================
  228. */
  229.  
  230. FILE        *outwad;
  231.  
  232. lumpinfo_t    outinfo[4096];
  233. int            outlumps;
  234.  
  235. short    (*wadshort) (short l);
  236. int        (*wadlong) (int l);
  237.  
  238. /*
  239. ===============
  240. NewWad
  241. ===============
  242. */
  243.  
  244. void NewWad (char *pathname, qboolean bigendien)
  245. {
  246.     outwad = SafeOpenWrite (pathname);
  247.     fseek (outwad, sizeof(wadinfo_t), SEEK_SET);
  248.     memset (outinfo, 0, sizeof(outinfo));
  249.     
  250.     if (bigendien)
  251.     {
  252.         wadshort = BigShort;
  253.         wadlong = BigLong;
  254.     }
  255.     else
  256.     {
  257.         wadshort = LittleShort;
  258.         wadlong = LittleLong;
  259.     }
  260.     
  261.     outlumps = 0;
  262. }
  263.  
  264.  
  265. /*
  266. ===============
  267. AddLump
  268. ===============
  269. */
  270.  
  271. void    AddLump (char *name, void *buffer, int length, int type, int compress)
  272. {
  273.     lumpinfo_t    *info;
  274.     int            ofs;
  275.     
  276.     info = &outinfo[outlumps];
  277.     outlumps++;
  278.  
  279.     memset (info,0,sizeof(info));
  280.     
  281.     strcpy (info->name, name);
  282.     strupr (info->name);
  283.     
  284.     ofs = ftell(outwad);
  285.     info->filepos = wadlong(ofs);
  286.     info->size = info->disksize = wadlong(length);
  287.     info->type = type;
  288.     info->compression = compress;
  289.     
  290. // FIXME: do compression
  291.  
  292.     SafeWrite (outwad, buffer, length);
  293. }
  294.  
  295.  
  296. /*
  297. ===============
  298. WriteWad
  299. ===============
  300. */
  301.  
  302. void WriteWad (void)
  303. {
  304.     wadinfo_t    header;
  305.     int            ofs;
  306.     
  307. // write the lumpingo
  308.     ofs = ftell(outwad);
  309.  
  310.     SafeWrite (outwad, outinfo, outlumps*sizeof(lumpinfo_t) );
  311.         
  312. // write the header
  313.  
  314. // a program will be able to tell the ednieness of a wad by the id
  315.     header.identification[0] = 'W';
  316.     header.identification[1] = 'A';
  317.     header.identification[2] = 'D';
  318.     header.identification[3] = '2';
  319.     
  320.     header.numlumps = wadlong(outlumps);
  321.     header.infotableofs = wadlong(ofs);
  322.         
  323.     fseek (outwad, 0, SEEK_SET);
  324.     SafeWrite (outwad, &header, sizeof(header));
  325.     fclose (outwad);
  326. }
  327.  
  328.  
  329.