home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / qdtool10 / qdfunct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-03  |  2.9 KB  |  140 lines

  1. /*
  2.  * Copyright (C) 1996 by Chris Johnson.  All rights reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and
  5.  * its documentation for any purpose and without fee is hereby
  6.  * granted, provided that the above copyright notice appear in all
  7.  * copies and that both that copyright notice and this permission
  8.  * notice appear in supporting documentation.  If more than a few
  9.  * lines of this code are used in a program which displays a copyright
  10.  * notice or credit notice, the following acknowledgment must also be
  11.  * displayed on the same screen: "This product includes software
  12.  * developed by Chris Johnson for use in the QuakeDef Tools package."
  13.  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESSED OR IMPLIED
  14.  * WARRANTY.
  15.  *
  16.  * (Thanks to Raphael Quinet for this nifty disclaimer!)
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. #include "qdfunct.h"
  23.  
  24. long *grab_addresses(FILE *bsp)
  25. {
  26.     long *    hdr_ptr;
  27.     long    loc_sav;
  28.  
  29.     hdr_ptr = (long *)malloc(sizeof(long) * BH_NELEM);
  30.  
  31.     if (hdr_ptr == NULL)
  32.     {
  33.         fprintf(stderr,
  34.                 "\nFatal error: Out of memory at grab_addresses()\n");
  35.  
  36.         return (NULL);
  37.     }
  38.  
  39.     loc_sav = ftell(bsp);
  40.     rewind(bsp);
  41.  
  42.     fread(hdr_ptr, sizeof(long), BH_NELEM, bsp);
  43.  
  44.     fseek(bsp, loc_sav, SEEK_SET);
  45.  
  46.     return (hdr_ptr);
  47. }
  48.  
  49. // Returns: EOF on premature EOF
  50. //            0 otherwise
  51. int export_quake_def(FILE *bsp, FILE *qdf, unsigned long len)
  52. {
  53.     int        cur_char;
  54.     int        prev_char = 0;
  55.  
  56.     // Make it so
  57.     for (; len > 0; len--)
  58.     {
  59.         if ((cur_char = fgetc(bsp)) == EOF)
  60.             return (EOF);
  61.  
  62.         // Normally a terminating character
  63.         if (cur_char == 0x00)
  64.             break;
  65.  
  66.         // Should we indent?
  67.         if (prev_char == 0x0A && cur_char != '{' && cur_char != '}')
  68.             fputc(' ', qdf);
  69.  
  70.         // Put it... IN....
  71.         fputc(cur_char, qdf);
  72.  
  73.         // Should we put an extra <CR>?
  74.         if (cur_char == '}')
  75.             fputc('\n', qdf);
  76.  
  77.         // Keep track of last character
  78.         prev_char = cur_char;
  79.     }
  80.  
  81.     return (0);
  82. }
  83.  
  84. void import_quake_def(FILE *qdf, FILE *new_bsp)
  85. {
  86.     char     buffer[BUFSIZ];
  87.     int     cur_char;
  88.     int     prev_char = 0;
  89.     int        ctr;
  90.  
  91.     for (ctr = 0; ((cur_char = fgetc(qdf)) != EOF);)
  92.     {
  93.         if (ctr >= BUFSIZ)
  94.         {
  95.             fwrite(buffer, sizeof(char) * BUFSIZ, 1, new_bsp);
  96.             ctr = 0;
  97.         }
  98.  
  99.         if (cur_char == ' ' && prev_char == '\n')
  100.             continue;
  101.  
  102.         if (cur_char == '\n')
  103.         {
  104.             if (prev_char != '\n')
  105.             {
  106.                 buffer[ctr] = 0x0A;
  107.                 ctr++;
  108.  
  109.                 prev_char = cur_char;
  110.             }
  111.  
  112.             continue;
  113.         }
  114.  
  115.         buffer[ctr] = cur_char;
  116.         ctr++;
  117.  
  118.         prev_char = cur_char;
  119.     }
  120.  
  121.     // Any unwritten data?
  122.     if (ctr)
  123.         fwrite(buffer, sizeof(char) * ctr, 1, new_bsp);
  124. }
  125.  
  126. void fix_bsp_hdr(FILE *bsp, long *ara, long diff)
  127. {
  128.     long loc_sav;
  129.  
  130.     loc_sav = ftell(bsp);
  131.     rewind(bsp);
  132.  
  133.     ara[BH_POSTQD_ELEM] += diff;
  134.     ara[BH_QDSIZE_ELEM] =
  135.         ara[BH_POSTQD_ELEM] - ara[BH_QD_ELEM] - 1;
  136.  
  137.     fwrite(ara, sizeof(long), BH_NELEM, bsp);
  138.  
  139.     fseek(bsp, loc_sav, SEEK_SET);
  140. }