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

  1. /***********************************************************************
  2.  
  3.                       CSDB Library, Free Evaluation Version 2.0.5 
  4.                                        Release: January 22th 1997 
  5.  
  6.        Header file of the OLAY class.
  7.  
  8.                                            Copyright(c) 1994-1997 
  9.                                                           ComBits 
  10.                                                   The Netherlands 
  11. ***********************************************************************/
  12.  
  13. #ifndef __CSOLAY_H
  14. #define __CSOLAY_H
  15.  
  16. #include "cspage.h"
  17. #include "csoli.h"
  18.  
  19.  
  20. //#ifdef _CP_001
  21.  
  22. #ifdef feof
  23.   // The feof() macro defined for ANSI C, makes it impossible
  24.   // to declare a member function with that name.
  25.   // Because there is also a feof() function defined, we
  26.   // can savely 'undef' it.
  27.   #undef feof
  28. #endif
  29.  
  30. //#endif
  31.  
  32.  
  33. #ifdef _CP_002
  34.   #pragma warning(disable: 4270 4067 4018)
  35. #endif
  36.  
  37.  
  38.  
  39.      struct olpo_os
  40.      {
  41.         U32  pos;
  42.         U32  page;
  43.         S16  offset;
  44.      };              // OLay-POs
  45.  
  46.  
  47.  
  48. class OLAY: public PAGE
  49. {
  50.  
  51.  protected:
  52.  
  53.      typedef struct
  54.      {
  55.         U32  p_page;
  56.         U32  n_page;
  57.         S16  used;      //Number of bytes in use on this page.
  58.      } olhe;            //OLay HEad
  59.  
  60.     typedef struct olpo_os olpo;
  61.  
  62.  
  63.      OLAYI table;
  64.      U32   lr_page;       //Last Referred Page
  65.      int   lr_dirty;      //..............page dirty
  66.      csCHAR *lr_add ;     //..............Address
  67.  
  68.  
  69.  
  70. protected:
  71.  
  72.      olpo current;
  73.      U32  last_page;
  74.      U32  last_pos;
  75.      U32  first_page;
  76.      U16  per_page;
  77.      U32  o_empty;
  78.      U32  tel_empty;
  79.  
  80.  
  81.   protected:
  82.  
  83.  
  84.      void init_vars(void);
  85.      void init_defaults(void) {}
  86.  
  87.      void prev_page_a(olpo &olp); // To the first byte in the previous page
  88.      void next_page_a(olpo &olp);
  89.      void next_page_a(olhe *hp,olpo &olp)
  90.      {
  91.          olp.pos+=left_used(hp,olp.offset);
  92.          olp.page=next_page_num(hp);
  93.          olp.offset=mini_off();
  94.      }
  95.  
  96.      void start_page(olpo &olp)
  97.      {
  98.         olp.pos-=olp.offset-mini_off();
  99.         olp.offset=mini_off();
  100.      }
  101.  
  102. //////////////////////// Page manipulation ///////////////////////////////
  103.  
  104.      olpo insert_page(olpo &olp);
  105.      void unchain_page(olpo &pos);
  106.      void append_page(void);
  107.      U32  new_page(void);
  108.  
  109. //////////////////////// Skip  //////////////////////////////////////////
  110.  
  111.      void skip_plus(olpo &olp,U32  delta);
  112.      void skip_min(olpo &olp,U32  delta);
  113.  
  114.      void skip_t(olpo &pos,S32 delta)
  115.      {
  116.         if(delta>0) skip_plus_t(pos,delta);
  117.         else        skip_min_t(pos,-delta);
  118.      }
  119.  
  120.      void skip_plus_t(olpo &pos,U32 delta)
  121.      {
  122.         if(delta>1500) go_to(pos,pos.pos+delta);
  123.         else           skip_plus(pos,delta);
  124.      }
  125.  
  126.      void skip_min_t(olpo &pos,U32 delta)
  127.      {
  128.         if(delta>1500) go_to(pos,pos.pos-delta);
  129.         else           skip_min(pos,delta);
  130.      }
  131.  
  132.  
  133. //////////////////////// Disk access ////////////////////////////////////
  134.  
  135.  
  136.      csCHAR *load_page_h(U32 page)
  137.      {
  138.         if(page==lr_page) return lr_add;
  139.         lr_dirty=FALSE;
  140.         return lr_add=load_page(lr_page=page);
  141.      }
  142.  
  143.      csCHAR *load_page_hd(U32 page)
  144.      {
  145.         if(page==lr_page)
  146.         {
  147.             if(!lr_dirty) { dirty(lr_add); lr_dirty=TRUE; }
  148.             return lr_add;
  149.         }
  150.         lr_dirty=TRUE;
  151.         return lr_add=load_page_d(lr_page=page);
  152.      }
  153.  
  154.  
  155.      olhe *HP(U32 page)  { return (olhe *)load_page_h(page); }
  156.      olhe *HPd(U32 page) { return (olhe *)load_page_hd(page); }
  157.  
  158. /////////////////////////// DB Header ///////////////////////////////////
  159.      virtual int read_header(void);
  160.      virtual int write_header(void);
  161.  
  162.  
  163.  
  164. /////////////////////////  Modifying page header //////////////////////////////////
  165.  
  166.      void modify_head(U32 page,U32 p_page,U32 n_page)
  167.           {olhe *p=HPd(page); p->p_page=p_page; p->n_page=n_page;}
  168.      void modify_head(U32 page,U32 p_page,U32 n_page,U16 used)
  169.           {olhe *p=HPd(page); p->p_page=p_page; p->n_page=n_page;p->used=used; }
  170.      void modify_p_page(U32 page,U32 p_page) { HPd(page)->p_page=p_page; }
  171.      void modify_n_page(U32 page,U32 n_page) { HPd(page)->n_page=n_page;}
  172.  
  173.  
  174. /////////////////////////  Prev/Next page number //////////////////////////////////
  175.  
  176.      U32  prev_page_num(olpo& olp) { return prev_page_num(olp.page); }
  177.      U32  prev_page_num(U32 page)  { return HP(page)->p_page; }
  178.  
  179.      U32  next_page_num(olpo& olp) { return next_page_num(olp.page); }
  180.      U32  next_page_num(U32  page) { return HP(page)->n_page; }
  181.      U32  next_page_num(olhe *hp)  { return hp->n_page; }
  182.  
  183.  
  184. /////////////////////////  Pointers ///////////////////////////////////////////////
  185.  
  186.      csCHAR *pos_po(olpo &olp)  { return load_page_h(olp.page)+olp.offset; }
  187.      csCHAR *pos_pod(olpo &olp) { return load_page_hd(olp.page)+olp.offset; }
  188.      csCHAR *pos_pod_last(void)
  189.      {
  190.         olhe *h=HPd(last_page);
  191.         return ((csCHAR *)h)+h->used+mini_off();
  192.      }
  193.  
  194. /////////////////////////  Amount of data on a page ///////////////////////////////
  195.  
  196.      U16  is_used(olpo &olp)  { return is_used(olp.page); }
  197.      U16  is_used(U32  page)  { return HP(page)->used; }
  198.  
  199.      void modify_used(olpo &pos,U16 use)         { HPd(pos.page)->used=use; }
  200.      void modify_used(U32 page,U16 use)          { HPd(page)->used=use; }
  201.      void modify_used_delta(olpo &pos,S16 delta) { HPd(pos.page)->used+=delta; }
  202.      void modify_used_delta(U32 page,S16 delta)  { HPd(page)->used+=delta; }
  203.  
  204. // Can become negative!
  205.      S16  left_used(olpo &olp)           { return left_used(HP(olp.page),olp.offset); }
  206.      S16  left_used(olhe *hp,U16 offset) { return hp->used-(offset-mini_off()); }
  207.  
  208.      U16  space_free(olpo &olp) { return max_free()-is_used(olp); }
  209.      U16  space_free(U32 page)  { return max_free()-is_used(page); }
  210.  
  211. /////////////////////////  Locating //////////////////////////////////////////////
  212.  
  213.      void go_to(olpo &pos,U32 new_pos);
  214.      olpo go_to(U32  pos);
  215.  
  216.      void last(olpo &pos)
  217.      {
  218.           pos.page=last_page;
  219.           pos.pos=last_pos;
  220.           pos.offset=mini_off()+is_used(last_page);
  221.      }
  222. /////////////////////////  Auxiliary //////////////////////////////////////////////
  223.  
  224.      U16  mini_off(void)   { return sizeof(olhe); }
  225.      U16  max_free(void)   { return per_page; } //Set during open()
  226.      void ola_copy(olpo &p2,olpo &p1,U16 len);
  227.  
  228.      virtual void set_id(void);
  229.      virtual int check_id(U32 id);
  230.  
  231.  
  232. /////////////////////////  Support for public functions ////////////////////////////
  233.  
  234.      void del_mod_use(olpo &pos,U16 new_used);
  235.      int  insert(olpo &pos,void *p,U32 len);
  236.      U32  read(olpo &pos, void *p,U32 len);
  237.      int  replace(olpo &pos,U32 old_len,void *p_new,U32 new_len);
  238.      U16  stil_fit(olpo &pos) { return page_size()-pos.offset; }
  239.      U16  stil_fit(U16 used ) { return max_free()-used; }
  240.      int  writea(olpo &pos,void *p,U32 len);
  241.      int  write(olpo &pos,void *p,U32 len);
  242.  
  243.      int inserta(olpo &pos,void *p,U32 len)
  244.          {
  245.             if (pos.pos==bottom()) return append(pos,p,len);
  246.             else                   return insert(pos,p,len);
  247.          }
  248.  
  249. /////////////////////////  Table functions ////////////////////////////////////////
  250.  
  251.      int  table_look(U32  pos,olpo& op);
  252.      void table_insert(olpo& op)                { table.insert(op.pos,op.page); }
  253.      void table_insert(U32 pa,U32 po)           { table.insert(po,pa); }
  254.      void table_delete_update(olpo& op,U32 len) { table.upd_delet(op.pos,len); }
  255.      void table_insert_update(olpo& op,U32 len) { table.upd_insert(op.pos,len); }
  256.      void table_unchain_update(olpo &pos)       { table.delet(pos.pos); }
  257.  
  258.  
  259. ////////////////////////////////////////////////////////////////////////
  260. ////////////////////////////////////////////////////////////////////////
  261. ////////////////////// PUBLIC FUNCTIONS ////////////////////////////////
  262. ////////////////////////////////////////////////////////////////////////
  263. ////////////////////////////////////////////////////////////////////////
  264.  
  265.  
  266.   public:
  267.      OLAY(void);
  268.      virtual
  269.     ~OLAY(void);
  270.  
  271. //////////////////////// Compatibility function /////////////////////////////
  272.      U32  numbytes(void)      { return bottom()-1; }
  273.  
  274. //////////////////////// Definition //////////////////////////////////////////
  275. /* Inherited
  276.      void page_size(U16 t)
  277.      void header_page_size(U16  n);
  278. */
  279.      int  define(csCHAR *name,U16 page_length=2048);
  280.  
  281. //////////////////////// Open & Close ////////////////////////////////////////
  282. /* Inherited
  283.      int  already_open(void);
  284. */
  285.      int  open(csCHAR *name,S16 kb_buf=30);
  286.      int  save(void);
  287.      int  close(void);
  288.  
  289. //////////////////////// Read & Write ////////////////////////////////////////
  290.      U32  read(U32  pos,void *p,U32 len)
  291.           { olpo op=go_to(pos); return read(op,p,len); }
  292.  
  293.      int  write(U32  pos,void *p,U32 len)
  294.           { olpo op=go_to(pos); return write(op,p,len); }
  295.  
  296.      int  writea(U32 pos,void *p,U32 len)
  297.           { olpo op=go_to(pos); return writea(op,p,len); }
  298.  
  299. //////////////////////// Append  /////////////////////////////////////////////
  300.      int  append(void *p,U32 amount);
  301.      int  append(olpo &pos,void *p,U32 amount);
  302.  
  303. //////////////////////// Replace /////////////////////////////////////////////
  304.      int  replace(U32 pos,U32 old_len,void *p_new,U32 new_len)
  305.           { olpo op=go_to(pos); return replace(op,old_len,p_new,new_len); }
  306.  
  307. //////////////////////// Delete //////////////////////////////////////////////
  308.  
  309.      S32  delet(U32  pos,S32 len)
  310.           { olpo op=go_to(pos);  return delet(op,len); }
  311.      S32  delet(olpo &pos,S32 len);
  312.  
  313. //////////////////////// Insert //////////////////////////////////////////////
  314.      int  insert(U32  pos,void *p,U32 len)
  315.           { olpo op=go_to(pos); return insert(op,p,len); }
  316.      int  inserta(U32 pos,void *p,U32 len)
  317.           { if (pos==bottom()) return append(p,len);
  318.             else               return insert(pos,p,len);  }
  319.  
  320. //////////////////////// Last position /////////////////////////////////////////
  321.      U32  bottom(void)        { return last_pos; }
  322.  
  323. //////////////////////// Number of bytes in File ///////////////////////////////
  324.      U32  filesize(void)      { return bottom()-1; }
  325.  
  326. //////////////////////// Import & Export //////////////////////////////////////
  327.      int  import_bin(csCHAR *name)  { return import(name,TRUE);  }
  328.      int  import_asc(csCHAR *name)  { return import(name,FALSE); }
  329.      int  import(csCHAR *name,int bin_mode=TRUE);
  330.  
  331.      int  export_bin(csCHAR *name)  { return export(name,TRUE);  }
  332.      int  export_asc(csCHAR *name)  { return export(name,FALSE); }
  333.      int  export(csCHAR *name,int bin_mode=TRUE);
  334.  
  335. //////////////////////// Data in Header Page /////////////////////////////////
  336.   /* Inherited
  337.      int  data_2_header(void * ptr,U16 length);
  338.      int  header_2_data(void * ptr,U16 length);
  339.      U16  max_data_in_header(void);
  340.   */
  341.  
  342. ////////////////////////// Reports /////////////////////////////////////////////
  343.      int  report(csCHAR *name,int sub=1);
  344.      void report(FILE *fp,int sub=1);
  345.  
  346.  
  347. ////////////////////////// Sequential IO ///////////////////////////////////////
  348. // The next functions are intended to mimic the classical
  349. // sequential IO, which its typical notion of a file pointer
  350. // which increases with the amount of data read or written.
  351. // IMPORTANT:
  352. //     All the other functions are complete unaware of this
  353. //     (the file pointer that is) and don't update it.
  354. //     So, every time you have used one of the functions outside
  355. //     this category, you should re-use the fseek to
  356. //     correctly position this (virtual) file pointer.
  357. //
  358.  
  359.     int   fseek(long offset,int origin=SEEK_SET);
  360.     long  ftell(void)  { return current.pos-1; }
  361.     int   feof(void)   { return (current.pos==bottom()); }
  362.     int   fgetc(void);
  363.     int   fputc(int c);
  364.     int   fread( void *buf,int size,int count);
  365.     int   fwrite(void *buf,int size,int count);
  366.     csCHAR *fgets(csCHAR *str,int num);
  367.     int   fputs(csCHAR *str) { return !fwrite(str,strlen(str),1); }
  368.  
  369.  
  370. // The next two functions are our additions to support insertions
  371. // and deletions.
  372. //
  373. // The delete function leaves the file pointer unchanged, which means
  374. // pointing to the same offset as before the delete. Note however, that
  375. // the data 'underneath' the pointer is no longer the same.
  376. // If an attempt is made to delete more data then there is left in
  377. // the file (from the file pointer to the end), all the remaining
  378. // data is deleted and no error is produced.
  379. //
  380. // The insert function increments the file pointer with the amount of
  381. // data inserted.
  382. //
  383. // The insert functions returns zero on failure and nonzero on succes.
  384. // The delete functions returns the amount of data actually deleted.
  385. //
  386.     long  fdelete(long amount);
  387.     int   finsert(void *buf,long amount);
  388.  
  389. ////////////////////////// Miscellaneous ///////////////////////////////////////
  390. /* Inherited
  391.      int  background(void)
  392. */
  393.      int  pack(void);
  394.      int  empty(void);
  395.      void info(void);
  396.  
  397.  
  398.      void fflush(void)           { BUFFER::save(); }
  399.      virtual int class_ID(void)  { return CS_CLID_OLAY; }
  400.  
  401.  
  402. };
  403.  
  404.  
  405.  
  406.  
  407.  
  408. #ifdef _CP_002
  409.   #pragma warning(default: 4270 4067 4018)
  410. #endif
  411.  
  412.  
  413.  
  414.  
  415. #endif
  416.