forked from skohlsmith/cdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.c
More file actions
2009 lines (1767 loc) · 49.5 KB
/
Copy pathlibrary.c
File metadata and controls
2009 lines (1767 loc) · 49.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* /lib/library.c
*
* Support for libraries.
*
* Example usage:
*
* inherit "/std/room";
* inherit "/lib/library";
*
* void
* create_room()
* {
* set_short("Library");
* set_long("This is a library.\n");
*
* // Set the directory where book files will be stored
* set_book_directory("/d/Domain/subdir/");
*
* // Make two shelves, one for maps and one for other books
* add_book_shelf(({ "general", "maps" }));
*
* // Initialize the library
* create_library();
*
* // Add a sign with help information
* add_item("sign", library_help());
* add_cmd_item("sign", "read", library_help());
* }
*
* void
* init()
* {
* ::init();
*
* // Add the library commands
* init_library();
* }
*
* N.B. Directories given in configuration must exist for the library to
* function properly. If a shelf is added, a directory with the
* same name as the shelf must exist under the the directory given
* to set_book_directory().
*/
#include <cmdparse.h>
#include <files.h>
#include <language.h>
#include <macros.h>
#include <mail.h>
#include <stdproperties.h>
#define BOOK_TYPE book_types[0]
#define PLURAL_BOOK_TYPE \
(sizeof(plural_book_types) ? \
plural_book_types[0] : LANG_PWORD(book_types[0]))
#define SHELF_TYPE shelf_types[0]
#define PLURAL_SHELF_TYPE \
(sizeof(plural_shelf_types) ? \
plural_shelf_types[0] : LANG_PWORD(shelf_types[0]))
#define BOOK_ID (MASTER + "_library_book")
#define DEFAULT_TEXT_FILE_NAME "book"
#define TITLE_LINE 1
#define SUMMARY_LINE 2
#define AUTHOR_LINE 3
#define TEXT_LINE 4
#define MAX_TITLE_SIZE 20
static int borrow_required;
static string book_dir = "",
appr_dir = "",
removal_dir = "",
book_list = "",
book_list_short = "",
appr_list = "",
appr_list_short = "",
text_file_name = DEFAULT_TEXT_FILE_NAME,
*book_types = ({ "book" }),
*plural_book_types = ({ "books" }),
*shelf_types = ({ "shelf" }),
*plural_shelf_types = ({ "shelves" }),
default_shelf = "";
static mapping book_map;
static mapping appr_map;
static mapping book_shelves = ([]);
public void done_writing(string title, string summary, string input);
public int library_approve_access();
public string get_book_name(string dir);
/*
* Configuration functions
*/
/*
* Function name: set_book_directory
* Description: Set the directory where books will be stored
* Arguments: string dir - the directory name with trailing slash
*/
public void
set_book_directory(string dir)
{
book_dir = dir;
}
/*
* Function name: set_book_approval_directory
* Description: Set the directory where unapproved books will be stored.
* If this is not set, new books will not need approval.
* Arguments: string dir - the directory name with trailing slash.
*/
public void
set_book_approval_directory(string dir)
{
appr_dir = dir;
}
/*
* Function name: set_book_removal_directory
* Description: Set the directory where removed (denied or discarded) books
* are placed. If this is not set, the files will simply be
* deleted.
* Arguments: string dir - the directory name with trailing slash.
*/
public void
set_book_removal_directory(string dir)
{
removal_dir = dir;
}
/*
* Function name: set_text_file_name
* Description: Set the prefix for the filenames of the books.
* Arguments: string name - the filename prefix
*/
public void
set_text_file_name(string name)
{
text_file_name = name;
}
/*
* Function name: set_book_type
* Description: Set the names to be used to identify books in the library.
* The argument to this function is passed directly to
* the set_name() function on each book instance.
* Arguments: mixed types - a string name or array of string names
*/
public void
set_book_type(mixed types)
{
if (stringp(types))
{
book_types = ({ types });
}
else if (pointerp(types))
{
book_types = types;
}
}
/*
* Function name: set_plural_book_type
* Description: Set the names to be used to identify multiple books.
* The argument to this function is passed directory to
* the set_pname function on each book instance.
* Arguments: mixed types - a string name or array of string names
*/
public void
set_plural_book_type(mixed types)
{
if (stringp(types))
{
plural_book_types = ({ types });
}
else if (pointerp(types))
{
plural_book_types = types;
}
}
/*
* Function name: set_borrow_required
* Description: Indicate that that books must be borrowed from
* the library in order to be read.
* Arguments: int i - if true, books must be borrowed
*/
public void
set_borrow_required(int i)
{
borrow_required = i;
}
/*
* Function name: add_book_shelf
* Description: Add book shelves to the library. This allows books to
* be listed according to categories.
* Arguments: mixed shelf - a shelf name or array of shelf names. These
* are used both as names for the shelves when
* players list shelves and also as subdirectories
* (under the directory specified by
* set_book_directory()) in which the book files
* for each shelf will be stored.
*/
public void
add_book_shelf(mixed shelf)
{
int i;
if (!pointerp(shelf))
{
shelf = ({ shelf });
}
if (!strlen(default_shelf))
{
default_shelf = shelf[0];
}
for (i = 0; i < sizeof(shelf); i++)
{
book_shelves[shelf[i]] = 0;
}
}
/*
* Function name: set_default_shelf
* Description: Set the default shelf to be used when none is specified
* by the code or player.
* Arguments: string shelf - the name of the shelf to use (one of the
* shelf names given in add_book_shelf())
*/
public void
set_default_shelf(string shelf)
{
default_shelf = shelf;
}
/*
* Function name: set_shelf_type
* Description: Set the names by which a shelf can be identified in case
* "shelf" is not appropriate.
* Arguments: mixed types - a string name or array of string names
*/
public void
set_shelf_type(mixed types)
{
if (stringp(types))
{
shelf_types = ({ types });
}
else if (pointerp(types))
{
shelf_types = types;
}
}
/*
* Function name: set_plural_shelf_type
* Description: Set the names by which multiple shelves can be identified
* in case "shelves" is not appropriate.
* Arguments: mixed types - a string name or array of string names
*/
public void
set_plural_shelf_type(mixed types)
{
if (stringp(types))
{
plural_shelf_types = ({ types });
}
else if (pointerp(types))
{
plural_shelf_types = types;
}
}
/*
* Hooks for altering default messages
*/
/*
* Function name: library_no_books_hook
* Description: Redefine this to alter the message given when commands
* fail because the library is empty.
* Returns: 1/0
*/
public int
library_no_books_hook()
{
write("There are no books available.\n");
return 1;
}
/*
* Function name: library_no_approval_books_hook
* Description: Redefine this to alter the message given when commands
* fail because there are no books needing approval
* Returns: 1/0
*/
public int
library_no_approval_books_hook()
{
write("There are no books needing approval.\n");
return 1;
}
/*
* Function name: library_list_short_hook
* Description: Redefine this to alter how the short listing of books
* is presented
*/
public void
library_list_short_hook()
{
write(book_list_short);
}
/*
* Function name: library_list_long_hook
* Description: Redefine this to alter how the long listing of books is
* presented
*/
public void
library_list_long_hook()
{
write(book_list);
}
/*
* Function name: library_list_approval_long_hook
* Description: Redefine this to alter how the long listing of books
* needing approval is presented
*/
public void
library_list_approval_long_hook()
{
write(appr_list);
}
/*
* Function name: library_list_approval_short_hook
* Description: Redefine this to alter how the short listing of books
* needing approval is presented
*/
public void
library_list_approval_short_hook()
{
write(appr_list_short);
}
/*
* Function name: library_no_shelves_hook
* Description: Redefine this to alter the message given when a command
* fails because no shelves exist
* Returns: 1/0
*/
public int
library_no_shelves_hook()
{
notify_fail("There are no different " + SHELF_TYPE + ".\n");
return 0;
}
/*
* Function name: library_list_shelves_hook
* Description: Redefine this to alter how the listing of shelves is
* presented
*/
public void
library_list_shelves_hook()
{
write(implode(map(m_indices(book_shelves), capitalize), "\n") + "\n");
}
/*
* Function name: library_list_shelf_long_hook
* Description: Redefine this to alter how the long listing of books
* on a given shelf is presented
* Arguments: string shelf - the name of the shelf
*/
public void
library_list_shelf_long_hook(string shelf)
{
write(book_shelves[shelf][0]);
}
/*
* Function name: library_list_shelf_short_hook
* Description: Redefine this to alter how the short listing of books
* on a given shelf is presented
* Arguments: string shelf - the name of the shelf
*/
public void
library_list_shelf_short_hook(string shelf)
{
write(book_shelves[shelf][1]);
}
/*
* Function name: library_list_syntax_failure_hook
* Description: Redefine this to alter the message given when
* incorrect syntax is used for the "list" command
* Arguments: string str - the arguments to the "list" command
* Returns: 1/0
*/
public int
library_list_syntax_failure_hook(string str)
{
notify_fail("Usage: " + query_verb() +
(strlen(appr_dir) ? " [approval]" : "") + " [titles]\n");
return 0;
}
/*
* Function name: library_borrow_syntax_failure_hook
* Description: Redefine this to alter the message given when
* incorrect syntax is used for the "borrow" or
* "read" command
* Arguments: string str - the arguments to the "borrow" command
* Returns: 1/0
*/
public int
library_borrow_syntax_failure_hook()
{
notify_fail("Usage: " + query_verb() + " <title>\n");
return 0;
}
/*
* Function name: library_borrow_unavailable_approval_book_hook
* Description: Redefine this to alter the message given when a
* user attempts to borrow or read a nonexistant book from
* books needing approval
* Arguments: string title - the title of the book
* Returns: 1/0
*/
public int
library_borrow_unavailable_approval_book_hook(string title)
{
write("There is no such " + BOOK_TYPE + " available for approval.\n");
return 1;
}
/*
* Function name: library_borrow_unavailable_book_hook
* Description: Redefine this to alter the message given when a
* user attempts to borrow or read a nonexistant book
* Arguments: string title - the title of the book
* Returns: 1/0
*/
public int
library_borrow_unavailable_book_hook(string title)
{
write("There is no such " + BOOK_TYPE + " available.\n");
return 1;
}
/*
* Function name: library_borrow_hook
* Description: Redefine this to alter the message given when a
* user borrows or reads a book
* Arguments: object book - the book borrowed
* string title - the title of the book
*/
public void
library_borrow_hook(object book, string title)
{
write("You borrow " + LANG_ADDART(book->short()) + ".\n");
say(QCTNAME(this_player()) + " borrows " + LANG_ADDART(book->short()) +
".\n");
}
/*
* Function name: library_return_syntax_failure_hook
* Description: Redefine this to alter the message given when incorrect
* syntax is used for the "return" command
* Arguments: string str - the arguments to the "return" command
* Returns: 1/0
*/
public int
library_return_syntax_failure_hook(string str)
{
notify_fail(capitalize(query_verb()) + " what?\n");
return 0;
}
/*
* Function name: library_return_hook
* Description: Redefine this to alter the message given when a
* user returns a book
* Arguments: object book - the book being returned
*/
public void
library_return_hook(object book)
{
write("You return the " + book->short() + ".\n");
say(QCTNAME(this_player()) + " returns " + LANG_ADDART(book->short()) +
".\n");
}
/*
* Function name: library_write_abort_hook
* Description: Redefine this to alter the message given when the
* user aborts writing a book
*/
public void
library_write_abort_hook()
{
write("You stop writing.\n");
}
/*
* Function name: library_write_prompt_title_input_hook
* Description: Redefine this to alter the message given to prompt
* the user to input a book title
*/
public void
library_write_prompt_title_input_hook()
{
write("What is the name of the book? (fewer than " + MAX_TITLE_SIZE +
" characters). (~q to quit)\n> ");
}
/*
* Function name: library_write_prompt_summary_input_hook
* Description: Redefine this to alter the message given to prompt
* the user to input a book summary
*/
public void
library_write_prompt_summary_input_hook()
{
write("\nEnter a short summary of the book. (~q to quit)\n> ");
}
/*
* Function name: library_write_failed_hook
* Description: Redefine this to alter the message given when a book
* cannot be saved for some reason.
*/
public void
library_write_failed_hook()
{
write("Failed to write book.\n");
}
/*
* Function name: library_approve_syntax_failure_hook
* Description: Redefine this to alter the message given when incorrect
* syntax is used for the "approve" command
* Arguments: string str - the arguments to the "approve" command
* Returns: 1/0
*/
public int
library_approve_syntax_failure_hook(string str)
{
notify_fail("Usage: " + query_verb() + " <title>\n");
return 0;
}
/*
* Function name: library_approve_unavailable_book_hook
* Description: Redefine this to alter the message given when a
* user attempts to approve a nonexistant book
* Arguments: string title - the book's title
* Returns: 1/0
*/
public int
library_approve_unavailable_book_hook(string title)
{
write("There is no book by that title that needs approval.\n");
return 1;
}
/*
* Function name: library_approve_hook
* Description: Redefine this to alter the message given when a
* user approves a book
* Arguments: string title - the book's title
*/
public void
library_approve_hook(string title)
{
write("Ok.\n");
}
/*
* Function name: library_classify_syntax_failure_hook
* Description: Redefine this to alter the message given when incorrect
* syntax is used for the "classify" command
* Arguments: string str - the arguments to the "classify" command
* Returns: 1/0
*/
public int
library_classify_syntax_failure_hook(string str)
{
notify_fail("Usage: " + query_verb() + " <title>.\n");
return 0;
}
/*
* Function name: library_classify_unavailable_book_hook
* Description: Redefine this to alter the message given when a
* user attempts to classify a nonexistant book
* Arguments: string title - the book's title
* Returns: 1/0
*/
public int
library_classify_unavailable_book_hook(string title)
{
notify_fail("There is no such " + BOOK_TYPE + " to classify.\n");
return 0;
}
/*
* Function name: library_classify_unavailable_shelf_hook
* Description: Redefine this to alter the message given when a
* user attempts to classify a book under a nonexistant
* shelf
* Arguments: string shelf - the shelf name
*/
public void
library_classify_unavailable_shelf_hook(string shelf)
{
write("There is no such " + SHELF_TYPE + ".\n");
}
/*
* Function name: library_classify_hook
* Description: Redefine this to alter the message given when a
* user classifies a book
* Arguments: string title - the book's title
* string shelf - the shelf
*/
public void
library_classify_hook(string title, string shelf)
{
write("Ok.\n");
}
/*
* Function name: library_classify_prompt_shelf_input_hook
* Description: Redefine this to alter the message given to prompt
* the user to input a shelf name
* Arguments: string title - the title of the book being classified
*/
public void
library_classify_prompt_shelf_input_hook(string title)
{
write("Which " + SHELF_TYPE + " do you wish to place " + title +
" in?\n> ");
}
/*
* Function name: library_deny_syntax_failure_hook
* Description: Redefine this to alter the message given when incorrect
* syntax is used for the "deny" command
* Arguments: string str - the arguments to the "deny" command
* Returns: 1/0
*/
public int
library_deny_syntax_failure_hook(string str)
{
notify_fail("Usage: " + query_verb() + " <title>\n");
return 0;
}
/*
* Function name: library_deny_unavailable_book_hook
* Description: Redefine this to alter the message given when a
* user attempts to deny a nonexistant book
* Arguments: string title - the book's title
* Returns: 1/0
*/
public int
library_deny_unavailable_book_hook(string title)
{
write("There is no book by that title that needs approval.\n");
return 1;
}
/*
* Function name: library_deny_hook
* Description: Redefine this to alter the message given when a
* user denies a book
* Arguments: string title - the book's file
*/
public void
library_deny_hook(string title)
{
write("Ok.\n");
}
/*
* Function name: library_discard_syntax_failure_hook
* Description: Redefine this to alter the message given when incorrect
* syntax is used for the "discard" command
* Arguments: string str - the arguments to the "discard" command
* Returns: 1/0
*/
public int
library_discard_syntax_failure_hook(string str)
{
notify_fail("Usage: " + query_verb() + " <title>\n");
return 0;
}
/*
* Function name: library_discard_unavailable_book_hook
* Description: Redefine this to alter the message given when a
* user attempts to discard a nonexistant book
* Arguments: string title - the book's title
* Returns: 1/0
*/
public int
library_discard_unavailable_book_hook(string title)
{
write("There is no " + BOOK_TYPE + " by that title.\n");
return 1;
}
/*
* Function name: library_discard_hook
* Description: Redefine this to alter the message given when a
* user discards a book
* Arguments: string title - the book discarded
*/
public void
library_discard_hook(string title)
{
write("Ok.\n");
}
/*
* Library routines
*/
/*
* Function name: library_remove_book
* Description: Remove a book from the library. If a removal directory
* is specified, the file will be moved there; otherewise,
* it is deleted.
* Arguments: string filename - the filename of the book file
*/
public void
library_remove_book(string filename)
{
setuid();
seteuid(getuid());
if (strlen(removal_dir))
{
rename(filename, get_book_name(removal_dir));
}
else
{
rm(filename);
}
}
/*
* Function name: library_move_book
* Description: Move a book file to another directory.
* Arguments: string book_file - the filename of the book file
* string new_dir - the destination directory
* Returns: 1/0 - move successful/unsuccessful
*/
public int
library_move_book(string book_file, string new_dir)
{
setuid();
seteuid(getuid());
return rename(book_file, get_book_name(new_dir));
}
/*
* Function name: query_book_title
* Description: Given a book file, return the book's title
* Arguments: string file - The filename for the desired book
* Returns: The book's title
*/
public string
query_book_title(string file)
{
string str = read_file(file, TITLE_LINE, 1);
/* remove trailing "\n" */
if (strlen(str) && (str[-1..] == "\n"))
{
str = str[..-2];
}
/* remove extra whitespace and return */
return implode(explode(str, " ") - ({ "" }), " ");
}
/*
* Function name: query_book_author
* Description: Given a book file, return the book's author
* Arguments: string file - the filename for the desired book
* Returns: The book's author
*/
public string
query_book_author(string file)
{
string str = read_file(file, AUTHOR_LINE, 1);
/* remove trailing "\n" */
if (strlen(str) && (str[-1..] == "\n"))
{
str = str[..-2];
}
/* remove extra whitespace and return */
return implode(explode(str, " ") - ({ "" }), " ");
}
/*
* Function name: query_book_summary
* Description: Given a book file, return the book's summary
* Arguments: string file - the filename for the desired book
* Returns: The book's author
*/
public string
query_book_summary(string file)
{
string str = read_file(file, SUMMARY_LINE, 1);
/* remove trailing "\n" */
if (strlen(str) && (str[-1..] == "\n"))
{
str = str[..-2];
}
/* remove extra whitespace and return */
return implode(explode(str, " ") - ({ "" }), " ");
}
/*
* Function name: query_book_text
* Description: Given a book file, return the book's text
* Arguments: string file - the filename for the desired book
* Returns: the book's text
*/
public string
query_book_text(string file)
{
return read_file(file, TEXT_LINE);
}
/*
* Function name: get_books
* Description: Get the full pathnames of all book files in a given directory
* Arguments: string dir - the directory to search
* Returns: An array of pathname strings
*/
public string *
get_books(string dir)
{
string *books;
setuid();
seteuid(getuid());
books = map(get_dir(dir), &operator(+)(dir));
/* Any non-empty file is counted as a book */
books = filter(books, &operator(>)(,0) @ file_size);
return books;
}
/*
* Function name: get_book_info
* Description: For each of a given array of book files, add an element
* to a given mapping. The elements map book title names
* to book filenames.
* Arguments: string *books - An array of book files.
* mapping m - A mapping in which to store the above
* mentioned information.
*/
public void
get_book_info(string *books, mapping m)
{
int i;
for (i = 0; i < sizeof(books); i++)
{
m[lower_case(query_book_title(books[i]))] = books[i];
}
}
/*
* Function name: format_book_list
* Description: Given an array of book files, return a nicely formatted
* string which lists the given books.
* Arguments: string *books - an array of book filename strings
* Returns: A string description of the given books
*/
public string
format_book_list(string *books)
{
int i;
string str = "";
for (i = 0; i < sizeof(books); i++)
{
str += sprintf("%-" + MAX_TITLE_SIZE + "s %-13s %-=40s\n",
query_book_title(books[i]), query_book_author(books[i]),
query_book_summary(books[i]));
}
return str;
}
/*
* Function name: format_book_list_short
* Description: Return a short version of the book listing (See documentation
* for format_book_list())
* Arguments: string *books - an array of book filename strings
* Returns: A string description of the given books
*/
public string
format_book_list_short(string *books)
{
string *titles = map(books, query_book_title);
return sprintf("%-#70.2s\n", implode(titles, "\n"));
}
/*
* Function name: update_books
* Description: Update the in-memory book information when something
* has changed.
*/
public void
update_books()
{
string *books, *shelves, shelf_list, shelf_list_short;
int i;
setuid();
seteuid(getuid());
book_map = ([]);
appr_map = ([]);
book_list = "";
book_list_short = "";
if (strlen(book_dir))
{
if (m_sizeof(book_shelves))
{
shelves = m_indices(book_shelves);
for (i = 0; i < sizeof(shelves); i++)
{
books = get_books(book_dir + shelves[i] + "/");
shelf_list = format_book_list(books);
book_list += shelf_list;
shelf_list_short = format_book_list_short(books);
book_list_short += shelf_list_short;
book_shelves[shelves[i]] = ({ shelf_list, shelf_list_short });
get_book_info(books, book_map);
}
}
else
{
books = get_books(book_dir);
book_list = format_book_list(books);
book_list_short = format_book_list_short(books);
get_book_info(books, book_map);
}
}
if (strlen(appr_dir))
{
books = get_books(appr_dir);
appr_list = format_book_list(books);
appr_list_short = format_book_list_short(books);
get_book_info(books, appr_map);
}
}
/*
* Function name: create_library
* Description: Initialize the library. This should be called when the
* library object is created, AFTER it has been configured.
*/
public void
create_library()
{
update_books();
}