home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / hl10osrc.lzh / Lib / Card.cc next >
Text File  |  1994-04-23  |  4KB  |  137 lines

  1. /* -*- Mode: C -*- */
  2. /* Card.cc - Card class implementation
  3.  * Created by Robert Heller on Fri Dec  6 23:52:08 1991
  4.  *
  5.  * ------------------------------------------------------------------
  6.  * Home Libarian by Deepwoods Software
  7.  * Common Class library implementation code
  8.  * ------------------------------------------------------------------
  9.  * Modification History:
  10.  * ------------------------------------------------------------------
  11.  * Contents:
  12.  * ------------------------------------------------------------------
  13.  * 
  14.  * 
  15.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  16.  *        All Rights Reserved
  17.  * 
  18.  */
  19. #include <Card.h>
  20. #include <ctype.h>
  21.  
  22. Card defCard;            // default prototypical card
  23. int Card::numblocks = 0;    // current number of allocated blocks
  24. Card* Card::blocks[maxblocks]; // allocated cards
  25. Card* Card::freelist = NULL;    // linked list of available cards
  26.  
  27. // Helper function to allocate an additional block of 100 Cards
  28. void Card::moreblocks ()
  29. {
  30.     // Have we run out of blocks?
  31.     if (numblocks < maxblocks) {
  32.         // if not...  bump block counter
  33.         int iblock = numblocks++;
  34.         // snarf some memory
  35.         Card* p = blocks[iblock] = (Card*) new char[sizeof(Card)*blocksize];
  36.         // build linked list.  author field doubles as next pointer.
  37.         for (int ic = 1; ic < blocksize; ic++) {
  38.             p->author = (char*) (p+1);
  39.             p++;
  40.         }
  41.         // point last tail at current freelist
  42.         p->author = (char*) freelist;
  43.         // and set freelist head to first Card in fresh block
  44.         freelist = blocks[iblock];
  45.     }
  46. }
  47.             
  48. // overloaded new operator for Cards
  49. void* Card::operator new (long bytes)
  50. {
  51.     // if freelist is empty, get more memory
  52.     if (defCard.freelist == 0) defCard.moreblocks();
  53.     // if freelist is still empty, return null
  54.     if (defCard.freelist == 0) return(0);
  55.     // get pointer to head of list
  56.     Card* newcard = defCard.freelist;
  57.     // set freelist to next card in the list
  58.     defCard.freelist = (Card*) newcard->author;
  59.     // unlink new card from the list
  60.     newcard->author = 0;
  61.     // retun new card
  62.     return(newcard);
  63. }
  64.  
  65. // overloaded delete operator for Cards
  66. void Card::operator delete(void* vptr)
  67. {
  68.     // convert pointer to a Card*
  69.     Card* ptr = (Card*) vptr;
  70.     // if a null pointer, just return
  71.     if (ptr == 0) return;
  72.     // has this card already be freed?
  73.     for (Card* p = defCard.freelist; p != 0; p = (Card*) p->author) {
  74.         if (p == ptr) return;    // if so, don't free it again
  75.     }
  76.     // link onto head of free list
  77.     ptr->author = (char*) defCard.freelist;
  78.     defCard.freelist = ptr;
  79. }
  80.  
  81. static struct {
  82.     CardType t;
  83.     char* n;
  84. } cardtypenames[] = 
  85.     { {Book,"Book"}, {Magazine,"Magazine"}, {CD,"CD"},
  86.       {AudioCassette,"AudioCassette"}, {Album,"Album"}, 
  87.       {LaserDisk,"LaserDisk"}, {VHSVideo,"VHSVideo"}, 
  88.       {BetaVideo,"BetaVideo"}, {EightMM,"EightMM"},
  89.       {EightTrack,"EightTrack"}, {DAT,"DAT"}, {Other,"Other"}
  90.     };
  91. const NumCardTypes = sizeof(cardtypenames) / sizeof(cardtypenames[0]);
  92.  
  93.  
  94. char* TypeName(CardType type)
  95. {
  96.     for (int i = 0; i < NumCardTypes; i++) {
  97.         if (type == cardtypenames[i].t) return(cardtypenames[i].n);
  98.     }
  99.     static char* dummy = "???";
  100.     return dummy;
  101. }
  102.     
  103. static int cfstrcmp(char* a,char* b)
  104. {
  105.     int comp = 0;
  106.     int ca,cb;
  107.     for (comp = 0;comp == 0;a++,b++) {
  108.         ca = *a; cb = *b;
  109.         if (islower(ca)) ca = toupper(ca);
  110.         if (islower(cb)) cb = toupper(cb);
  111.         comp = ca - cb;
  112.         if (ca == 0 || cb == 0) break;
  113.     }
  114.     return (comp);
  115. }
  116.  
  117. CardType NameType(char* name)
  118. {
  119.     for (int i = 0; i < NumCardTypes; i++) {
  120.         if (cfstrcmp(name,cardtypenames[i].n) == 0) 
  121.             return(cardtypenames[i].t);
  122.     }
  123.     return Other;
  124. }
  125.  
  126. Boolean CardTypeNameP(char* name)
  127. {
  128.     for (int i = 0; i < NumCardTypes; i++) {
  129.         if (cfstrcmp(name,cardtypenames[i].n) == 0) 
  130.             return(true);
  131.     }
  132.     return false;
  133. }
  134.  
  135.  
  136.  
  137.