home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / misc_src / cslib16b / csdbgen / demo.h < prev   
Encoding:
C/C++ Source or Header  |  1995-11-01  |  6.7 KB  |  189 lines

  1. #include "ctype.h" 
  2. #include "csa.h" 
  3. #include "csdb.h" 
  4.  
  5. #define NAME_LENGTH     40 
  6. #define ADDRESS_LENGTH     30 
  7. #define CITY_LENGTH     20 
  8.  
  9. ////////// Indexes to be used with the 'order()' function./////////////
  10. #define UNSORTED     0
  11. #define NAME_INDEX     1 
  12. #define ADDRESS_INDEX     2 
  13. #define CITY_INDEX     3 
  14. #define BIRTHDAY_INDEX     4 
  15.  
  16. typedef struct 
  17. {
  18.    char   _name[NAME_LENGTH+1]; 
  19.    char   _address[ADDRESS_LENGTH+1]; 
  20.    char   _city[CITY_LENGTH+1]; 
  21.    long   __birthday; 
  22.    float  _salary; 
  23. } nac_record;
  24.  
  25. class NAC
  26. {
  27.  protected:
  28.  
  29.    nac_record rec;
  30.    nac_record *recp;
  31.  
  32.    long current;
  33.    int  dirty;
  34.    int  is_open;
  35.    int  iOrder;
  36.  
  37.  
  38.    int  (NAC::*bof_fun)(void);
  39.    int  (NAC::*eof_fun)(void);
  40.    int  (NAC::*skip_fun)(int delta);
  41.    void (NAC::*top_fun)(void);
  42.    void (NAC::*bottom_fun)(void);
  43.    int  (NAC::*search_fun)(void *k);
  44.  
  45.    TBASE  db;
  46.    BTREEa in1;        //Index on field name  
  47.    BTREEa in2;        //Index on field address  
  48.    BTREEa in3;        //Index on field city  
  49.    BTREEl in4;        //Index on field birthday  
  50.  
  51.    DATE  _birthday;
  52.  
  53.    int bof0(void)    { return (current==1); }
  54.    int bof1(void)    { return in1.tBOF(); }
  55.    int bof2(void)    { return in2.tBOF(); }
  56.    int bof3(void)    { return in3.tBOF(); }
  57.    int bof4(void)    { return in4.tBOF(); }
  58.  
  59.    int eof0(void)    { return (current==db.numrec()); }
  60.    int eof1(void)    { return in1.tEOF(); }
  61.    int eof2(void)    { return in2.tEOF(); }
  62.    int eof3(void)    { return in3.tEOF(); }
  63.    int eof4(void)    { return in4.tEOF(); }
  64.  
  65.    void top0(void)    { current=1; }
  66.    void top1(void)    { in1.min_dat(¤t); }
  67.    void top2(void)    { in2.min_dat(¤t); }
  68.    void top3(void)    { in3.min_dat(¤t); }
  69.    void top4(void)    { in4.min_dat(¤t); }
  70.  
  71.    void bottom0(void)    { current=db.numrec(); }
  72.    void bottom1(void)    { in1.max_dat(¤t); }
  73.    void bottom2(void)    { in2.max_dat(¤t); }
  74.    void bottom3(void)    { in3.max_dat(¤t); }
  75.    void bottom4(void)    { in4.max_dat(¤t); }
  76.  
  77.    int  search0(void * )    { return TRUE; } 
  78.    int  search1(void *k)    { return in1.search_dat_ge(k,¤t); }
  79.    int  search2(void *k)    { return in2.search_dat_ge(k,¤t); }
  80.    int  search3(void *k)    { return in3.search_dat_ge(k,¤t); }
  81.    int  search4(void *k)    { return in4.search_dat_ge(k,¤t); }
  82.  
  83.    int  skip0(int delta); 
  84.    int  skip1(int delta)    { return in1.skip_dat(delta,¤t); }
  85.    int  skip2(int delta)    { return in2.skip_dat(delta,¤t); }
  86.    int  skip3(int delta)    { return in3.skip_dat(delta,¤t); }
  87.    int  skip4(int delta)    { return in4.skip_dat(delta,¤t); }
  88.  
  89.    void in1_ins_tok(void *s)    { in1.insert(s,¤t); }
  90.    void in1_del_tok(void *s)    { in1.delet(s,¤t); }
  91.  
  92.    void tokenize(char *s,void(NAC::*fun)(void *));
  93.  
  94.  public:
  95.  
  96.  //////////////////////////////// class constructor ////////////////////////////
  97.    NAC(void); 
  98.  
  99.  //////////////////////////////// class destructor /////////////////////////////
  100.    ~NAC(void)  { close(); }
  101.  
  102.  //////////////////////////////// current record number ////////////////////////
  103.    long curr_rec(void)   { return current; }
  104.  
  105.  //////////////////////////////// define ///////////////////////////////////////
  106.    void define(void);  
  107.  
  108.  //////////////////////////////// open & close ////////////////////////////////
  109.    void open(void);  
  110.    void close(void);  
  111.  
  112.  //////////////////////////////// delete //////////////////////////////////////
  113.    int  is_delet(void)  { return db.is_delet(current); } 
  114.    void undelet(void)   { db.undelet(current); }         
  115.    void delet(void)     { db.delet(current); }           
  116.  
  117.    int  is_delet(long n) { return db.is_delet(n); } 
  118.    void undelet(long n)  { db.undelet(n); }         
  119.    void delet(long n)    { db.delet(n); }           
  120.  
  121.  //////////////////////////////// number of records ///////////////////////////
  122.    long numrec(void)         { return db.numrec(); } 
  123.  
  124.  //////////////////////////////// import/export ///////////////////////////////
  125.    int  import(char *s);
  126.    int  export(char *s);
  127.    int  to_DBASE(char *s);
  128.  
  129.  //////////////////////////////// read/write current record ///////////////////
  130.  
  131.    void write_rec2(void);
  132.    void write_rec(void) { if(dirty) write_rec2(); }
  133.  
  134.    void read_rec(void) 
  135.    {
  136.         recp=(nac_record *)db.locate_rec(current); 
  137.         rec=*recp; 
  138.         _birthday.sem_jul(rec.__birthday); 
  139.    }
  140.  
  141.  //////////////////////////////// reindexing //////////////////////////////////
  142.    void reindex(void); 
  143.  
  144.  //////////////////////////////// append //////////////////////////////////////
  145.    void append(void);        //Indexes are NOT updated.
  146.    void append_blank(void);  //Indexes ARE updated.
  147.  
  148.  //////////////////////////////// data in header //////////////////////////////
  149.    int data_2_header(void *p,U16 size) { return db.data_2_header(p,size); } 
  150.    int header_2_data(void *p,U16 size) { return db.header_2_data(p,size); } 
  151.    U16 max_data_in_header(void)        { return db.max_data_in_header();  } 
  152.  
  153.  //////////////////////////////// pack ////////////////////////////////////////
  154.    void pack(void);
  155.  
  156.  //////////////////////////////// (change) active index ///////////////////////
  157.    void order(int nr);    
  158.    int  order(void)  { return iOrder; } 
  159.  
  160.  //////////////////////////////// testing begin/end ///////////////////////////
  161.    int  tBOF(void)       { return (this->*bof_fun)(); } 
  162.    int  tEOF(void)       { return (this->*eof_fun)(); } 
  163.  
  164.  //////////////////////////////// relocating //////////////////////////////////
  165.    int  skip(int del); 
  166.    void bottom(void)     { write_rec(); (this->*bottom_fun)(); read_rec(); }  
  167.    void top(void)        { write_rec(); (this->*top_fun)(); read_rec(); }  
  168.    void search(void *k)  { write_rec(); if(!(this->*search_fun)(k)) bottom(); read_rec(); }
  169.    void go_to(long n);
  170.  
  171.  /////////////////////////reading fields //////////////////////////////////////
  172.     char * name(void)           { return rec._name; } 
  173.     char * address(void)        { return rec._address; } 
  174.     char * city(void)           { return rec._city; } 
  175.     char * birthday(void)       { return (char *)_birthday; } 
  176.     float  salary(void)         { return rec._salary; } 
  177.  
  178.  /////////////////////////writing fields //////////////////////////////////////
  179.  // Note: when writing strings there are NO checks on the 
  180.  //       length. A string which is longer then the definition 
  181.  //       of the field indicates, will OVERWRITE other data!!     
  182.  //   
  183.     void   name(char *s)        { strcpy(rec._name,s); dirty=TRUE; } 
  184.     void   address(char *s)     { strcpy(rec._address,s); dirty=TRUE; } 
  185.     void   city(char *s)        { strcpy(rec._city,s); dirty=TRUE; } 
  186.     void   birthday(char *s)    { _birthday=s; dirty=TRUE; } 
  187.     void   salary(float f)      { rec._salary=f; dirty=TRUE; } 
  188.  
  189. };