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