home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / ndx303.zip / NDX.H < prev    next >
C/C++ Source or Header  |  1993-02-13  |  5KB  |  115 lines

  1. /*  NDX.H   . . .   Header file for index package   */
  2.  
  3. #ifndef NDX_H
  4. #define NDX_H
  5.  
  6. #include    <string.h>
  7. #include    <stdio.h>
  8. #include    <dir.h>
  9.  
  10. typedef unsigned        WORD;
  11. typedef unsigned long   DWORD;
  12. typedef unsigned char   BYTE;
  13.  
  14. #define FIELDOFFSET(type, field) ((WORD)&(((type *)0)->field))
  15.  
  16. const unsigned stackdepth = 10;     // Maximum tree height
  17. const unsigned nodesize =  512;     // Bytes in each node
  18. const unsigned maxkey = 100;        // Maximum key length
  19. const unsigned maxendkeys = nodesize - sizeof(DWORD) - sizeof(unsigned);
  20.                                     // Maximum keys data length
  21. const unsigned NCACHE = 10;         // Number of node buffers
  22. enum  types {NODUPS, FIFO, LIFO};   // File access modes
  23.  
  24. typedef int (*CFN)(const char *, const char *, ...);// Comparison function types
  25.  
  26. class Key {                 // Key structure
  27. public:
  28.     DWORD   lson;               // Pointer to left son (seek address in file)
  29.     DWORD   offset;             // Record seek address or number
  30.     char    data[maxkey];       // ASCIZ key
  31.     Key(){}
  32.     Key(const char *, DWORD);
  33. protected:
  34.     friend Node;
  35.     friend Index;
  36.     void    push(Node *);
  37.     Node *  pop(Key *&);
  38.     DWORD     insert(DWORD);
  39.     Key     *prev(Node *);                  // Returns previous Key in the Node
  40.     Key     *next(){return (Key *)((char *)this + length());}
  41.     BYTE    length() {return 2*sizeof(DWORD) + strlen(data) + 1;}
  42.                                             // Length of the Key
  43.     Key     *keycpy(Key *s){return (Key *)(memcpy(this, s, s->length()));}
  44. };
  45.  
  46. class Node {
  47.     friend Key;
  48.     friend Node;
  49.     friend Index;
  50.     DWORD       offset;     // Node offset or free node chain
  51.     unsigned    endkeys;    // First unused data byte in keys
  52.     char        keys[maxendkeys];   // The Key structures
  53.     DWORD   *rson(){return (DWORD *)(keys + endkeys);} // Pointer to right son
  54.     split();                                    // Split node in two
  55.     void    shiftup(Key *, unsigned);           // Make room for key
  56.     void    shiftdn(Key *, unsigned, unsigned); // Delete key
  57. };
  58.  
  59. class Frame {              // State stack frame
  60. friend Key;
  61.     friend Node;
  62.     DWORD   node;               // Offset of node in the index file
  63.     WORD    offset;             // Offset of the Key in that node
  64. };
  65.  
  66. typedef struct{             // Cache entry
  67.     char        dirty;      // Node needs writing
  68.     FILE        *handle;    // File handle
  69.     Node        *node;      // File block content
  70. } CACHE;
  71.  
  72. class Index {               // THE index structure
  73.     friend Key;
  74.     friend Node;
  75.     DWORD   root;               // File offset of the root node
  76.     DWORD   eof;                // File offset of the end of file
  77.     DWORD   freelist;           // File offset of free node list
  78.     BYTE    minor,              // Version number
  79.             major,
  80.             mode;               // File access mode
  81.     unsigned n;                 // File size (# keys, that is)
  82.   // Fields above are stored in the index header
  83.     BYTE    stacktop;           // Current stack top index
  84.     Frame   stack[stackdepth];  // Current state
  85.     Key     ckey;               // Current key structure
  86.     CFN     cmpfn;              // find(), etc., comparison function
  87.     char    filename[MAXPATH];  // Index filename
  88.     FILE    *handle;            // Index file handle
  89.     static  unsigned cache_users;   // Cache user count
  90.     static  CACHE    cache[NCACHE]; // The cache itself
  91.     initcache();
  92.     Node    *newnode();             // Get an empty node (maybe from freelist)
  93.     Node    *getnode(DWORD);        // Get a specific node
  94.     DWORD   _find(const char *, DWORD);   // Inner key search routine
  95.     void    read(DWORD, void *, unsigned);  // Read header or node
  96.     void    write(DWORD, void *, unsigned); // Write ditto
  97.   public:
  98.     static unsigned hdrsize;            // Size of Index data written to disk
  99.     Index(char *, int, CFN = (CFN)strcmp);   // Constructor to create new index
  100.     Index(char *, CFN = (CFN)strcmp);        // Constructor to open old one
  101.     ~Index();                           // Destructor, closes index
  102.     const unsigned count(){return n;}   // Return file size (# records)
  103.     isvalid();                          // Test index validity (1 if valid)
  104.     const Key& key(){return ckey;};     // Return current key
  105.     DWORD   insert(const char *, DWORD);  // Insert new key
  106.     DWORD   remove(const char *, DWORD);  // Remove key
  107.     DWORD   find(const char *);           // Find key
  108.     DWORD   findkeyrec(const char *, DWORD); // Find key, specific record
  109.     DWORD   first(unsigned = 0);        // First (last) key
  110.     DWORD   next();                     // Next key
  111.     DWORD   prev();                     // Previous key
  112. };
  113.  
  114. #endif  NDX_H
  115.