home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / hl10osrc.zoo / Include / common.h < prev    next >
Text File  |  2009-11-06  |  7KB  |  248 lines

  1. /* -*- Mode: C -*- */
  2. /* common.h - common include file - all other include files (and
  3.  *          programs) include this file
  4.  * Created by Robert Heller on Fri Dec  6 19:42:59 1991
  5.  *
  6.  * ------------------------------------------------------------------
  7.  * Home Libarian by Deepwoods Software
  8.  * Common Header Files
  9.  * ------------------------------------------------------------------
  10.  * Modification History:
  11.  * ------------------------------------------------------------------
  12.  * Contents:
  13.  * ------------------------------------------------------------------
  14.  * 
  15.  * 
  16.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  17.  *        All Rights Reserved
  18.  * 
  19.  */
  20.  
  21. #ifndef _COMMON_
  22. #define _COMMON_
  23. #include <stdlib.h>        // standard C library functions
  24. #include <string.h>
  25. #include <errno.h>
  26. /*
  27.  * O/S dependent stuff:
  28.  *    1) we need file modes (passed as args to open and creat (create under
  29.  *     OSK)
  30.  *    2) we need terminal mode hacking
  31.  *    3) we need signal and trap interception stuff
  32.  */
  33. #ifdef OSK
  34. #include <modes.h>        // file modes
  35. // generic symbols (hide O/S dependent names)
  36. #define ReadMode S_IREAD
  37. #define ReadWriteMode S_IREAD|S_IWRITE
  38. #define ReadFlags S_IREAD
  39. #define ReadWriteFlags S_IREAD|S_IWRITE
  40. extern "C" {
  41. extern char* strerror(int);    // missing from the OSK library...
  42. }
  43. // signal hackery
  44. #include "/dd/defs/signal.h"    // the G++ signal.h is a UNIX style signal.h
  45. // terminal modes
  46. //#include <sgstat.h>        // Get/Set tty modes (OSK)
  47. // special library functions
  48. //typedef int (*icept)(int);
  49. //extern "MWC" {
  50. //    int _ss_opt(int,char*);
  51. //    int _gs_opt(int,char*);
  52. //    int _gs_rdy(int);
  53. //    int intercept(icept);
  54. //    char *strerror(int);
  55. //};
  56. #else
  57. #include <fcntl.h>
  58. #include <sys/stat.h> 
  59. // generic symbols (hide O/S dependent names)
  60. #define ReadMode S_IREAD
  61. #define ReadWriteMode S_IREAD|S_IWRITE
  62. #define ReadFlags O_RDONLY|O_BINARY
  63. #define ReadWriteFlags O_RDWR|O_BINARY
  64. #endif
  65. // Global constants:
  66. // KeySize is how long key strings can be.  This includes the nul byte
  67. const KeySize = 36;
  68. // Tree order
  69. const Order   =  10;
  70. // Key type
  71. typedef char Key[KeySize];
  72. // with an order 10 tree and a 32 byte key string + standard overhead
  73. // (pointers, size info, data record pointers), we will get a tree node
  74. // (page) of just under 1024 bytes.  With a small filler, pages will
  75. // be 1024 bytes in size.  This is a reasonable size for most O/Ss.
  76. const SectorSize = 1024;
  77.  
  78. // Global enumerated types
  79. // Your basic true/false type:
  80. typedef int Boolean;
  81. const Boolean false = 0;
  82. const Boolean true  = 1;
  83. // File directions:
  84. enum Direction { in, out, inout };
  85. // File open status
  86. enum OpenStatus { failure, openold, opennew };
  87. // LastSearchType.  Used by SearchXX and SearchXXAgain.
  88. enum LSType { none, id, title, author, subj };
  89. // open modes
  90. enum OpenMode { ReadOnly = 0x001, ReadWrite = 0x003, ModeMask = 0x0FF,
  91.         Create = 0x100 };
  92. // types of cards
  93. enum CardType { Book = 'B', Magazine = 'M', CD = 'D', AudioCassette = 'C',
  94.         Album = 'A', LaserDisk = 'L', VHSVideo = 'V', BetaVideo = 'S',
  95.         EightMM = '8', EightTrack = 'E', DAT = '4', Other = 'O' };
  96. // Terminal command keys
  97. enum  CommandKeys {
  98.     escCmd    = -1,        // ESC key
  99.     upCmd   = -2,        // Up Arrow (^P)
  100.     downCmd = -3,        // Down Arrow (^N)
  101.     leftCmd = -4,        // Left Arrow (^B)
  102.     rightCmd = -5,        // Right Arrow (^F)
  103.     backCmd = -6,        // BackSpace (^H,RUBOUT)
  104.     deleCmd = -7        // DeleteLine (^U)
  105. };
  106. // Terminal drawing modes
  107. enum Mode {
  108.     defPen     = 0x00000,
  109.     graphPen   = 0x00100,
  110.     revsPen    = 0x00200,
  111.     charMask   = 0x000FF,
  112.     modeMask   = 0x0FF00
  113. };
  114. // Types of errors
  115. enum ErrKind {
  116.     memErr,  termErr, sysErr
  117. };
  118.  
  119. // Basic structure classes
  120.  
  121. // A disk record contains a size and a file offset.
  122. struct DiskRecord {
  123.     long int record_size;
  124.     long record_address;
  125.     DiskRecord (long int size = 0, long addr = 0L) 
  126.            {record_size = size; record_address = addr;}
  127. };
  128.  
  129. // A disk page is simular, except the size is fixed.
  130. // there are some special operator and constructor methods
  131. // used to make things convenient, since these are used like
  132. // pointers
  133. struct DiskPage {
  134.     const long int record_size = SectorSize;
  135.     long  record_address;
  136.     DiskPage (DiskPage& a) {record_address = a.record_address;}
  137.     DiskPage (long addr = 0L) {record_address = addr;}
  138.     inline friend Boolean operator == (DiskPage& a,DiskPage& b)
  139.                 {return(a.record_address == b.record_address);}
  140.     inline friend Boolean operator != (DiskPage& a,DiskPage& b)
  141.                 {return(a.record_address != b.record_address);}
  142.     inline DiskPage& operator = (DiskPage& a)
  143.                 {record_address = a.record_address;return *this;}
  144.  
  145. };
  146.  
  147. // A data item - has a key, a data record "pointer", and a child page
  148. // "pointer".
  149. struct Item {
  150.     Key key;
  151.     DiskRecord data;
  152.     DiskPage right;
  153. };
  154.  
  155. // A page is a tree node.
  156. // It has a size (number of used items), pointers to a child and parent,
  157. // a vector of items, and some filler (used to make sure the page is
  158. // exactly 1024 bytes in size).
  159. struct Page {
  160.     long int size;
  161.     DiskPage left;
  162.     DiskPage parent;
  163.     long int parentidx;
  164.     Item items[Order*2];
  165.     char filler[SectorSize-(sizeof(long int) +
  166.                 (2*sizeof(DiskPage)) +
  167.                 sizeof(long int) +
  168.                 (sizeof(Item)*Order*2))];
  169. };
  170.  
  171. // Page Table Entry
  172. // Has a dirty flag, a pointer to the disk page, and a pointer to
  173. // the core copy of the disk page.
  174. struct PTEntry {
  175.     Boolean isdirty;
  176.     DiskPage diskpage;
  177.     Page     *corepage;
  178. };
  179.  
  180. // HomeBlock - this is a 1024 byte record at the start of the file.
  181. // it contains a 8 byte magic header, pointers to the roots of the
  182. // four trees and a vector of available pre-allocated pages.
  183. const int MaxNumFree = (SectorSize - ( 8 + (4 * sizeof(DiskPage)) + 
  184.                        sizeof(long int))) /
  185.                sizeof(DiskPage);
  186.  
  187. struct HomeBlock {
  188.     const char  * const Magic = "LIBRV000";
  189.     char magic[8];
  190.     DiskPage IdRoot, TitleRoot, AuthorRoot, SubjRoot;
  191.     long int numfree;
  192.     DiskPage freepages[MaxNumFree];
  193. };
  194.  
  195. // Core resident record - it has a size and a chunk of memory (the
  196. // record itself).
  197. // Lots of fun.  I wish C++ had a garbage collector...
  198. struct Record {
  199.     long int size;
  200.     char *buffer;
  201.          Record () {size = 0; buffer = 0;}        // empty buffer
  202.          Record (long int size) {Record::size = size; // preallocated buffer
  203.                      buffer = new char[size];}
  204.          Record (Record& rec)        // record from another record
  205.         {
  206.             size = rec.size;
  207.             if (size > 0) {
  208.                 buffer = new char[size];
  209.                 memcpy(buffer,rec.buffer,size);
  210.             }
  211.         }
  212.         ~Record () {if (size > 0) delete buffer;}    // Oh for a GC!
  213.     Record& operator = (Record& rec)        // copy a record
  214.         {
  215.             if (size < rec.size) {
  216.                 delete buffer;
  217.                 buffer = new char[rec.size];
  218.             } else if (rec.size <= 0 && size > 0)
  219.                 delete buffer;
  220.             size = rec.size;
  221.             if (rec.size > 0)
  222.                 memcpy(buffer,rec.buffer,rec.size);
  223.             return *this;
  224.         }
  225.     void NewBuffer(long int size)        // buffer re-allocator
  226.         {
  227.             if (Record::size > 0) delete buffer;
  228.             Record::size = size;
  229.             if (size > 0) buffer = new char[size];
  230.         }            
  231. };
  232.  
  233. // In core item.  Code outside of vBTree.cc never does actual disk I/O.
  234. // Instead it passes in-core records and the insertion, searching, and
  235. // traversal code reads and writes the data from and to the disk file.
  236. struct CoreItem {
  237.     Key key;
  238.     Record data;
  239. };
  240.  
  241. // Function pointer types
  242. typedef void (*TravFunc)(CoreItem*,int);
  243. typedef void (*ErrFun)(int,const char*);
  244. typedef void (*InterruptFun)(void);
  245.  
  246. #endif
  247.  
  248.