home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / MAGAZINE / DDJ9309.ZIP / NETSQL.ZIP / BFILE.H < prev    next >
Text File  |  1993-04-26  |  7KB  |  204 lines

  1. /*
  2.  
  3.       bfile.h
  4.       Btrieve class for Borland C++
  5.       09/06/91
  6.  
  7.       Douglas J. Reilly
  8.       Access Microsystems Inc.
  9.       404 Midstreams Road
  10.       Brick, New Jersey  08724
  11.       (908) 892-2683
  12.       CompuServe 74040,607
  13.  
  14.       Comments?  Questions?  Suggestions?
  15.       Have a paying C/C++ programming job you need done?
  16.       Give me a call.
  17.  
  18.       Released into the public domain.  Do with it as you see fit, but
  19.       if you do anything really neat with it, let me know...
  20.  
  21. 10/15/91  DR  Make len default to 0 in constructor, and then set it from
  22.                  open return.
  23.               Set newmode in open() call to -99 by default, ensures
  24.                  that we realize that no mode was sent.
  25. 11/11/91  DR  Multiple fixes and changes to better handle variable length
  26.                  records.
  27.               Add clone_file() to public interface.
  28. 11/12/91  DR  Add support for just_update parm to put_rec();
  29.  
  30. 08/06/92  DR  Add allows_nulls(key_num)
  31. */
  32.  
  33. #ifndef BFILE_H
  34. #define BFILE_H
  35. #include "btrieve.h"
  36. extern "C" {
  37. int BTRV(int ,char *,char *,int *,char *,int );
  38. }
  39.  
  40. class bfile {
  41.    char pos_blk[128];        // position block
  42.    char fname[64];           // physical file name
  43.    char logical_name[10];    // logical file name (not used yet)
  44.    int  mode;                // open mode
  45.    int  file_flag;           // flag from STAT call.
  46.    int  fixed_len;           // The non-variable length.
  47.    unsigned  rec_len;        // record length, i.e. higher than above for
  48.                              //    variable lenght records.
  49.                              //    This is == to above UNLESS you pass
  50.                              //    a length to constructor.  For variable
  51.                              //    length records, you MUST pass a length
  52.                              //    that is the upper limit you expect.
  53.    unsigned  last_rec_len;   // most recent returned len
  54.    int  key_num;             // current key number
  55.    int  num_keys;            // number of keys in file
  56.    int  status;              // error status
  57.    int  opened;              // opened flag, not really essential because data
  58.                              //   seemed like it was as natural a flag
  59.                              //   as possible, since we don't want to write
  60.                              //   data to null.
  61.    char *data;               // The data, of course...
  62.    char owner[60];           // owner of the file, used for secured files.
  63. public:
  64.   // file name, lenght, owner, and open mode
  65.   // Note that constructor opens file, destructor closes.
  66.    bfile(char far *name,unsigned len=0,char far *towner=0,int newmode=0);
  67.    ~bfile();
  68.   // Close file, free up data pointer above.
  69.    int  close();
  70.   // Open, really SB reopen I guess since only useful after close...
  71.    int  open(int newmode=-99);
  72.   // file name, lenght, owner, and open mode
  73.   // Note that this opens file, destructor closes.
  74.    newfile(char far *name,int len=0,char far *towner=0,int newmode=0);
  75.   // Gets a record.  Uses key 0 unless you set key number (below).
  76.    int  get_rec(char far *keystr,int op=B_GET_EQ);
  77.   // overload ++ operator
  78.    int  operator++()
  79.    {
  80.       char temp[255];
  81.       return(get_rec(temp,B_GET_NEXT));
  82.    }
  83.   // overload -- operator
  84.    int  operator--()
  85.    {
  86.       char temp[255];
  87.       return(get_rec(temp,B_GET_PREV));
  88.    }
  89.   // overload operator++
  90.    int  operator++(int )
  91.    {
  92.       char temp[255];
  93.       return(get_rec(temp,B_GET_NEXT));
  94.    }
  95.   // overload  operator--
  96.    int  operator--(int )
  97.    {
  98.       char temp[255];
  99.       return(get_rec(temp,B_GET_PREV));
  100.    }
  101.   // Self explanatory...
  102.    void set_key_num(int key=0)
  103.         {
  104.           // sanity check...
  105.            if ( key<num_keys )
  106.            {
  107.               key_num=key;
  108.            }
  109.         }
  110.    int  get_key_num() { return key_num; }
  111.    int  get_key_len(int key_num=-1);
  112.   // Get the number of keys allowed.
  113.    int  get_num_keys() { return num_keys; }
  114.   // take newdata and copy it into the data element.
  115.    int  set_data(char far *newdata)
  116.         {
  117.            if ( data!=NULL )
  118.            {
  119.               memcpy(data,newdata,rec_len);
  120.               return(1);
  121.            }
  122.            return(0);
  123.         }
  124.   // return pointer to data.
  125.    char *get_data(int *last_len=0)
  126.         {
  127.            if ( last_len!=0 )
  128.            {
  129.               *last_len=last_rec_len;
  130.            }
  131.            return data;
  132.         }
  133.   // return pointer to COPY data.
  134.    char *dup_data()
  135.         {
  136.            char *datacopy=0;
  137.            if ( opened && data!=0 )
  138.            {
  139.               datacopy=new char[rec_len];
  140.               if ( datacopy!=0 )
  141.               {
  142.                  memset(datacopy,'\0',rec_len);
  143.                  memcpy(datacopy,data,rec_len);
  144.               }
  145.            }
  146.            return datacopy;
  147.         }
  148.   // Insert or update record.  could make seperate functions to force one
  149.   //   or the other.  I prefer this.
  150.   //   SEE BELOW...
  151.    int  put_rec(char far *keystr=0,unsigned tlen=0,int just_update=0);
  152.   // Insert.  made seperate functions to force insert.
  153.    int  insert(char far *keystr=0,unsigned tlen=0);
  154.   // self explanatory...except that if keystr==0, positioning is not done
  155.    int  del_rec(char far *keystr=0);
  156.   // allow user to get status after operation.
  157.    int  get_status(){ return status; }
  158.   // does the specified key number allow NULLS?  Important to know..
  159.    int allows_nulls(int test_key);
  160.   // This is really awful, but allows user to SET status to fool others...
  161.    void set_status(int newstat) { status=newstat; };
  162.   // gets a full FIL_SPEC record (see btrieve.h)
  163.    void get_bstat(struct FIL_SPEC *);
  164.   // returns rec_len private element.
  165.    unsigned  get_len() { return rec_len; }
  166.   // returns fixed_len private element.
  167.    int  get_fixed_len() { return fixed_len; }
  168.   // returns last_rec_len private element.
  169.    unsigned  get_last_len() { return last_rec_len; }
  170.    int  get_pos(char far *buf)
  171.    {
  172.       unsigned tlen=4;
  173.       char temp[256];
  174.       status=BTRV(B_GET_POS,pos_blk,buf,(int *)&tlen,temp,key_num);
  175.       return(status);
  176.    }
  177.    int  unlock()
  178.    {
  179.       unsigned tlen=128;
  180.       char temp[256];
  181.       char buf[256];
  182.       status=BTRV(B_UNLOCK,pos_blk,buf,(int *)&tlen,temp,-2);
  183.       status=BTRV(B_UNLOCK,pos_blk,buf,(int *)&tlen,temp,0);
  184.       return(status);
  185.    }
  186.    int set_pos(char far *buf,char far *keystr=0);
  187.    int get_fname(char far *tstr)
  188.    {
  189.       if ( tstr!=0 )
  190.       {
  191.          strcpy(tstr,fname);
  192.       }
  193.       return(tstr!=0);
  194.    }
  195.    int clone_file(char far *destfname,int overwrite=1);
  196.    int variable_len() { return (file_flag&1); }
  197.    int key_only() { return (file_flag&16); }
  198.    int key_from_data(char far *);
  199.    long num_links(int tkey);
  200.    int get_pos_blk(char far *t) { memcpy(t,pos_blk,128); return(data!=0); }
  201.    int set_pos_blk(char far *t) { memcpy(pos_blk,t,128); return(data!=0); }
  202. };
  203. #endif
  204.