home *** CD-ROM | disk | FTP | other *** search
/ Quake++ for Quake / Quake++.iso / quake / qeu / repak.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-01  |  3.7 KB  |  106 lines

  1. /*
  2.  * Repak.c - Uses the routines in the QEU library to re-create a
  3.  *           Quake PACK file from a list of separate files.
  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_pack.h"
  14.  
  15. void main(int argc, char *argv[])
  16. {
  17.   char       *filename;
  18.   FILE       *file;
  19.   FILE       *srcfile;
  20.   int         ftype;
  21.   PACKDirPtr  dir;
  22.   UInt16      dirsize;
  23.   char       *indexname = NULL;
  24.   UInt32      size, count;
  25.  
  26.   /* read the parameters... */
  27.   for (argv++, argc--; argc && **argv == '-'; argv++, argc--)
  28.     if ((*argv)[1] == 'h')
  29.       {
  30.     fprintf(stderr, "REPAK %s by Raphael Quinet\n\n", QEU_VERSION);
  31.     fprintf(stderr, "Usage: repak [-h] [-i <indexfile>] file.pak [entryname...]\n");
  32.     fprintf(stderr, "       -h  -- display this help screen\n");
  33.     fprintf(stderr, "       -i  -- get the list of files from the specified index file\n");
  34.     fprintf(stderr, "Repak will create a PACK file containing all the files specified on the\n");
  35.     fprintf(stderr, "command line, or append them to the PACK file if it already exists\n");
  36.     fprintf(stderr, "If the -i option is used, the list of files is read from the specified\n");
  37.     fprintf(stderr, "index file (usually %s, as created by unpak).\n", QEU_INDEX_FILE);
  38.     exit(1);
  39.       }
  40.     else if ((*argv)[1] == 'i' && argc-- > 1)
  41.       indexname = *++argv;
  42.     else
  43.       ProgError("Invalid argument (%s).  Use repak -h for help.", *argv);
  44.   if (argc <= 0)
  45.     ProgError("Missing argument.  Use repak -h for help.");
  46.   filename = *argv;
  47.   if (indexname == NULL && argc < 2)
  48.     ProgError("List of files missing.  Use repak -h for help.");
  49.  
  50.   /* check if the file exists */
  51.   file = OpenFileReadMagic(filename, &ftype);
  52.   if (file == NULL)
  53.     {
  54.       /* create a new PACK file */
  55.       file = fopen(filename, "wb");
  56.       if (file == NULL)
  57.     ProgError("Cannot create file (%s)", filename);
  58.       if (WritePACKHeader(file, &count, &dir, &dirsize) == FALSE)
  59.     ProgError("Cannot write PACK file header");
  60.     }
  61.   else
  62.     {
  63.       /* append to an existing PACK file */
  64.       if (ftype != FTYPE_PACK)
  65.     ProgError("File is not a PACK file (%s)", filename);
  66.       dir = ReadPACKDirectory(file, 0L, &dirsize);
  67.       if (dir == NULL)
  68.     ProgError("Cannot read main directory from %s", filename);
  69.       /* re-open file for writing */
  70.       fclose(file);
  71.       file = fopen(filename, "r+b");
  72.       count = dir[dirsize - 1].offset + dir[dirsize - 1].size;
  73.       if (file == NULL || fseek(file, count, SEEK_SET) < 0)
  74.     ProgError("Cannot re-open file for writing (%s)", filename);
  75.     }
  76.  
  77.   /* parse the index file and read all the files listed in it */
  78.   if (indexname != NULL)
  79.     ProgError("Option -i not implemented yet.  Sorry...\n"); /*! missing */
  80.  
  81.   /* if there are extra arguments on the command line, read these files too */
  82.   for (argv++, argc--; argc; argv++, argc--)
  83.     {
  84.       srcfile = fopen(*argv, "rb");
  85.       if (srcfile == NULL)
  86.     ProgError("File not found (%s)", *argv);
  87.       size = GetFileSize(srcfile);
  88.       printf("Copying %lu bytes from %s\n", size, *argv);
  89.       if (CopyBytes(file, srcfile, size) == FALSE)
  90.     ProgError("Cannot copy data");
  91.       if (AddPACKEntry(file, &count, &dir, &dirsize, *argv, size) == FALSE)
  92.     ProgError("Cannot register entry in PACK directory (%s)", *argv);
  93.       fclose(srcfile);
  94.     }
  95.  
  96.   /* write the directory */
  97.   size = WritePACKDirectory(file, &count, dir, dirsize);
  98.   if (size == 0L)
  99.     ProgError("Cannot write PACK directory");
  100.   printf("Total size of %s: %lu bytes\n", filename, size);
  101.  
  102.   /* close the file and say goodbye */
  103.   fclose(file);
  104.   exit(0);
  105. }
  106.