home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cslio205.zip / INCLUDE / CSRBASE.H < prev    next >
C/C++ Source or Header  |  1997-01-21  |  7KB  |  257 lines

  1. /***********************************************************************
  2.  
  3.                       CSDB Library, Free Evaluation Version 2.0.5 
  4.                                        Release: January 22th 1997 
  5.  
  6.        Header file of the RBASE class.
  7.        This class maintains a database 100% in RAM.
  8.  
  9.                                            Copyright(c) 1994-1997 
  10.                                                           ComBits 
  11.                                                   The Netherlands 
  12. ***********************************************************************/
  13.  
  14. #ifndef __CSRBASE_H
  15. #define __CSRBASE_H
  16.  
  17. #include <csdir.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include "csbase.h"
  22.  
  23.  
  24. #ifdef CS_DEBUG
  25.    #define CHECK_REC(p) check_rec(p);
  26. #else
  27.    #define CHECK_REC(p)
  28. #endif
  29.  
  30.  
  31.  
  32. #ifdef _CP_001
  33. #pragma warn -sig
  34. #endif
  35.  
  36. #ifdef _MSC_VER
  37. #pragma warning(disable : 4051 4135)
  38. #endif
  39.  
  40.  
  41.  
  42. //////////////////////////////////////////////////////////////////////////
  43.  
  44. class RBASE: public BASE
  45. {
  46.  
  47.  
  48. protected:
  49.  
  50.    U16    lenrec;
  51.    U16    recpage;
  52.    U16    rec_foot;
  53.    U16    len_rec_plus_foot;
  54.    S32    NUMREC;
  55.    S32    lrco;
  56.    S32    wrco;
  57.    S32    rrco;
  58.    int    buff_nr;
  59.    void **buff_in;
  60.    S32    headoff;      // Offset to the first page in file.
  61.    U16    headpagesize;
  62.    U8     exit_or_rc;
  63.    S16    ilock;
  64.    S32    cur_rec;
  65.  
  66. protected:
  67.  
  68.  
  69.    void    init_defaults(void);
  70.    void    init_vars(void);
  71.    virtual int  check_id(U32 id);
  72.    virtual void set_id(void);
  73.  
  74.  
  75.    int  has_been_updated(void) { return ((lrco+wrco)>0); }
  76.  
  77.    void check_rec(S32 l);
  78.  
  79.    S32  r2p(S32 rec)     { return (1+(rec-1)/recpage); }
  80.    U16  oinp(S32 rec)    { return (((U16 )((rec-1)%recpage))*len_rec_plus_foot); }
  81.  
  82.    void exit_on_error(int tf)   { exit_or_rc=tf; }
  83.  
  84.    csCHAR *load_page(S32 page)  { return (csCHAR *)buff_in[page]; }
  85.    int  add_page(void );
  86.  
  87.    S32  pos_infile(S32 pagnr)           { return headoff+pagnr*lenpage; }
  88.    void buffer_2_file(S32 pos,void *buf);
  89.    void file_2_buffer(S32 pos,void *buf);
  90.  
  91.    void header_page_size(size_t n);
  92. // int  open_it(void);
  93. // int  close_it(void);
  94.  
  95.    int  close2(void);
  96.  
  97. ////////////////////////////////////////////////////////////////////////////
  98. //////////////////// Public Functions //////////////////////////////////////
  99. ////////////////////////////////////////////////////////////////////////////
  100.  
  101.  
  102. public:
  103.  
  104.    RBASE(void);
  105.    virtual
  106.   ~RBASE(void);
  107.  
  108.  
  109.  
  110. //////////////////////// Compatibility function ////////////////////////
  111.    void speed_option_121(void)   { smallest_page(); }
  112.  
  113.  
  114.  
  115. ///////////////////////////////////////////////////////////////////////////
  116. // The next functions are normally inherited from the page class,
  117. // but RBASE has to implement them itself.
  118.  
  119.  
  120.  
  121.    ////////////////// Data in Header Page /////////////////////////////////
  122.  
  123.    int  data_2_header(void * ptr,U16 length);
  124.    int  header_2_data(void * ptr,U16 length);
  125.    U16  max_data_in_header(void);
  126.  
  127.  
  128.  
  129. ///////////////////////// Define ///////////////////////////////////////
  130. /* Inherited
  131.    void page_size(U16 length);
  132.    U16  page_size(void) ;
  133.  
  134. */
  135.  
  136.    void smallest_page(void)      { recpage=1; }
  137.    int  define(csCHAR *name,U16  reclen);
  138.  
  139.  
  140. ////////////////////////// Open & Close ///////////////////////////////
  141.    int  open(csCHAR *name,S16 kb=100);
  142.    int  open(void)              { return is_open; }
  143.    int  close();
  144.    int  save(void);
  145.  
  146.  
  147. //////////////////////// Locking ////////////////////////////////////////////
  148.    int  ignore_lock(int TrueOrFalse) { return ilock=TrueOrFalse; }
  149.    int  ignore_lock(void)            { return ilock; }
  150.    int  set_lock(int TrueOrFalse);
  151.    int  is_locked(void);
  152.  
  153. ////////////////////////// Record Length ///////////////////////////////////////
  154.    U16  lengthrec(void)   { return lenrec; }
  155.  
  156.  
  157. //////////////////////// Locate, Read & Writes records  ///////////////////////
  158.    csCHAR *locate_rec_l(S32  rec)   { lrco++; return load_page(r2p(rec))+oinp(rec); }
  159.    csCHAR *locate_rec_ld(S32  rec)  { lrco++; return load_page(r2p(rec))+oinp(rec); }
  160.    csCHAR *locate_rec(S32 rec)      { lrco++; return load_page(r2p(rec))+oinp(rec); }
  161.    csCHAR *locate_rec_d(S32 rec)    { lrco++; return load_page(r2p(rec))+oinp(rec); }
  162.  
  163.    void locate_double(S32 rec1,void *&d1,S32 rec2,void *&d2);
  164.    void locate_double_d(S32 rec1,void *&d1,S32 rec2,void *&d2);
  165.  
  166.    void read_rec(  S32 rec, void *d);
  167.    void write_rec( S32 rec, void *d);
  168.  
  169. //////////////////////// Add Records /////////////////////////////////////////
  170.  
  171.    S32 append_rec(void *data);
  172.    S32 append_rec(void);
  173.  
  174. ////////////////////////// Deleting ////////////////////////////////////////////
  175.  
  176.  
  177.    void set_delet(S32 r,int tf);
  178.    void undelet(void *d)  { *((csCHAR *)d+lengthrec())=0; }
  179.    void delet(void *d)    { *((csCHAR *)d+lengthrec())=1; }
  180.    int  is_delet(void *d) { return *((csCHAR *)d+lengthrec()); }
  181.  
  182.    int  is_delet(S32 l)  { return is_delet(locate_rec(l)); }
  183.    void delet(S32 r)      { set_delet(r,TRUE); }
  184.    void undelet(S32 r )  { set_delet(r,FALSE); }
  185.  
  186. //////////////////////// Number of Records ///////////////////////////////////
  187.  
  188.    S32  numrec(void)      { return NUMREC; }
  189.    void set_numrec(S32 n) { NUMREC=n; set_num_page(r2p(n)); }
  190.    void dec_numrec(void ) { NUMREC--; }
  191.  
  192. ////////////////////////// Current /////////////////////////////////////////////
  193.  
  194.    S32  set_current(S32  rec);
  195.    S32  current(void)    { return cur_rec; }
  196.    S32  skip(S32  delta) { S32  l=current(); return (set_current(l+delta)-l); }
  197.    S32  next(S32  delta) { return skip(delta); }
  198.    S32  prev(S32  delta) { return -skip(-delta); }
  199.  
  200. //////////////////////// Report Writing //////////////////////////////////////
  201.    int  report(csCHAR *name,int sub=1);
  202.    void report(FILE *fipo,int sub);
  203.  
  204.  
  205.  
  206.  
  207. ////////////////////////// Miscellaneous ///////////////////////////////////////
  208. /* Inherited
  209.    int  background(void)
  210. */
  211.  
  212. //   int empty(void);
  213.  
  214.    int  pack(void);
  215. // void zap(void);
  216.    void info(void);
  217.    virtual int class_ID(void)  { return CS_CLID_RBASE; }
  218.  
  219. ///////////////////////// Dummy functions /////////////////////////////////
  220. // Fake functions to ensure compatibility
  221. // with TBASE.
  222.  
  223.    void dirty(void *) {}
  224.    U16  number_buff_max(void)    { return U16_MAX;  }
  225.    U16  number_buff_min(void)    { return 1; }
  226.    void max_with_priority(int ,U16){}
  227.    int  max_with_priority(int )    { return U16_MAX; }
  228.    void number_buff(U16 ,U16 )     {}
  229.    void number_buff_min(U16)       {}
  230.    int  background(void)           { return TRUE; }
  231.  
  232.  
  233.  
  234.  
  235. };
  236.  
  237.  
  238.  
  239.  
  240. ////////////////////////////////////////////////////////////////////////////
  241. ////////////////////////////////////////////////////////////////////////////
  242.  
  243.  
  244.  
  245. #ifdef _CP_001
  246. #pragma warn .sig
  247. #endif
  248.  
  249.  
  250. #ifdef _MSC_VER
  251. #pragma warning(default : 4051 4135)
  252. #endif
  253.  
  254.  
  255.  
  256. #endif
  257.