home *** CD-ROM | disk | FTP | other *** search
/ D!Zone (Collector's Edition) / D_ZONE_CD.ISO / helps / dmijum / wadfile.c < prev    next >
C/C++ Source or Header  |  1994-12-06  |  43KB  |  1,094 lines

  1. /*
  2.  *    WADFILE.C
  3.  *
  4.  *    Version 1.0.0
  5.  *
  6.  *    Abstract:
  7.  *     This module contains all the WADFILE processing routines for the
  8.  *     WadLib library.
  9.  *
  10.  *    History:
  11.  *     1.0.0    (March 31, 1994)
  12.  *
  13.  *  Author:
  14.  *     Michael McMahon
  15.  *
  16.  ****************************************************************************
  17.  * Wadfile Function Directory
  18.  *---------------------------------------------------------------------------
  19.  *Index: Name                  : Description
  20.  *---------------------------------------------------------------------------
  21.  * #1  : WadfileAddLump        : Add a lump to a Wadfile being created.
  22.  * #2  : WadfileClose           : Close file, flush cache, update WAD directory
  23.  * #3  : WadfileCopyEntry       : Copies a lump from one Wadfile to another.
  24.  * #4  : WadfileCopyMap        : Copies a map from one Wadfile to another.
  25.  * #5  : WadfileCreate           : Create WAD file and initialize Wadfile struct.
  26.  * #6  : WadfileGetDirInfo       : Read directory info into Wadfile structure.
  27.  * #7  : WadfileGetNextDirInfo : Move to next directory entry and read it in.
  28.  * #8  : WadfileGetPrevDirInfo : Move to last directory entry and read it in.
  29.  * #9  : WadfileInitialize       : One-time call, set up memory allocators.
  30.  * #10 : WadfileLumpClose       : Close lump inside WAD file, free its memory.
  31.  * #11 : WadfileLumpCopy       : Copy lump file from one Wadfile to another.
  32.  * #12 : WadfileLumpOpen       : Open lump at current directory position.
  33.  * #13 : WadfileOpen           : Open a WAD file and initialize Wadfile struct.
  34.  * #14 : WadfileSeek           : Find first dir entry with a search string.
  35.  * #15 : WadfileSeekMap        : Find map given episode and map. ExMy
  36.  ****************************************************************************
  37.  *
  38.  *  WADLIB SOFTWARE LICENSE AGREEMENT
  39.  *
  40.  *    1. GRANT OF LICENSE. Michael McMahon and his affiliations (collectively
  41.  *       the "AUTHOR") grant you (either an individual or an entity) the
  42.  *       non-exclusive, royalty-free right to use this library source code,
  43.  *       documentation, and sample code (collectively, the "SOFTWARE") for
  44.  *       any lawful purpose subject to the terms of this license.  By using the
  45.  *       SOFTWARE you are agreeing to be bound to all the terms of this license.
  46.  *
  47.  *    2. COPYRIGHT.  The SOFTWARE is Copyright (c) 1994, Michael McMahon,
  48.  *       PO Box 14807, San Luis Nabisco, CA 93406-4807 USA. All Rights Reserved
  49.  *       Worldwide.  You may not use, modify, or distribute the SOFTWARE except
  50.  *       as otherwise provided herein.
  51.  *
  52.  *    3. DECLARATION OF PUBLIC DOMAIN DISTRIBUTION AND USE. The distribution
  53.  *       and use of the SOFTWARE is hereby designated PUBLIC DOMAIN by the
  54.  *       the AUTHOR.    You may not sell, rent, or lease this SOFTWARE.  The
  55.  *       SOFTWARE may be reproduced verbatim in part or in full by any
  56.  *       reproduction means for any lawful purpose, and may also be subject to
  57.  *       the following agreement.
  58.  *
  59.  *    4. AGREEMENT FOR USE OF SOFTWARE. The AUTHOR grants you a non-exclusive,
  60.  *       royalty-free right to incorporate the SOFTWARE into any production for
  61.  *       any legal purpose as long as you agree
  62.  *        (a) to indemnify, hold harmless, and defend the AUTHOR from and against
  63.  *            any claims or lawsuits, including attorneys' fees, that arise or
  64.  *            result from the use or distribution of your software production; and
  65.  *        (b) no matter how much the SOFTWARE is modified, the AUTHOR owns the
  66.  *            copyright and this original, unmodified copyright notice remains
  67.  *            intact in the source code; and,
  68.  *        (c) the AUTHOR is not held responsible for fixing bugs or making
  69.  *            enhancements or changes to the SOFTWARE for any reason; and,
  70.  *        (d) the SOFTWARE is not redistributed if it is modified in any way; and,
  71.  *      (e) otherwise comply with the terms of this agreement; and,
  72.  *        (f) the AUTHOR is forgiven for making so many demands.
  73.  *
  74.  *     THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE
  75.  *     AUTHOR FURTHER DISCLAIMS ALL IMPLIED WARRANTIES, INCLUDING WITHOUT
  76.  *     LIMITATION ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR OF FITNESS
  77.  *     FOR A PARTICULAR PURPOSE.    THE ENTIRE RISK ARISING OUT OF THE USE
  78.  *     OR PERFORMANCE OF THE SOFTWARE REMAINS WITH YOU.
  79.  *
  80.  *     The author can be reached at:
  81.  *      Michael McMahon
  82.  *      P.O. Box 14807
  83.  *      San Luis Nabisco, CA 93406-4807 USA
  84.  *      Internet: mmcmahon@oboe.calpoly.edu
  85.  *      [Bug reports, suggestions, success stories, etc. are welcome; tech
  86.  *       support, and other unnecessary two-way mail, is not]
  87.  */
  88.  
  89. #include <assert.h>
  90. #include <stdio.h>
  91. #include <string.h>
  92. #include <mem.h>
  93. #include "general.h"
  94. #include "wadfile.h"
  95.  
  96. /* Private wadfile variables */
  97. static int    wadfileInitialized=FALSE; /* Module doesn't work unless TRUE */
  98. static WadfileMalloc  wadfileMalloc;  /* User's allocate memory routine  */
  99. static WadfileFree      wadfileFree;      /* User's free memory routine      */
  100. static int wadfileNumAllocations;      /* Keeps track of the malloc's     */
  101.  
  102. /*------------------------- Private routines ------------------------------*/
  103.  
  104. /**[ Internal 1 of 2 ]****************************************************
  105.  *                                                                       *
  106.  *      wf_malloc                                                          *
  107.  *                                                                         *
  108.  * Desc:                                                                 *
  109.  *       This calls the user's memory allocation routine and updates       *
  110.  *       the 'wadfileNumAllocations' variable.                             *
  111.  *                                                                         *
  112.  * Def:                                                                  *
  113.  *       static void * wf_malloc(unsigned long size);                      *
  114.  *                                                                       *
  115.  * Parm:                                                                 *
  116.  *       size    Number of bytes to allocate.                              *
  117.  *                                                                         *
  118.  * Retn:                                                                 *
  119.  *       Pointer to allocated memory, or NULL if not enough memory.         *
  120.  *                                                                         *
  121.  * Notes:                                                                *
  122.  *       This is the gateway for the WADFILE routines to access the user's *
  123.  *       memory allocation routine. Don't call 'wadfileMalloc()' directly. *
  124.  *                                                                         *
  125.  *************************************************************************/
  126.  
  127. static void * wf_malloc(unsigned long size)
  128. {
  129.     void * result;
  130.  
  131.     /* Sanity check */
  132.     assert(wadfileMalloc != NULL);
  133.  
  134.     /* Allocate memory and bump wadfileNumAllocations if successful */
  135.     result = wadfileMalloc(size);
  136.     if (result != NULL)
  137.       wadfileNumAllocations++;
  138.  
  139.     return result;
  140. }
  141.  
  142. /**[ Internal 2 of 2 ]****************************************************
  143.  *                                                                       *
  144.  *    wf_free                                                            *
  145.  *                                                                       *
  146.  * Desc:                                                                 *
  147.  *     This calls the user's memory de-allocation routine and updates    *
  148.  *     the 'wadfileNumAllocations' variable.                             *
  149.  *                                                                       *
  150.  * Def:                                                                  *
  151.  *       static void wf_free(void * ptr);                                  *
  152.  *                                                                       *
  153.  * Parm:                                                                 *
  154.  *       ptr       Valid memory pointer that was allocated with wf_malloc.     *
  155.  *                                                                       *
  156.  * Retn:                                                                 *
  157.  *       None                                                              *
  158.  *                                                                         *
  159.  * Notes:                                                                *
  160.  *     This is the gateway for the WADFILE routines to access the user's *
  161.  *       memory deallocation routine. Don't call 'wadfileFree()' directly. *
  162.  *                                                                       *
  163.  *************************************************************************/
  164.  
  165. static void wf_free(void * ptr)
  166. {
  167.     /* Sanity