Source:NetHack 3.6.0/src/dlb.c

From NetHackWiki
(Redirected from Source:NetHack 3.6.0/dlb.c)
Jump to: navigation, search

Below is the full text to dlb.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/dlb.c#line123]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

Top of file

  1.  /* NetHack 3.6	dlb.c	$NHDT-Date: 1446975464 2015/11/08 09:37:44 $  $NHDT-Branch: master $:$NHDT-Revision: 1.15 $ */
  2.  /* Copyright (c) Kenneth Lorber, Bethesda, Maryland, 1993. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "config.h"
  6.  #include "dlb.h"
  7.  
  8.  #ifdef __DJGPP__
  9.  #include <string.h>
  10.  #endif
  11.  
  12.  #define DATAPREFIX 4
  13.  
  14.  #if defined(OVERLAY)
  15.  #define STATIC_DCL extern
  16.  #define STATIC_OVL
  17.  #else /* !OVERLAY */
  18.  #define STATIC_DCL static
  19.  #define STATIC_OVL static
  20.  #endif /* OVERLAY */
  21.  
  22.  #ifdef DLB
  23.  /*
  24.   * Data librarian.  Present a STDIO-like interface to NetHack while
  25.   * multiplexing on one or more "data libraries".  If a file is not found
  26.   * in a given library, look for it outside the libraries.
  27.   */
  28.  
  29.  typedef struct dlb_procs {
  30.      boolean NDECL((*dlb_init_proc));
  31.      void NDECL((*dlb_cleanup_proc));
  32.      boolean FDECL((*dlb_fopen_proc), (DLB_P, const char *, const char *));
  33.      int FDECL((*dlb_fclose_proc), (DLB_P));
  34.      int FDECL((*dlb_fread_proc), (char *, int, int, DLB_P));
  35.      int FDECL((*dlb_fseek_proc), (DLB_P, long, int));
  36.      char *FDECL((*dlb_fgets_proc), (char *, int, DLB_P));
  37.      int FDECL((*dlb_fgetc_proc), (DLB_P));
  38.      long FDECL((*dlb_ftell_proc), (DLB_P));
  39.  } dlb_procs_t;
  40.  
  41.  /* without extern.h via hack.h, these haven't been declared for us */
  42.  extern FILE *FDECL(fopen_datafile, (const char *, const char *, int));
  43.  

DLBLIB functions

  1.  #ifdef DLBLIB
  2.  /*
  3.   * Library Implementation:
  4.   *
  5.   * When initialized, we open all library files and read in their tables
  6.   * of contents.  The library files stay open all the time.  When
  7.   * a open is requested, the libraries' directories are searched.  If
  8.   * successful, we return a descriptor that contains the library, file
  9.   * size, and current file mark.  This descriptor is used for all
  10.   * successive calls.
  11.   *
  12.   * The ability to open more than one library is supported but used
  13.   * only in the Amiga port (the second library holds the sound files).
  14.   * For Unix, the idea would be to split the NetHack library
  15.   * into text and binary parts, where the text version could be shared.
  16.   */
  17.  
  18.  #define MAX_LIBS 4
  19.  static library dlb_libs[MAX_LIBS];
  20.  
  21.  STATIC_DCL boolean FDECL(readlibdir, (library * lp));
  22.  STATIC_DCL boolean FDECL(find_file, (const char *name, library **lib,
  23.                                       long *startp, long *sizep));
  24.  STATIC_DCL boolean NDECL(lib_dlb_init);
  25.  STATIC_DCL void NDECL(lib_dlb_cleanup);
  26.  STATIC_DCL boolean FDECL(lib_dlb_fopen, (dlb *, const char *, const char *));
  27.  STATIC_DCL int FDECL(lib_dlb_fclose, (dlb *));
  28.  STATIC_DCL int FDECL(lib_dlb_fread, (char *, int, int, dlb *));
  29.  STATIC_DCL int FDECL(lib_dlb_fseek, (dlb *, long, int));
  30.  STATIC_DCL char *FDECL(lib_dlb_fgets, (char *, int, dlb *));
  31.  STATIC_DCL int FDECL(lib_dlb_fgetc, (dlb *));
  32.  STATIC_DCL long FDECL(lib_dlb_ftell, (dlb *));
  33.  
  34.  /* not static because shared with dlb_main.c */
  35.  boolean FDECL(open_library, (const char *lib_name, library *lp));
  36.  void FDECL(close_library, (library * lp));
  37.  
  38.  /* without extern.h via hack.h, these haven't been declared for us */
  39.  extern char *FDECL(eos, (char *));
  40.  
  41.  /*
  42.   * Read the directory out of the library.  Return 1 if successful,
  43.   * 0 if it failed.
  44.   *
  45.   * NOTE: An improvement of the file structure should be the file
  46.   * size as part of the directory entry or perhaps in place of the
  47.   * offset -- the offset can be calculated by a running tally of
  48.   * the sizes.
  49.   *
  50.   * Library file structure:
  51.   *
  52.   * HEADER:
  53.   * %3ld library FORMAT revision (currently rev 1)
  54.   * %1c  space
  55.   * %8ld # of files in archive (includes 1 for directory)
  56.   * %1c  space
  57.   * %8ld size of allocation for string space for directory names
  58.   * %1c  space
  59.   * %8ld library offset - sanity check - lseek target for start of first file
  60.   * %1c  space
  61.   * %8ld size - sanity check - byte size of complete archive file
  62.   *
  63.   * followed by one DIRECTORY entry for each file in the archive, including
  64.   *  the directory itself:
  65.   * %1c  handling information (compression, etc.)  Always ' ' in rev 1.
  66.   * %s   file name
  67.   * %1c  space
  68.   * %8ld offset in archive file of start of this file
  69.   * %c   newline
  70.   *
  71.   * followed by the contents of the files
  72.   */
  73.  #define DLB_MIN_VERS 1 /* min library version readable by this code */
  74.  #define DLB_MAX_VERS 1 /* max library version readable by this code */
  75.  

readlibdir

  1.  /*
  2.   * Read the directory from the library file.   This will allocate and
  3.   * fill in our globals.  The file pointer is reset back to position
  4.   * zero.  If any part fails, leave nothing that needs to be deallocated.
  5.   *
  6.   * Return TRUE on success, FALSE on failure.
  7.   */
  8.  STATIC_OVL boolean
  9.  readlibdir(lp)
  10.  library *lp; /* library pointer to fill in */
  11.  {
  12.      int i;
  13.      char *sp;
  14.      long liboffset, totalsize;
  15.  
  16.      if (fscanf(lp->fdata, "%ld %ld %ld %ld %ld\n", &lp->rev, &lp->nentries,
  17.                 &lp->strsize, &liboffset, &totalsize) != 5)
  18.          return FALSE;
  19.      if (lp->rev > DLB_MAX_VERS || lp->rev < DLB_MIN_VERS)
  20.          return FALSE;
  21.  
  22.      lp->dir = (libdir *) alloc(lp->nentries * sizeof(libdir));
  23.      lp->sspace = (char *) alloc(lp->strsize);
  24.  
  25.      /* read in each directory entry */
  26.      for (i = 0, sp = lp->sspace; i < lp->nentries; i++) {
  27.          lp->dir[i].fname = sp;
  28.          if (fscanf(lp->fdata, "%c%s %ld\n", &lp->dir[i].handling, sp,
  29.                     &lp->dir[i].foffset) != 3) {
  30.              free((genericptr_t) lp->dir);
  31.              free((genericptr_t) lp->sspace);
  32.              lp->dir = (libdir *) 0;
  33.              lp->sspace = (char *) 0;
  34.              return FALSE;
  35.          }
  36.          sp = eos(sp) + 1;
  37.      }
  38.  
  39.      /* calculate file sizes using offset information */
  40.      for (i = 0; i < lp->nentries; i++) {
  41.          if (i == lp->nentries - 1)
  42.              lp->dir[i].fsize = totalsize - lp->dir[i].foffset;
  43.          else
  44.              lp->dir[i].fsize = lp->dir[i + 1].foffset - lp->dir[i].foffset;
  45.      }
  46.  
  47.      (void) fseek(lp->fdata, 0L, SEEK_SET); /* reset back to zero */
  48.      lp->fmark = 0;
  49.  
  50.      return TRUE;
  51.  }
  52.  

find_file

  1.  /*
  2.   * Look for the file in our directory structure.  Return 1 if successful,
  3.   * 0 if not found.  Fill in the size and starting position.
  4.   */
  5.  STATIC_OVL boolean
  6.  find_file(name, lib, startp, sizep)
  7.  const char *name;
  8.  library **lib;
  9.  long *startp, *sizep;
  10.  {
  11.      int i, j;
  12.      library *lp;
  13.  
  14.      for (i = 0; i < MAX_LIBS && dlb_libs[i].fdata; i++) {
  15.          lp = &dlb_libs[i];
  16.          for (j = 0; j < lp->nentries; j++) {
  17.              if (FILENAME_CMP(name, lp->dir[j].fname) == 0) {
  18.                  *lib = lp;
  19.                  *startp = lp->dir[j].foffset;
  20.                  *sizep = lp->dir[j].fsize;
  21.                  return TRUE;
  22.              }
  23.          }
  24.      }
  25.      *lib = (library *) 0;
  26.      *startp = *sizep = 0;
  27.      return FALSE;
  28.  }
  29.  

open_library

  1.  /*
  2.   * Open the library of the given name and fill in the given library
  3.   * structure.  Return TRUE if successful, FALSE otherwise.
  4.   */
  5.  boolean
  6.  open_library(lib_name, lp)
  7.  const char *lib_name;
  8.  library *lp;
  9.  {
  10.      boolean status = FALSE;
  11.  
  12.      lp->fdata = fopen_datafile(lib_name, RDBMODE, DATAPREFIX);
  13.      if (lp->fdata) {
  14.          if (readlibdir(lp)) {
  15.              status = TRUE;
  16.          } else {
  17.              (void) fclose(lp->fdata);
  18.              lp->fdata = (FILE *) 0;
  19.          }
  20.      }
  21.      return status;
  22.  }
  23.  

close_library

  1.  void
  2.  close_library(lp)
  3.  library *lp;
  4.  {
  5.      (void) fclose(lp->fdata);
  6.      free((genericptr_t) lp->dir);
  7.      free((genericptr_t) lp->sspace);
  8.  
  9.      (void) memset((char *) lp, 0, sizeof(library));
  10.  }
  11.  

lib_dlb_init

  1.  /*
  2.   * Open the library file once using stdio.  Keep it open, but
  3.   * keep track of the file position.
  4.   */
  5.  STATIC_OVL boolean
  6.  lib_dlb_init(VOID_ARGS)
  7.  {
  8.      /* zero out array */
  9.      (void) memset((char *) &dlb_libs[0], 0, sizeof(dlb_libs));
  10.  
  11.      /* To open more than one library, add open library calls here. */
  12.      if (!open_library(DLBFILE, &dlb_libs[0]))
  13.          return FALSE;
  14.  #ifdef DLBFILE2
  15.      if (!open_library(DLBFILE2, &dlb_libs[1])) {
  16.          close_library(&dlb_libs[0]);
  17.          return FALSE;
  18.      }
  19.  #endif
  20.      return TRUE;
  21.  }
  22.  

lib_dlb_cleanup

  1.  STATIC_OVL void
  2.  lib_dlb_cleanup(VOID_ARGS)
  3.  {
  4.      int i;
  5.  
  6.      /* close the data file(s) */
  7.      for (i = 0; i < MAX_LIBS && dlb_libs[i].fdata; i++)
  8.          close_library(&dlb_libs[i]);
  9.  }
  10.  

lib_dlb_fopen

  1.  /*ARGSUSED*/
  2.  STATIC_OVL boolean
  3.  lib_dlb_fopen(dp, name, mode)
  4.  dlb *dp;
  5.  const char *name;
  6.  const char *mode UNUSED;
  7.  {
  8.      long start, size;
  9.      library *lp;
  10.  
  11.      /* look up file in directory */
  12.      if (find_file(name, &lp, &start, &size)) {
  13.          dp->lib = lp;
  14.          dp->start = start;
  15.          dp->size = size;
  16.          dp->mark = 0;
  17.          return TRUE;
  18.      }
  19.  
  20.      return FALSE; /* failed */
  21.  }
  22.  

lib_dlb_fclose

  1.  /*ARGUSED*/
  2.  STATIC_OVL int
  3.  lib_dlb_fclose(dp)
  4.  dlb *dp UNUSED;
  5.  {
  6.      /* nothing needs to be done */
  7.      return 0;
  8.  }
  9.  

lib_dlb_fread

  1.  STATIC_OVL int
  2.  lib_dlb_fread(buf, size, quan, dp)
  3.  char *buf;
  4.  int size, quan;
  5.  dlb *dp;
  6.  {
  7.      long pos, nread, nbytes;
  8.  
  9.      /* make sure we don't read into the next file */
  10.      if ((dp->size - dp->mark) < (size * quan))
  11.          quan = (dp->size - dp->mark) / size;
  12.      if (quan == 0)
  13.          return 0;
  14.  
  15.      pos = dp->start + dp->mark;
  16.      if (dp->lib->fmark != pos) {
  17.          fseek(dp->lib->fdata, pos, SEEK_SET); /* check for error??? */
  18.          dp->lib->fmark = pos;
  19.      }
  20.  
  21.      nread = fread(buf, size, quan, dp->lib->fdata);
  22.      nbytes = nread * size;
  23.      dp->mark += nbytes;
  24.      dp->lib->fmark += nbytes;
  25.  
  26.      return nread;
  27.  }
  28.  

lib_dlb_fseek

  1.  STATIC_OVL int
  2.  lib_dlb_fseek(dp, pos, whence)
  3.  dlb *dp;
  4.  long pos;
  5.  int whence;
  6.  {
  7.      long curpos;
  8.  
  9.      switch (whence) {
  10.      case SEEK_CUR:
  11.          curpos = dp->mark + pos;
  12.          break;
  13.      case SEEK_END:
  14.          curpos = dp->size - pos;
  15.          break;
  16.      default: /* set */
  17.          curpos = pos;
  18.          break;
  19.      }
  20.      if (curpos < 0)
  21.          curpos = 0;
  22.      if (curpos > dp->size)
  23.          curpos = dp->size;
  24.  
  25.      dp->mark = curpos;
  26.      return 0;
  27.  }
  28.  

lib_dlb_fgets

  1.  STATIC_OVL char *
  2.  lib_dlb_fgets(buf, len, dp)
  3.  char *buf;
  4.  int len;
  5.  dlb *dp;
  6.  {
  7.      int i;
  8.      char *bp, c = 0;
  9.  
  10.      if (len <= 0)
  11.          return buf; /* sanity check */
  12.  
  13.      /* return NULL on EOF */
  14.      if (dp->mark >= dp->size)
  15.          return (char *) 0;
  16.  
  17.      len--; /* save room for null */
  18.      for (i = 0, bp = buf; i < len && dp->mark < dp->size && c != '\n';
  19.           i++, bp++) {
  20.          if (dlb_fread(bp, 1, 1, dp) <= 0)
  21.              break; /* EOF or error */
  22.          c = *bp;
  23.      }
  24.      *bp = '\0';
  25.  
  26.  #if defined(MSDOS) || defined(WIN32)
  27.      if ((bp = index(buf, '\r')) != 0) {
  28.          *bp++ = '\n';
  29.          *bp = '\0';
  30.      }
  31.  #endif
  32.  
  33.      return buf;
  34.  }
  35.  

lib_dlb_fgetc

  1.  STATIC_OVL int
  2.  lib_dlb_fgetc(dp)
  3.  dlb *dp;
  4.  {
  5.      char c;
  6.  
  7.      if (lib_dlb_fread(&c, 1, 1, dp) != 1)
  8.          return EOF;
  9.      return (int) c;
  10.  }
  11.  

lib_dlb_ftell

  1.  STATIC_OVL long
  2.  lib_dlb_ftell(dp)
  3.  dlb *dp;
  4.  {
  5.      return dp->mark;
  6.  }
  7.  
  8.  const dlb_procs_t lib_dlb_procs = { lib_dlb_init,  lib_dlb_cleanup,
  9.                                      lib_dlb_fopen, lib_dlb_fclose,
  10.                                      lib_dlb_fread, lib_dlb_fseek,
  11.                                      lib_dlb_fgets, lib_dlb_fgetc,
  12.                                      lib_dlb_ftell };
  13.  
  14.  #endif /* DLBLIB */
  15.  
  16.  #ifdef DLBRSRC
  17.  const dlb_procs_t rsrc_dlb_procs = { rsrc_dlb_init,  rsrc_dlb_cleanup,
  18.                                       rsrc_dlb_fopen, rsrc_dlb_fclose,
  19.                                       rsrc_dlb_fread, rsrc_dlb_fseek,
  20.                                       rsrc_dlb_fgets, rsrc_dlb_fgetc,
  21.                                       rsrc_dlb_ftell };
  22.  #endif
  23.  

Global wrapper functions

  1.  /* Global wrapper functions ------------------------------------------------
  2.   */
  3.  
  4.  #define do_dlb_init (*dlb_procs->dlb_init_proc)
  5.  #define do_dlb_cleanup (*dlb_procs->dlb_cleanup_proc)
  6.  #define do_dlb_fopen (*dlb_procs->dlb_fopen_proc)
  7.  #define do_dlb_fclose (*dlb_procs->dlb_fclose_proc)
  8.  #define do_dlb_fread (*dlb_procs->dlb_fread_proc)
  9.  #define do_dlb_fseek (*dlb_procs->dlb_fseek_proc)
  10.  #define do_dlb_fgets (*dlb_procs->dlb_fgets_proc)
  11.  #define do_dlb_fgetc (*dlb_procs->dlb_fgetc_proc)
  12.  #define do_dlb_ftell (*dlb_procs->dlb_ftell_proc)
  13.  
  14.  static const dlb_procs_t *dlb_procs;
  15.  static boolean dlb_initialized = FALSE;
  16.  

dlb_init

  1.  boolean
  2.  dlb_init()
  3.  {
  4.      if (!dlb_initialized) {
  5.  #ifdef DLBLIB
  6.          dlb_procs = &lib_dlb_procs;
  7.  #endif
  8.  #ifdef DLBRSRC
  9.          dlb_procs = &rsrc_dlb_procs;
  10.  #endif
  11.  
  12.          if (dlb_procs)
  13.              dlb_initialized = do_dlb_init();
  14.      }
  15.  
  16.      return dlb_initialized;
  17.  }
  18.  

dlb_cleanup

  1.  void
  2.  dlb_cleanup()
  3.  {
  4.      if (dlb_initialized) {
  5.          do_dlb_cleanup();
  6.          dlb_initialized = FALSE;
  7.      }
  8.  }
  9.  

dlb_fopen

  1.  dlb *
  2.  dlb_fopen(name, mode)
  3.  const char *name, *mode;
  4.  {
  5.      FILE *fp;
  6.      dlb *dp;
  7.  
  8.      if (!dlb_initialized)
  9.          return (dlb *) 0;
  10.  
  11.      /* only support reading; ignore possible binary flag */
  12.      if (!mode || mode[0] != 'r')
  13.          return (dlb *) 0;
  14.  
  15.      dp = (dlb *) alloc(sizeof(dlb));
  16.      if (do_dlb_fopen(dp, name, mode))
  17.          dp->fp = (FILE *) 0;
  18.      else if ((fp = fopen_datafile(name, mode, DATAPREFIX)) != 0)
  19.          dp->fp = fp;
  20.      else {
  21.          /* can't find anything */
  22.          free((genericptr_t) dp);
  23.          dp = (dlb *) 0;
  24.      }
  25.  
  26.      return dp;
  27.  }
  28.  

dlb_fclose

  1.  int
  2.  dlb_fclose(dp)
  3.  dlb *dp;
  4.  {
  5.      int ret = 0;
  6.  
  7.      if (dlb_initialized) {
  8.          if (dp->fp)
  9.              ret = fclose(dp->fp);
  10.          else
  11.              ret = do_dlb_fclose(dp);
  12.  
  13.          free((genericptr_t) dp);
  14.      }
  15.      return ret;
  16.  }
  17.  

dlb_fread

  1.  int
  2.  dlb_fread(buf, size, quan, dp)
  3.  char *buf;
  4.  int size, quan;
  5.  dlb *dp;
  6.  {
  7.      if (!dlb_initialized || size <= 0 || quan <= 0)
  8.          return 0;
  9.      if (dp->fp)
  10.          return (int) fread(buf, size, quan, dp->fp);
  11.      return do_dlb_fread(buf, size, quan, dp);
  12.  }
  13.  

dlb_fseek

  1.  int
  2.  dlb_fseek(dp, pos, whence)
  3.  dlb *dp;
  4.  long pos;
  5.  int whence;
  6.  {
  7.      if (!dlb_initialized)
  8.          return EOF;
  9.      if (dp->fp)
  10.          return fseek(dp->fp, pos, whence);
  11.      return do_dlb_fseek(dp, pos, whence);
  12.  }
  13.  

dlb_fgets

  1.  char *
  2.  dlb_fgets(buf, len, dp)
  3.  char *buf;
  4.  int len;
  5.  dlb *dp;
  6.  {
  7.      if (!dlb_initialized)
  8.          return (char *) 0;
  9.      if (dp->fp)
  10.          return fgets(buf, len, dp->fp);
  11.      return do_dlb_fgets(buf, len, dp);
  12.  }
  13.  

dlb_fgetc

  1.  int
  2.  dlb_fgetc(dp)
  3.  dlb *dp;
  4.  {
  5.      if (!dlb_initialized)
  6.          return EOF;
  7.      if (dp->fp)
  8.          return fgetc(dp->fp);
  9.      return do_dlb_fgetc(dp);
  10.  }
  11.  

dlb_ftell

  1.  long
  2.  dlb_ftell(dp)
  3.  dlb *dp;
  4.  {
  5.      if (!dlb_initialized)
  6.          return 0;
  7.      if (dp->fp)
  8.          return ftell(dp->fp);
  9.      return do_dlb_ftell(dp);
  10.  }
  11.  
  12.  #endif /* DLB */
  13.  
  14.  /*dlb.c*/