home *** CD-ROM | disk | FTP | other *** search
- /***
- **** QuBE --- "Thing" manipulation routines.
- ***/
-
- #include "qube.h"
- #include "thing.h"
-
- /* Thingarray. Maximum 1024 things, which is fine for now, but again, it
- should be a linked list or something more expandable */
-
- thing *thingarray[1024];
- int nextthing = 0;
- int curthing = 0;
- long int linenum = 0;
-
- long int readcount;
- int putback = 0;
- int lastread;
- long int maxlen;
-
- /*
- ** ThingList. Prints out the things, verbatim.
- */
-
- void ThingList(void)
- {
- int c;
-
- if (header.id != 0x17)
- Error("Not a valid .BSP file");
-
- fseek(fi, header.things, SEEK_SET);
- readcount = 0L;
- maxlen = header.thingslen;
- while ((c = nextchar(fi)) != EOF) printf("%c", c);
- }
-
- /*
- ** ThingXtract. Extracts out the things, verbatim.
- */
-
- void ThingXtract(int argnum, char **argv)
- {
- int c;
- FILE *fo;
-
- if (header.id != 0x17)
- Error("Not a valid .BSP file");
-
- fseek(fi, header.things, SEEK_SET);
- readcount = 0L;
-
- maxlen = header.thingslen;
- if ((fo = fopen(argv[argnum+1], "wt")) == NULL)
- Error("Unable to create output file.");
-
- while ((c = nextchar(fi)) != EOF) fprintf(fo, "%c", c);
-
- fclose(fo);
- }
-
- /*
- ** ThingReplace. Replaces the things in a .BSP file.
- */
-
- void ThingReplace(int argnum, char **argv)
- {
- FILE *fo, *tf;
- char *tempstr, *tempstr2;
- long charswritten = 0L;
- char *temp;
- long int i, j, k;
- long int readsize;
-
- maxlen = 0x7FFFFFFF;
-
- if (header.id != 0x17)
- Error("Not a valid .BSP file");
-
- readcount = 0L;
-
- temp = malloc(32768);
-
- if ((fo = fopen("$$$_temp.bsp", "wb")) == NULL)
- Error("Unable to create output file.");
-
- setvbuf(fo, NULL, _IOFBF, 32768);
-
- fseek(fi, 0, SEEK_SET);
- fseek(fo, 0, SEEK_SET);
-
- for (j = 0L; j < header.things; j += 32768L) {
- if (header.things - j >= 32768) k = 32768;
- else k = header.things - j;
- readsize = fread(temp, sizeof(char), k, fi);
- fwrite(temp, sizeof(char), readsize, fo);
- }
-
- if ((tf = fopen(argv[argnum+1], "rt")) == NULL)
- Error("Unable to open thing file.");
-
- setvbuf(tf, NULL, _IOFBF, 32768);
-
- while (!feof(tf)) {
- skipspace(tf);
- if (!getopenbrace(tf))
- Error("Missing opening brace in thing file.");
-
- fprintf(fo, "{\n");
-
- do {
- skipspace(tf);
- if (nextchar(tf) == '}') break;
- putback = 1;
- if (fileend())
- Error("Early end-of-file in thing file");
- if ((tempstr = getstring(tf)) == NULL)
- Error("Syntax error (line %ld) in thing file; type needed", linenum);
- skipspace(tf);
- if (fileend())
- Error("Early end-of-file in thing file");
- if ((tempstr2 = getstring(tf)) == NULL)
- Error("Syntax error (line %ld) in thing file; entry needed", linenum);
- fprintf(fo, "\"%s\" \"%s\"\n", tempstr, tempstr2);
- free(tempstr);
- free(tempstr2);
- skipspace(tf);
- } while (!feof(tf));
-
- skipspace(tf);
-
- fprintf(fo, "}\n");
- }
- fclose(tf);
-
- charswritten = ftell(fo) - header.things;
- header.pictures += charswritten - header.thingslen;
- fseek(fi, header.things + header.thingslen, SEEK_SET);
-
- while (!feof(fi)) {
- readsize = fread(temp, sizeof(char), 32768, fi);
- fwrite(temp, sizeof(char), readsize, fo);
- }
-
- fseek(fo, 0, SEEK_SET);
- header.thingslen = charswritten;
- fwrite(&header, 4, 0x50, fo);
-
- free(temp);
-
- fclose(fi);
- fclose(fo);
-
- unlink(argv[filenamearg]);
- rename("$$$_temp.bsp", argv[filenamearg]);
- }
-
- /*
- ** getstring. Reads in a "" delimited string, stripping the quotes.
- */
-
- char *getstring(FILE *fp)
- {
- char *temp = malloc(32);
- int totallen = 32;
- int curlen = 0;
- int c;
-
- if (nextchar(fp) != '\"') {
- free(temp);
- return(NULL);
- }
-
- do {
- c = nextchar(fp);
-
- if (curlen >= totallen) temp = realloc(temp, curlen += 32);
-
- temp[curlen++] = c;
- } while (c != '\"' && c != '\n' && c != EOF);
-
- if (c != '\"') {
- free(temp);
- return(NULL);
- }
-
- if (curlen) curlen--;
- temp[curlen] = '\0';
-
- return(temp);
- }
-
- /*
- ** getopenbrace. Reads in a left brace.
- */
-
- int getopenbrace(FILE *fp)
- {
- return(nextchar(fp) == '{');
- }
-
- /*
- ** getclosebrace. Reads in a right brace.
- */
-
- int getclosebrace(FILE *fp)
- {
- return(nextchar(fp) == '}');
- }
-
- /*
- ** skipspace. Skips over whitespace (space, tab, newline).
- */
-
- void skipspace(FILE *fp)
- {
- unsigned char c;
-
- do {
- c = nextchar(fp);
- } while (c <= 32);
-
- putback = 1;
- }
-
- /*
- ** nextchar. Reads in the next character, with putback.
- */
-
- int nextchar(FILE *fp)
- {
- if (putback) {
- putback = 0;
- return(lastread);
- }
-
- if (readcount < maxlen) {
- readcount++;
- lastread = fgetc(fp);
- if (lastread == '\n') linenum++;
- return(lastread);
- }
- else return(lastread = EOF);
- }
-
- /*
- ** fileend. Returns whether we're at the file's end.
- */
-
- int fileend(void)
- {
- if (lastread == EOF) return(1);
- else return(0);
- }
-
-