home *** CD-ROM | disk | FTP | other *** search
- /*
- * Rewad.c - Uses the routines in the QEU library to re-create a
- * Doom/Heretic/Hexen WAD file from a list of separate files.
- *
- * Do whatever you want with this file, but don't blame me if
- * something doesn't work. If you manage to destroy your hard disk
- * with it, that's too bad for you... Use it at your own risks!
- */
-
- #include "qeu.h"
- #include "q_misc.h"
- #include "q_files.h"
- #include "f_wad.h"
-
- void main(int argc, char *argv[])
- {
- char *filename;
- FILE *file;
- FILE *srcfile;
- int ftype;
- WADDirPtr dir;
- UInt16 dirsize;
- char *indexname = NULL;
- UInt32 size, count;
- char *entryname;
-
- /* read the parameters... */
- for (argv++, argc--; argc && **argv == '-'; argv++, argc--)
- if ((*argv)[1] == 'h')
- {
- fprintf(stderr, "REWAD %s by Raphael Quinet\n\n", QEU_VERSION);
- fprintf(stderr, "Usage: rewad [-h] [-i <indexfile>] file.wad [entryname...]\n");
- fprintf(stderr, " -h -- display this help screen\n");
- fprintf(stderr, " -i -- get the list of files from the specified index file\n");
- fprintf(stderr, "Rewad will create a WAD file containing all the files specified on the\n");
- fprintf(stderr, "command line, or append them to the WAD file if it already exists\n");
- fprintf(stderr, "If the -i option is used, the list of files is read from the specified\n");
- fprintf(stderr, "index file (usually %s, as created by unwad).\n", QEU_INDEX_FILE);
- exit(1);
- }
- else if ((*argv)[1] == 'i' && argc-- > 1)
- indexname = *++argv;
- else
- ProgError("Invalid argument (%s). Use rewad -h for help.", *argv);
- if (argc <= 0)
- ProgError("Missing argument. Use rewad -h for help.");
- filename = *argv;
- if (indexname == NULL && argc < 2)
- ProgError("List of files missing. Use rewad -h for help.");
-
- /* check if the file exists */
- file = OpenFileReadMagic(filename, &ftype);
- if (file == NULL)
- {
- /* create a new PWAD file */
- file = fopen(filename, "wb");
- if (file == NULL)
- ProgError("Cannot create file (%s)", filename);
- if (WriteWADHeader(file, &count, &dir, &dirsize, TRUE) == FALSE)
- ProgError("Cannot write WAD file header");
- }
- else
- {
- /* append to an existing WAD file */
- if (ftype != FTYPE_PWAD && ftype != FTYPE_IWAD)
- ProgError("File is not a WAD file (%s)", filename);
- dir = ReadWADDirectory(file, 0L, &dirsize);
- if (dir == NULL)
- ProgError("Cannot read main directory from %s", filename);
- /* re-open file for writing */
- fclose(file);
- file = fopen(filename, "r+b");
- count = dir[dirsize - 1].offset + dir[dirsize - 1].size;
- if (file == NULL || fseek(file, count, SEEK_SET) < 0)
- ProgError("Cannot re-open file for writing (%s)", filename);
- }
-
- /* parse the index file and read all the files listed in it */
- if (indexname != NULL)
- ProgError("Option -i not implemented yet. Sorry...\n"); /*! missing */
-
- /* if there are extra arguments on the command line, read these files too */
- for (argv++, argc--; argc; argv++, argc--)
- {
- entryname = QStrNDupHack(*argv, 8);
- srcfile = fopen(*argv, "rb");
- if (srcfile == NULL)
- ProgError("File not found (%s)", *argv);
- size = GetFileSize(srcfile);
- printf("Copying %lu bytes from %s\n", size, *argv);
- if (CopyBytes(file, srcfile, size) == FALSE)
- ProgError("Cannot copy data");
- if (AddWADEntry(file, &count, &dir, &dirsize, entryname, size) == FALSE)
- ProgError("Cannot register entry in WAD directory (%s)", entryname);
- fclose(srcfile);
- }
-
- /* write the directory */
- size = WriteWADDirectory(file, &count, dir, dirsize);
- if (size == 0L)
- ProgError("Cannot write WAD directory");
- printf("Total size of %s: %lu bytes\n", filename, size);
-
- /* close the file and say goodbye */
- fclose(file);
- exit(0);
- }
-