Source:NetHack 5.0.0/src/dlb.c
Jump to navigation
Jump to search
Below is the full text to dlb.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/dlb.c#line12]], 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.
1. /* NetHack 5.0 dlb.c $NHDT-Date: 1596498157 2020/08/03 23:42:37 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.20 $ */ 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 /* see decl.h */ 13. 14. #ifdef DLB 15. /* 16. * Data librarian. Present a STDIO-like interface to NetHack while 17. * multiplexing on one or more "data libraries". If a file is not found 18. * in a given library, look for it outside the libraries. 19. */ 20. 21. typedef struct dlb_procs { 22. boolean (*dlb_init_proc)(void); 23. void (*dlb_cleanup_proc)(void); 24. boolean (*dlb_fopen_proc)(DLB_P, const char *, const char *); 25. int (*dlb_fclose_proc)(DLB_P); 26. int (*dlb_fread_proc)(char *, int, int, DLB_P); 27. int (*dlb_fseek_proc)(DLB_P, long, int); 28. char *(*dlb_fgets_proc)(char *, int, DLB_P); 29. int (*dlb_fgetc_proc)(DLB_P); 30. long (*dlb_ftell_proc)(DLB_P); 31. } dlb_procs_t; 32. 33. #if defined(VERSION_IN_DLB_FILENAME) 34. char dlbfilename[MAX_DLB_FILENAME]; 35. #endif 36. 37. /* without extern.h via hack.h, these haven't been declared for us */ 38. extern FILE *fopen_datafile(const char *, const char *, int); 39. #define FITSuint(x) FITSuint_((x), __func__, __LINE__) 40. /* implementation will be in either dlb_main.c or the core */ 41. extern unsigned FITSuint_(unsigned long long, const char *, int); 42. 43. 44. #ifdef DLBLIB 45. /* 46. * Library Implementation: 47. * 48. * When initialized, we open all library files and read in their tables 49. * of contents. The library files stay open all the time. When 50. * a open is requested, the libraries' directories are searched. If 51. * successful, we return a descriptor that contains the library, file 52. * size, and current file mark. This descriptor is used for all 53. * successive calls. 54. * 55. * The ability to open more than one library is supported but used 56. * only in the Amiga port (the second library holds the sound files). 57. * For Unix, the idea would be to split the NetHack library 58. * into text and binary parts, where the text version could be shared. 59. */ 60. 61. #define MAX_LIBS 4 62. staticfn library dlb_libs[MAX_LIBS]; 63. 64. staticfn boolean readlibdir(library * lp); 65. staticfn boolean find_file(const char *name, library **lib, long *startp, 66. long *sizep); 67. staticfn boolean lib_dlb_init(void); 68. staticfn void lib_dlb_cleanup(void); 69. staticfn boolean lib_dlb_fopen(dlb *, const char *, const char *); 70. staticfn int lib_dlb_fclose(dlb *); 71. staticfn int lib_dlb_fread(char *, int, int, dlb *); 72. staticfn int lib_dlb_fseek(dlb *, long, int); 73. staticfn char *lib_dlb_fgets(char *, int, dlb *); 74. staticfn int lib_dlb_fgetc(dlb *); 75. staticfn long lib_dlb_ftell(dlb *); 76. 77. /* not static because shared with dlb_main.c */ 78. boolean open_library(const char *lib_name, library *lp); 79. void close_library(library * lp); 80. 81. /* without extern.h via hack.h, these haven't been declared for us */ 82. extern char *eos(char *); 83. 84. /* 85. * Read the directory out of the library. Return 1 if successful, 86. * 0 if it failed. 87. * 88. * NOTE: An improvement of the file structure should be the file 89. * size as part of the directory entry or perhaps in place of the 90. * offset -- the offset can be calculated by a running tally of 91. * the sizes. 92. * 93. * Library file structure: 94. * 95. * HEADER: 96. * %3ld library FORMAT revision (currently rev 1) 97. * %1c space 98. * %8ld # of files in archive (includes 1 for directory) 99. * %1c space 100. * %8ld size of allocation for string space for directory names 101. * %1c space 102. * %8ld library offset - sanity check - lseek target for start of first file 103. * %1c space 104. * %8ld size - sanity check - byte size of complete archive file 105. * 106. * followed by one DIRECTORY entry for each file in the archive, including 107. * the directory itself: 108. * %1c handling information (compression, etc.) Always ' ' in rev 1. 109. * %s file name 110. * %1c space 111. * %8ld offset in archive file of start of this file 112. * %c newline 113. * 114. * followed by the contents of the files 115. */ 116. #define DLB_MIN_VERS 1 /* min library version readable by this code */ 117. #define DLB_MAX_VERS 1 /* max library version readable by this code */ 118. 119. /* 120. * Read the directory from the library file. This will allocate and 121. * fill in our globals. The file pointer is reset back to position 122. * zero. If any part fails, leave nothing that needs to be deallocated. 123. * 124. * Return TRUE on success, FALSE on failure. 125. */ 126. staticfn boolean 127. readlibdir(library *lp) /* library pointer to fill in */ 128. { 129. int i; 130. char *sp; 131. long liboffset, totalsize; 132. 133. if (fscanf(lp->fdata, "%ld %ld %ld %ld %ld\n", &lp->rev, &lp->nentries, 134. &lp->strsize, &liboffset, &totalsize) != 5) 135. return FALSE; 136. if (lp->rev > DLB_MAX_VERS || lp->rev < DLB_MIN_VERS) 137. return FALSE; 138. 139. lp->dir = (libdir *) alloc(FITSuint(lp->nentries * sizeof(libdir))); 140. lp->sspace = (char *) alloc(FITSuint(lp->strsize)); 141. 142. /* read in each directory entry */ 143. for (i = 0, sp = lp->sspace; i < lp->nentries; i++) { 144. lp->dir[i].fname = sp; 145. if (fscanf(lp->fdata, "%c%s %ld\n", &lp->dir[i].handling, sp, 146. &lp->dir[i].foffset) != 3) { 147. free((genericptr_t) lp->dir); 148. free((genericptr_t) lp->sspace); 149. lp->dir = (libdir *) 0; 150. lp->sspace = (char *) 0; 151. return FALSE; 152. } 153. sp = eos(sp) + 1; 154. } 155. 156. /* calculate file sizes using offset information */ 157. for (i = 0; i < lp->nentries; i++) { 158. if (i == lp->nentries - 1) 159. lp->dir[i].fsize = totalsize - lp->dir[i].foffset; 160. else 161. lp->dir[i].fsize = lp->dir[i + 1].foffset - lp->dir[i].foffset; 162. } 163. 164. (void) fseek(lp->fdata, 0L, SEEK_SET); /* reset back to zero */ 165. lp->fmark = 0; 166. 167. return TRUE; 168. } 169. 170. /* 171. * Look for the file in our directory structure. Return 1 if successful, 172. * 0 if not found. Fill in the size and starting position. 173. */ 174. staticfn boolean 175. find_file(const char *name, library **lib, long *startp, long *sizep) 176. { 177. int i, j; 178. library *lp; 179. 180. for (i = 0; i < MAX_LIBS && dlb_libs[i].fdata; i++) { 181. lp = &dlb_libs[i]; 182. for (j = 0; j < lp->nentries; j++) { 183. if (FILENAME_CMP(name, lp->dir[j].fname) == 0) { 184. *lib = lp; 185. *startp = lp->dir[j].foffset; 186. *sizep = lp->dir[j].fsize; 187. return TRUE; 188. } 189. } 190. } 191. *lib = (library *) 0; 192. *startp = *sizep = 0; 193. return FALSE; 194. } 195. 196. /* 197. * Open the library of the given name and fill in the given library 198. * structure. Return TRUE if successful, FALSE otherwise. 199. */ 200. boolean 201. open_library(const char *lib_name, library *lp) 202. { 203. boolean status = FALSE; 204. 205. lp->fdata = fopen_datafile(lib_name, RDBMODE, DATAPREFIX); 206. if (lp->fdata) { 207. if (readlibdir(lp)) { 208. status = TRUE; 209. } else { 210. (void) fclose(lp->fdata); 211. lp->fdata = (FILE *) 0; 212. } 213. } 214. return status; 215. } 216. 217. void 218. close_library(library *lp) 219. { 220. (void) fclose(lp->fdata); 221. free((genericptr_t) lp->dir); 222. free((genericptr_t) lp->sspace); 223. 224. (void) memset((char *) lp, 0, sizeof(library)); 225. } 226. 227. /* 228. * Open the library file once using stdio. Keep it open, but 229. * keep track of the file position. 230. */ 231. staticfn boolean 232. lib_dlb_init(void) 233. { 234. /* zero out array */ 235. (void) memset((char *) &dlb_libs[0], 0, sizeof(dlb_libs)); 236. #ifdef VERSION_IN_DLB_FILENAME 237. build_dlb_filename((const char *) 0); 238. #endif 239. /* To open more than one library, add open library calls here. */ 240. if (!open_library(DLBFILE, &dlb_libs[0])) 241. return FALSE; 242. #ifdef DLBFILE2 243. if (!open_library(DLBFILE2, &dlb_libs[1])) { 244. close_library(&dlb_libs[0]); 245. return FALSE; 246. } 247. #endif 248. return TRUE; 249. } 250. 251. staticfn void 252. lib_dlb_cleanup(void) 253. { 254. int i; 255. 256. /* close the data file(s) */ 257. for (i = 0; i < MAX_LIBS && dlb_libs[i].fdata; i++) 258. close_library(&dlb_libs[i]); 259. } 260. 261. #ifdef VERSION_IN_DLB_FILENAME 262. char * 263. build_dlb_filename(const char *lf) 264. { 265. Sprintf(dlbfilename, "%s%d%d%d", 266. lf ? lf : DLBBASENAME, VERSION_MAJOR, VERSION_MINOR, PATCHLEVEL); 267. return dlbfilename; 268. } 269. #endif 270. 271. /*ARGSUSED*/ 272. staticfn boolean 273. lib_dlb_fopen(dlb *dp, const char *name, const char *mode UNUSED) 274. { 275. long start, size; 276. library *lp; 277. 278. /* look up file in directory */ 279. if (find_file(name, &lp, &start, &size)) { 280. dp->lib = lp; 281. dp->start = start; 282. dp->size = size; 283. dp->mark = 0; 284. return TRUE; 285. } 286. 287. return FALSE; /* failed */ 288. } 289. 290. /*ARGUSED*/ 291. staticfn int 292. lib_dlb_fclose(dlb *dp UNUSED) 293. { 294. /* nothing needs to be done */ 295. return 0; 296. } 297. 298. staticfn int 299. lib_dlb_fread(char *buf, int size, int quan, dlb *dp) 300. { 301. long pos, nread, nbytes; 302. 303. /* make sure we don't read into the next file */ 304. if ((dp->size - dp->mark) < (size * quan)) 305. quan = (int)((dp->size - dp->mark) / size); 306. if (quan == 0) 307. return 0; 308. 309. pos = dp->start + dp->mark; 310. if (dp->lib->fmark != pos) { 311. fseek(dp->lib->fdata, pos, SEEK_SET); /* check for error??? */ 312. dp->lib->fmark = pos; 313. } 314. 315. nread = fread(buf, size, quan, dp->lib->fdata); 316. nbytes = nread * size; 317. dp->mark += nbytes; 318. dp->lib->fmark += nbytes; 319. 320. return (int) nread; 321. } 322. 323. staticfn int 324. lib_dlb_fseek(dlb *dp, long pos, int whence) 325. { 326. long curpos; 327. 328. switch (whence) { 329. case SEEK_CUR: 330. curpos = dp->mark + pos; 331. break; 332. case SEEK_END: 333. curpos = dp->size - pos; 334. break; 335. default: /* set */ 336. curpos = pos; 337. break; 338. } 339. if (curpos < 0) 340. curpos = 0; 341. if (curpos > dp->size) 342. curpos = dp->size; 343. 344. dp->mark = curpos; 345. return 0; 346. } 347. 348. staticfn char * 349. lib_dlb_fgets(char *buf, int len, dlb *dp) 350. { 351. int i; 352. char *bp, c = 0; 353. 354. if (len <= 0) 355. return buf; /* sanity check */ 356. 357. /* return NULL on EOF */ 358. if (dp->mark >= dp->size) 359. return (char *) 0; 360. 361. len--; /* save room for null */ 362. for (i = 0, bp = buf; i < len && dp->mark < dp->size && c != '\n'; 363. i++, bp++) { 364. if (dlb_fread(bp, 1, 1, dp) <= 0) 365. break; /* EOF or error */ 366. c = *bp; 367. } 368. *bp = '\0'; 369. 370. #if defined(MSDOS) || defined(WIN32) 371. if ((bp = strchr(buf, '\r')) != 0) { 372. *bp++ = '\n'; 373. *bp = '\0'; 374. } 375. #endif 376. 377. return buf; 378. } 379. 380. staticfn int 381. lib_dlb_fgetc(dlb *dp) 382. { 383. char c; 384. 385. if (lib_dlb_fread(&c, 1, 1, dp) != 1) 386. return EOF; 387. return (int) c; 388. } 389. 390. staticfn long 391. lib_dlb_ftell(dlb *dp) 392. { 393. return dp->mark; 394. } 395. 396. static const dlb_procs_t lib_dlb_procs = { lib_dlb_init, lib_dlb_cleanup, 397. lib_dlb_fopen, lib_dlb_fclose, 398. lib_dlb_fread, lib_dlb_fseek, 399. lib_dlb_fgets, lib_dlb_fgetc, 400. lib_dlb_ftell }; 401. 402. #endif /* DLBLIB */ 403. 404. #ifdef DLBRSRC 405. static const dlb_procs_t rsrc_dlb_procs = { rsrc_dlb_init, rsrc_dlb_cleanup, 406. rsrc_dlb_fopen, rsrc_dlb_fclose, 407. rsrc_dlb_fread, rsrc_dlb_fseek, 408. rsrc_dlb_fgets, rsrc_dlb_fgetc, 409. rsrc_dlb_ftell }; 410. #endif 411. 412. /* Global wrapper functions ------------------------------------------------ 413. */ 414. 415. #define do_dlb_init (*dlb_procs->dlb_init_proc) 416. #define do_dlb_cleanup (*dlb_procs->dlb_cleanup_proc) 417. #define do_dlb_fopen (*dlb_procs->dlb_fopen_proc) 418. #define do_dlb_fclose (*dlb_procs->dlb_fclose_proc) 419. #define do_dlb_fread (*dlb_procs->dlb_fread_proc) 420. #define do_dlb_fseek (*dlb_procs->dlb_fseek_proc) 421. #define do_dlb_fgets (*dlb_procs->dlb_fgets_proc) 422. #define do_dlb_fgetc (*dlb_procs->dlb_fgetc_proc) 423. #define do_dlb_ftell (*dlb_procs->dlb_ftell_proc) 424. 425. static const dlb_procs_t *dlb_procs; 426. static boolean dlb_initialized = FALSE; 427. 428. boolean 429. dlb_init(void) 430. { 431. if (!dlb_initialized) { 432. #ifdef DLBLIB 433. dlb_procs = &lib_dlb_procs; 434. #endif 435. #ifdef DLBRSRC 436. dlb_procs = &rsrc_dlb_procs; 437. #endif 438. 439. if (dlb_procs) 440. dlb_initialized = do_dlb_init(); 441. } 442. 443. return dlb_initialized; 444. } 445. 446. void 447. dlb_cleanup(void) 448. { 449. if (dlb_initialized) { 450. do_dlb_cleanup(); 451. dlb_initialized = FALSE; 452. } 453. } 454. 455. dlb * 456. dlb_fopen(const char *name, const char *mode) 457. { 458. FILE *fp; 459. dlb *dp; 460. 461. if (!dlb_initialized) 462. return (dlb *) 0; 463. 464. /* only support reading; ignore possible binary flag */ 465. if (!mode || mode[0] != 'r') 466. return (dlb *) 0; 467. 468. dp = (dlb *) alloc(sizeof(dlb)); 469. if (do_dlb_fopen(dp, name, mode)) 470. dp->fp = (FILE *) 0; 471. else if ((fp = fopen_datafile(name, mode, DATAPREFIX)) != 0) 472. dp->fp = fp; 473. else { 474. /* can't find anything */ 475. free((genericptr_t) dp); 476. dp = (dlb *) 0; 477. } 478. 479. return dp; 480. } 481. 482. int 483. dlb_fclose(dlb *dp) 484. { 485. int ret = 0; 486. 487. if (dlb_initialized) { 488. if (dp->fp) 489. ret = fclose(dp->fp); 490. else 491. ret = do_dlb_fclose(dp); 492. 493. free((genericptr_t) dp); 494. } 495. return ret; 496. } 497. 498. int 499. dlb_fread(char *buf, int size, int quan, dlb *dp) 500. { 501. if (!dlb_initialized || size <= 0 || quan <= 0) 502. return 0; 503. if (dp->fp) 504. return (int) fread(buf, size, quan, dp->fp); 505. return do_dlb_fread(buf, size, quan, dp); 506. } 507. 508. int 509. dlb_fseek(dlb *dp, long pos, int whence) 510. { 511. if (!dlb_initialized) 512. return EOF; 513. if (dp->fp) 514. return fseek(dp->fp, pos, whence); 515. return do_dlb_fseek(dp, pos, whence); 516. } 517. 518. char * 519. dlb_fgets(char *buf, int len, dlb *dp) 520. { 521. if (!dlb_initialized) 522. return (char *) 0; 523. if (dp->fp) 524. return fgets(buf, len, dp->fp); 525. return do_dlb_fgets(buf, len, dp); 526. } 527. 528. int 529. dlb_fgetc(dlb *dp) 530. { 531. if (!dlb_initialized) 532. return EOF; 533. if (dp->fp) 534. return fgetc(dp->fp); 535. return do_dlb_fgetc(dp); 536. } 537. 538. long 539. dlb_ftell(dlb *dp) 540. { 541. if (!dlb_initialized) 542. return 0; 543. if (dp->fp) 544. return ftell(dp->fp); 545. return do_dlb_ftell(dp); 546. } 547. 548. #endif /* DLB */ 549. 550. /*dlb.c*/