home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / qeu03 / unwad2.c < prev   
Encoding:
C/C++ Source or Header  |  1996-03-03  |  3.0 KB  |  88 lines

  1. /*
  2.  * Unwad2.c - Uses the routines in the QEU library to extract data
  3.  *            from a Quake WAD2 file.
  4.  *
  5.  * Do whatever you want with this file, but don't blame me if
  6.  * something doesn't work.  If you manage to destroy your hard disk
  7.  * with it, that's too bad for you...  Use it at your own risks!
  8.  */
  9.  
  10. #include "qeu.h"
  11. #include "q_misc.h"
  12. #include "q_files.h"
  13. #include "f_wad2.h"
  14.  
  15. void main(int argc, char *argv[])
  16. {
  17.   char       *filename = NULL;
  18.   FILE       *file;
  19.   int         ftype;
  20.   WAD2DirPtr  dir;
  21.   UInt16      dirsize, n;
  22.   Bool        view = FALSE;
  23.   Bool        convert = FALSE;
  24.   char       *dirname = NULL;
  25.  
  26.   /* read the parameters... */
  27.   for (argv++, argc--; argc && **argv == '-'; argv++, argc--)
  28.     if ((*argv)[1] == 'h')
  29.       {
  30.     fprintf(stderr, "UNWAD2 %s by Raphael Quinet\n\n", QEU_VERSION);
  31.     fprintf(stderr, "Usage: unwad2 [-h] [-v] [-c] [-d <directory>] file.wad [entryname...]\n");
  32.     fprintf(stderr, "       -h  -- display this help screen\n");
  33.     fprintf(stderr, "       -v  -- view the contents of the wad file without extracting any data\n");
  34.     fprintf(stderr, "       -c  -- convert bitmaps to BMP files when extracting\n");
  35.     fprintf(stderr, "       -d  -- use the specified directory for extraction\n");
  36.     fprintf(stderr, "By default, unwad2 will extract all entries from the wad file in a directory\n");
  37.     fprintf(stderr, "which has the same name as the wad file but the extension '.dir'.  If some\n");
  38.     fprintf(stderr, "names are given after the file name, only these entries will be extracted.\n");
  39.     exit(1);
  40.       }
  41.     else if ((*argv)[1] == 'v')
  42.       view = TRUE;
  43.     else if ((*argv)[1] == 'c')
  44.       convert = TRUE;
  45.     else if ((*argv)[1] == 'd' && argc-- > 1)
  46.       dirname = *++argv;
  47.     else
  48.       ProgError("Invalid argument (%s).  Use unwad2 -h for help.", *argv);
  49.   if (argc > 0)
  50.     filename = *argv;
  51.   else
  52.     ProgError("Missing argument.  Use unwad2 -h for help.");
  53.   if (dirname == NULL)
  54.     dirname = ChangeFileExtension(filename, NULL, "dir");
  55.  
  56.   /* open the WAD2 file... */
  57.   file = OpenFileReadMagic(filename, &ftype);
  58.   if (file == NULL)
  59.     ProgError("File not found (%s)", filename);
  60.   if (ftype != FTYPE_WAD2)
  61.     ProgError("File is not a WAD2 file (%s)", filename);
  62.  
  63.   /* do something... */
  64.   dir = ReadWAD2Directory(file, 0, &dirsize);
  65.   if (dir == NULL)
  66.     ProgError("Cannot read main directory from %s", filename);
  67.   if (view == TRUE)
  68.     DumpWAD2Directory(stdout, dir, dirsize);
  69.   else if (argc > 1)
  70.     for (argv++, argc--; argc; argv++, argc--)
  71.       {
  72.     n = FindWAD2Entry(dir, dirsize, *argv);
  73.     if (n >= dirsize)
  74.       ProgError("Could not find entry %s in directory", *argv);
  75.     if (UnWAD2File(stdout, file, 0, dir, dirsize, n, dirname,
  76.                convert) == FALSE)
  77.       ProgError("Could not unpack entry %s from %s", *argv, filename);
  78.       }
  79.   else
  80.     if (UnWAD2File(stdout, file, 0, dir, dirsize, dirsize, dirname,
  81.            convert) == FALSE)
  82.       ProgError("Could not unpack all entries from %s", filename);
  83.  
  84.   /* close the file and say goodbye */
  85.   fclose(file);
  86.   exit(0);
  87. }
  88.