… | |
… | |
534 | /* get_titlelist() - returns pointer to the title list referanced by i */ |
534 | /* get_titlelist() - returns pointer to the title list referanced by i */ |
535 | |
535 | |
536 | static titlelist * |
536 | static titlelist * |
537 | get_titlelist (int i) |
537 | get_titlelist (int i) |
538 | { |
538 | { |
|
|
539 | if (!booklist) |
|
|
540 | booklist = get_empty_booklist (); |
|
|
541 | |
539 | titlelist *tl = booklist; |
542 | titlelist *tl = booklist; |
540 | int number = i; |
543 | int number = i; |
541 | |
544 | |
542 | if (number < 0) |
545 | if (number < 0) |
543 | return tl; |
546 | return tl; |
… | |
… | |
546 | { |
549 | { |
547 | if (!tl->next) |
550 | if (!tl->next) |
548 | tl->next = get_empty_booklist (); |
551 | tl->next = get_empty_booklist (); |
549 | |
552 | |
550 | tl = tl->next; |
553 | tl = tl->next; |
551 | number--; |
554 | --number; |
552 | } |
555 | } |
553 | |
556 | |
554 | return tl; |
557 | return tl; |
555 | } |
558 | } |
556 | |
559 | |
… | |
… | |
621 | * created and updated across multiple runs of the program. |
624 | * created and updated across multiple runs of the program. |
622 | */ |
625 | */ |
623 | static void |
626 | static void |
624 | init_book_archive () |
627 | init_book_archive () |
625 | { |
628 | { |
626 | FILE *fp; |
|
|
627 | int comp, nroftitle = 0; |
629 | int nroftitle = 0; |
628 | char buf[MAX_BUF], fname[MAX_BUF], *cp; |
630 | char fname[MAX_BUF]; |
629 | title *book = NULL; |
|
|
630 | titlelist *bl = get_empty_booklist (); |
631 | titlelist *bl = get_empty_booklist (); |
631 | static int did_init_barch; |
|
|
632 | |
|
|
633 | if (did_init_barch) |
|
|
634 | return; |
|
|
635 | |
|
|
636 | did_init_barch = 1; |
|
|
637 | |
|
|
638 | if (!booklist) |
|
|
639 | booklist = bl; |
|
|
640 | |
632 | |
641 | sprintf (fname, "%s/bookarch", settings.localdir); |
633 | sprintf (fname, "%s/bookarch", settings.localdir); |
642 | LOG (llevDebug, " Reading bookarch from %s...\n", fname); |
634 | LOG (llevDebug, " Reading bookarch from %s...\n", fname); |
643 | |
635 | |
644 | if ((fp = open_and_uncompress (fname, 0, &comp)) != NULL) |
636 | object_thawer thawer (fname); |
|
|
637 | |
|
|
638 | if (!thawer) |
|
|
639 | { |
|
|
640 | LOG (llevDebug, "could not read bookarch file\n"); |
|
|
641 | return; |
645 | { |
642 | } |
|
|
643 | |
|
|
644 | while (thawer.kw) |
|
|
645 | { |
|
|
646 | if (thawer.kw != KW_title) |
|
|
647 | if (!thawer.parse_error ("bookarch file")) |
|
|
648 | break; |
|
|
649 | |
|
|
650 | title *book = get_empty_book (); /* init new book entry */ |
|
|
651 | thawer.get (book->name); |
|
|
652 | |
646 | int value, type = 0; |
653 | int type = -1; |
647 | size_t i; |
|
|
648 | |
654 | |
649 | while (fgets (buf, MAX_BUF, fp) != NULL) |
655 | for (;;) |
650 | { |
656 | { |
651 | if (*buf == '#') |
657 | thawer.next (); |
652 | continue; |
658 | |
653 | if ((cp = strchr (buf, '\n')) != NULL) |
659 | switch (thawer.kw) |
654 | *cp = '\0'; |
|
|
655 | cp = buf; |
|
|
656 | while (*cp == ' ') /* Skip blanks */ |
|
|
657 | cp++; |
|
|
658 | if (!strncmp (cp, "title", 4)) |
|
|
659 | { |
660 | { |
660 | book = get_empty_book (); /* init new book entry */ |
661 | case KW_type: thawer.get (type ); break; |
661 | book->name = strchr (cp, ' ') + 1; |
662 | case KW_authour: thawer.get (book->authour ); break; |
662 | type = -1; |
663 | case KW_arch: thawer.get (book->archname ); break; |
663 | nroftitle++; |
664 | case KW_level: thawer.get (book->level ); break; |
664 | continue; |
665 | case KW_size: thawer.get (book->size ); break; |
665 | } |
666 | case KW_index: thawer.get (book->msg_index); break; |
666 | if (!strncmp (cp, "authour", 4)) |
667 | |
667 | { |
668 | case KW_end: |
668 | book->authour = strchr (cp, ' ') + 1; |
|
|
669 | } |
|
|
670 | if (!strncmp (cp, "arch", 4)) |
|
|
671 | { |
|
|
672 | book->archname = strchr (cp, ' ') + 1; |
|
|
673 | } |
|
|
674 | else if (sscanf (cp, "level %d", &value)) |
|
|
675 | { |
|
|
676 | book->level = (uint16) value; |
|
|
677 | } |
|
|
678 | else if (sscanf (cp, "type %d", &value)) |
|
|
679 | { |
|
|
680 | type = (uint16) value; |
|
|
681 | } |
|
|
682 | else if (sscanf (cp, "size %d", &value)) |
|
|
683 | { |
|
|
684 | book->size = (uint16) value; |
|
|
685 | } |
|
|
686 | else if (sscanf (cp, "index %d", &value)) |
|
|
687 | { |
|
|
688 | book->msg_index = (uint16) value; |
|
|
689 | } |
|
|
690 | else if (!strncmp (cp, "end", 3)) |
|
|
691 | { /* link it */ |
669 | /* link it */ |
692 | bl = get_titlelist (type); |
670 | bl = get_titlelist (type); |
693 | book->next = bl->first_book; |
671 | book->next = bl->first_book; |
694 | bl->first_book = book; |
672 | bl->first_book = book; |
695 | bl->number++; |
673 | bl->number++; |
|
|
674 | ++nroftitle; |
|
|
675 | goto book_done; |
|
|
676 | |
|
|
677 | default: |
|
|
678 | delete book; |
|
|
679 | goto book_done; |
696 | } |
680 | } |
697 | } |
681 | } |
|
|
682 | |
|
|
683 | book_done: |
|
|
684 | thawer.next (); |
|
|
685 | } |
|
|
686 | |
698 | LOG (llevDebug, "book archives(used/avail): \n"); |
687 | LOG (llevDebug, "book archives(used/avail): \n"); |
|
|
688 | int i; |
699 | for (bl = booklist, i = 0; bl != NULL && i < sizeof (max_titles) / sizeof (*max_titles); bl = bl->next, i++) |
689 | for (bl = booklist, i = 0; bl && i < sizeof (max_titles) / sizeof (*max_titles); bl = bl->next, i++) |
700 | { |
|
|
701 | LOG (llevDebug, " (%d/%d)\n", bl->number, max_titles[i]); |
690 | LOG (llevDebug, " (%d/%d)\n", bl->number, max_titles[i]); |
702 | } |
|
|
703 | close_and_delete (fp, comp); |
|
|
704 | } |
|
|
705 | |
691 | |
706 | #ifdef BOOK_MSG_DEBUG |
|
|
707 | LOG (llevDebug, "init_book_archive() got %d titles.\n", nroftitle); |
692 | LOG (llevDebug, "init_book_archive() got %d titles.\n", nroftitle); |
708 | #endif |
|
|
709 | LOG (llevDebug, " done.\n"); |
693 | LOG (llevDebug, " done.\n"); |
710 | } |
694 | } |
711 | |
695 | |
712 | /* init_mon_info() - creates the linked list of pointers to |
696 | /* init_mon_info() - creates the linked list of pointers to |
713 | * monster archetype objects if not called previously |
697 | * monster archetype objects if not called previously |