Source:NetHack 3.4.3/src/dlb.c

From NetHackWiki
(Redirected from Source:Ref/dlb cleanup)
Jump to: navigation, search

Below is the full text to src/dlb.c from NetHack 3.4.3. To link to a particular line, write [[dlb.c#line123]], for example.

Top of file[edit]

  1. /*	SCCS Id: @(#)dlb.c	3.4	1997/07/29	*/
  2. /* Copyright (c) Kenneth Lorber, Bethesda, Maryland, 1993. */
  3. /* NetHack may be freely redistributed.  See license for details. */

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.

  1. #include "config.h"
  2. #include "dlb.h"
  3.  
  4. #ifdef __DJGPP__
  5. #include <string.h>
  6. #endif
  7.  
  8. #define DATAPREFIX 4
  9.  
  10. #ifdef DLB
  11. /*
  12. * Data librarian.  Present a STDIO-like interface to NetHack while
  13. * multiplexing on one or more "data libraries".  If a file is not found
  14. * in a given library, look for it outside the libraries.
  15. */
  16.  
  17. typedef struct dlb_procs {
  18. boolean NDECL((*dlb_init_proc));
  19. void NDECL((*dlb_cleanup_proc));
  20. boolean FDECL((*dlb_fopen_proc), (DLB_P,const char *,const char *));
  21. int FDECL((*dlb_fclose_proc), (DLB_P));
  22. int FDECL((*dlb_fread_proc), (char *,int,int,DLB_P));
  23. int FDECL((*dlb_fseek_proc), (DLB_P,long,int));
  24. char *FDECL((*dlb_fgets_proc), (char *,int,DLB_P));
  25. int FDECL((*dlb_fgetc_proc), (DLB_P));
  26. long FDECL((*dlb_ftell_proc), (DLB_P));
  27. } dlb_procs_t;
  28.  
  29. /* without extern.h via hack.h, these haven't been declared for us */
  30. extern FILE *FDECL(fopen_datafile, (const char *,const char *,int));
  31.  
  32. #ifdef DLBLIB
  33. /*
  34. * Library Implementation:
  35. *
  36. * When initialized, we open all library files and read in their tables
  37. * of contents.  The library files stay open all the time.  When
  38. * a open is requested, the libraries' directories are searched.  If
  39. * successful, we return a descriptor that contains the library, file
  40. * size, and current file mark.  This descriptor is used for all
  41. * successive calls.
  42. *
  43. * The ability to open more than one library is supported but used
  44. * only in the Amiga port (the second library holds the sound files).
  45. * For Unix, the idea would be to split the NetHack library
  46. * into text and binary parts, where the text version could be shared.
  47. */
  48.  
  49. #define MAX_LIBS 4
  50. static library dlb_libs[MAX_LIBS];
  51.  
  52. static boolean FDECL(readlibdir,(library *lp));
  53. static boolean FDECL(find_file,(const char *name, library **lib, long *startp,
  54. 								long *sizep));
  55. static boolean NDECL(lib_dlb_init);
  56. static void NDECL(lib_dlb_cleanup);
  57. static boolean FDECL(lib_dlb_fopen,(dlb *, const char *, const char *));
  58. static int FDECL(lib_dlb_fclose,(dlb *));
  59. static int FDECL(lib_dlb_fread,(char *, int, int, dlb *));
  60. static int FDECL(lib_dlb_fseek,(dlb *, long, int));
  61. static char *FDECL(lib_dlb_fgets,(char *, int, dlb *));
  62. static int FDECL(lib_dlb_fgetc,(dlb *));
  63. static long FDECL(lib_dlb_ftell,(dlb *));
  64.  
  65. /* not static because shared with dlb_main.c */
  66. boolean FDECL(open_library,(const char *lib_name, library *lp));
  67. void FDECL(close_library,(library *lp));
  68.  
  69. /* without extern.h via hack.h, these haven't been declared for us */
  70. extern char *FDECL(eos, (char *));

readlibdir[edit]

  1. /*
  2. * Read the directory out of the library.  Return 1 if successful,
  3. * 0 if it failed.
  4. *
  5. * NOTE: An improvement of the file structure should be the file
  6. * size as part of the directory entry or perhaps in place of the
  7. * offset -- the offset can be calculated by a running tally of
  8. * the sizes.
  9. *
  10. * Library file structure:
  11. *
  12. * HEADER:
  13. * %3ld	library FORMAT revision (currently rev 1)
  14. * %1c	space
  15. * %8ld	# of files in archive (includes 1 for directory)
  16. * %1c	space
  17. * %8ld	size of allocation for string space for directory names
  18. * %1c	space
  19. * %8ld	library offset - sanity check - lseek target for start of first file
  20. * %1c	space
  21. * %8ld	size - sanity check - byte size of complete archive file
  22. *
  23. * followed by one DIRECTORY entry for each file in the archive, including
  24. *  the directory itself:
  25. * %1c	handling information (compression, etc.)  Always ' ' in rev 1.
  26. * %s	file name
  27. * %1c	space
  28. * %8ld	offset in archive file of start of this file
  29. * %c	newline
  30. *
  31. * followed by the contents of the files
  32. */
  33. #define DLB_MIN_VERS  1	/* min library version readable by this code */
  34. #define DLB_MAX_VERS  1	/* max library version readable by this code */
  35.  
  36. /*
  37. * Read the directory from the library file.   This will allocate and
  38. * fill in our globals.  The file pointer is reset back to position
  39. * zero.  If any part fails, leave nothing that needs to be deallocated.
  40. *
  41. * Return TRUE on success, FALSE on failure.
  42. */
  43. static boolean
  44. readlibdir(lp)
  45. library *lp;	/* library pointer to fill in */
  46. {
  47. int i;
  48. char *sp;
  49. long liboffset, totalsize;
  50.  
  51. if (fscanf(lp->fdata, "%ld %ld %ld %ld %ld\n",
  52. 	    &lp->rev,&lp->nentries,&lp->strsize,&liboffset,&totalsize) != 5)
  53. 	return FALSE;
  54. if (lp->rev > DLB_MAX_VERS || lp->rev < DLB_MIN_VERS) return FALSE;
  55.  
  56. lp->dir = (libdir *) alloc(lp->nentries * sizeof(libdir));
  57. lp->sspace = (char *) alloc(lp->strsize);
  58.  
  59. /* read in each directory entry */
  60. for (i = 0, sp = lp->sspace; i < lp->nentries; i++) {
  61. 	lp->dir[i].fname = sp;
  62. 	if (fscanf(lp->fdata, "%c%s %ld\n",
  63. 			&lp->dir[i].handling, sp, &lp->dir[i].foffset) != 3) {
  64. 	    free((genericptr_t) lp->dir);
  65. 	    free((genericptr_t) lp->sspace);
  66. 	    lp->dir = (libdir *) 0;
  67. 	    lp->sspace = (char *) 0;
  68. 	    return FALSE;
  69. 	}
  70. 	sp = eos(sp) + 1;
  71. }
  72.  
  73. /* calculate file sizes using offset information */
  74. for (i = 0; i < lp->nentries; i++) {
  75. 	if (i == lp->nentries - 1)
  76. 	    lp->dir[i].fsize = totalsize - lp->dir[i].foffset;
  77. 	else
  78. 	    lp->dir[i].fsize = lp->dir[i+1].foffset - lp->dir[i].foffset;
  79. }
  80.  
  81. (void) fseek(lp->fdata, 0L, SEEK_SET);	/* reset back to zero */
  82. lp->fmark = 0;
  83.  
  84. return TRUE;
  85. }

find_file[edit]

  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 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. }

open_library[edit]

  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. }

close_library[edit]

  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. }

lib_dlb_init[edit]

  1. /*
  2. * Open the library file once using stdio.  Keep it open, but
  3. * keep track of the file position.
  4. */
  5. static boolean
  6. lib_dlb_init()
  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])) return FALSE;
  13. #ifdef DLBFILE2
  14. if (!open_library(DLBFILE2, &dlb_libs[1]))  {
  15. 	close_library(&dlb_libs[0]);
  16. 	return FALSE;
  17. }
  18. #endif
  19. return TRUE;
  20. }

lib_dlb_cleanup[edit]

  1. static void
  2. lib_dlb_cleanup()
  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. }

lib_dlb_fopen[edit]

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

lib_dlb_fclose[edit]

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

lib_dlb_fread[edit]

  1. static 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) return 0;
  13.  
  14. pos = dp->start + dp->mark;
  15. if (dp->lib->fmark != pos) {
  16. 	fseek(dp->lib->fdata, pos, SEEK_SET);	/* check for error??? */
  17. 	dp->lib->fmark = pos;
  18. }
  19.  
  20. nread = fread(buf, size, quan, dp->lib->fdata);
  21. nbytes = nread * size;
  22. dp->mark += nbytes;
  23. dp->lib->fmark += nbytes;
  24.  
  25. return nread;
  26. }

lib_dlb_fseek[edit]

  1. static 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:	   curpos = dp->mark + pos;	break;
  11. 	case SEEK_END:	   curpos = dp->size - pos;	break;
  12. 	default: /* set */ curpos = pos;		break;
  13. }
  14. if (curpos < 0) curpos = 0;
  15. if (curpos > dp->size) curpos = dp->size;
  16.  
  17. dp->mark = curpos;
  18. return 0;
  19. }

lib_dlb_fgets[edit]

  1. static 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) return buf;	/* sanity check */
  11.  
  12. /* return NULL on EOF */
  13. if (dp->mark >= dp->size) return (char *) 0;
  14.  
  15. len--;	/* save room for null */
  16. for (i = 0, bp = buf;
  17. 		i < len && dp->mark < dp->size && c != '\n'; i++, bp++) {
  18. 	if (dlb_fread(bp, 1, 1, dp) <= 0) break;	/* EOF or error */
  19. 	c = *bp;
  20. }
  21. *bp = '\0';
  22.  
  23. #if defined(MSDOS) || defined(WIN32)
  24. if ((bp = index(buf, '\r')) != 0) {
  25. 	*bp++ = '\n';
  26. 	*bp = '\0';
  27. }
  28. #endif
  29.  
  30. return buf;
  31. }

lib_dlb_fgetc[edit]

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

lib_dlb_ftell[edit]

  1. static long
  2. lib_dlb_ftell(dp)
  3. dlb *dp;
  4. {
  5. return dp->mark;
  6. }
  1. const dlb_procs_t lib_dlb_procs = {
  2. lib_dlb_init,
  3. lib_dlb_cleanup,
  4. lib_dlb_fopen,
  5. lib_dlb_fclose,
  6. lib_dlb_fread,
  7. lib_dlb_fseek,
  8. lib_dlb_fgets,
  9. lib_dlb_fgetc,
  10. lib_dlb_ftell
  11. };
  12.  
  13. #endif /* DLBLIB */
  14.  
  15. #ifdef DLBRSRC
  16. const dlb_procs_t rsrc_dlb_procs = {
  17. rsrc_dlb_init,
  18. rsrc_dlb_cleanup,
  19. rsrc_dlb_fopen,
  20. rsrc_dlb_fclose,
  21. rsrc_dlb_fread,
  22. rsrc_dlb_fseek,
  23. rsrc_dlb_fgets,
  24. rsrc_dlb_fgetc,
  25. rsrc_dlb_ftell
  26. };
  27. #endif
  28.  
  29. /* Global wrapper functions ------------------------------------------------ */
  30.  
  31. #define do_dlb_init (*dlb_procs->dlb_init_proc)
  32. #define do_dlb_cleanup (*dlb_procs->dlb_cleanup_proc)
  33. #define do_dlb_fopen (*dlb_procs->dlb_fopen_proc)
  34. #define do_dlb_fclose (*dlb_procs->dlb_fclose_proc)
  35. #define do_dlb_fread (*dlb_procs->dlb_fread_proc)
  36. #define do_dlb_fseek (*dlb_procs->dlb_fseek_proc)
  37. #define do_dlb_fgets (*dlb_procs->dlb_fgets_proc)
  38. #define do_dlb_fgetc (*dlb_procs->dlb_fgetc_proc)
  39. #define do_dlb_ftell (*dlb_procs->dlb_ftell_proc)
  40.  
  41. static const dlb_procs_t *dlb_procs;
  42. static boolean dlb_initialized = FALSE;

dlb_init[edit]

  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. }

dlb_cleanup[edit]

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

dlb_fopen[edit]

  1. dlb *
  2. dlb_fopen(name, mode)
  3. const char *name, *mode;
  4. {
  5. FILE *fp;
  6. dlb *dp;
  7.  
  8. if (!dlb_initialized) return (dlb *) 0;
  9.  
  10. dp = (dlb *) alloc(sizeof(dlb));
  11. if (do_dlb_fopen(dp, name, mode))
  12. 	dp->fp = (FILE *) 0;
  13. else if ((fp = fopen_datafile(name, mode, DATAPREFIX)) != 0)
  14. 	dp->fp = fp;
  15. else {
  16. 	/* can't find anything */
  17. 	free((genericptr_t) dp);
  18. 	dp = (dlb *) 0;
  19. 	}
  20.  
  21. return dp;
  22. }

dlb_fclose[edit]

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

dlb_fread[edit]

  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) return 0;
  8. if (dp->fp) return (int) fread(buf, size, quan, dp->fp);
  9. return do_dlb_fread(buf, size, quan, dp);
  10. }

dlb_fseek[edit]

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

dlb_fgets[edit]

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

dlb_fgetc[edit]

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

dlb_ftell[edit]

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