home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13462 < prev    next >
Encoding:
Text File  |  1992-09-10  |  6.3 KB  |  215 lines

  1. Xref: sparky comp.lang.c:13462 comp.lang.c++:13476
  2. Newsgroups: comp.lang.c,comp.lang.c++
  3. Path: sparky!uunet!mnemosyne.cs.du.edu!arrakis!thor
  4. From: thor@arrakis.denver.co.us (Robert B. Hood)
  5. Subject: cBASE - Part 2 of 4
  6. Message-ID: <1992Sep10.134313.8531@arrakis.denver.co.us>
  7. Summary: A dBASE III+ Class in C++
  8. Reply-To: thor@arrakis.denver.co.us
  9. Organization: Bob's Programming Paradise, Lakewood, CO, USA
  10. Date: Thu, 10 Sep 1992 13:43:13 GMT
  11. Lines: 202
  12.  
  13. ------------------------ CBASE.HPP ----------------------------------
  14. // cBASE - A dBASE III+ class for accessing and manipulating .DBF files.
  15. //                Copyright (C) 1992 Bob Hood
  16. //
  17. // Author      : Bob Hood (thor@arrakis.denver.co.us)
  18. // Last Update : 08.01.92
  19. // Header Files: cBASE.HPP
  20. // Comments    : Class is still under construction, so use at your own risk.
  21. //
  22. //               You are free to copy and distribute this code as long as
  23. //               you do not charge a fee for it, and the Copyright notice
  24. //               above remains intact.
  25.  
  26. #ifndef TRUE
  27.     #define TRUE    1
  28.     #define FALSE   0
  29. #endif
  30.  
  31. #define NO_PROBLEM                  0
  32. #define DOS_OPEN_ERROR              -1
  33. #define DOS_WRITE_ERROR             -2
  34. #define DOS_READ_ERROR              -3
  35. #define ALREADY_OPEN_ERROR          -4
  36. #define NO_DB_OPEN_ERROR            -5
  37. #define INVALID_RECORD_ERROR        -6
  38. #define INVALID_FIELD_NAME_ERROR    -7
  39. #define INVALID_FIELD_NUM_ERROR     -8
  40. #define FILE_LOCK_FAILED_ERROR      -9
  41. #define FILE_UNLOCK_FAILED_ERROR    -10
  42. #define RECORD_LOCK_FAILED_ERROR    -11
  43. #define RECORD_UNLOCK_FAILED_ERROR  -12
  44. #define RECORD_NOT_LOCKED_ERROR     -13
  45.  
  46. typedef unsigned int  INT;
  47. typedef unsigned long LONG;
  48. typedef unsigned char BYTE;
  49.  
  50. typedef struct field_rec
  51.     {
  52.         char        field_name[11];
  53.         char        field_type;
  54.         char        dummy[4];
  55.  
  56.         union
  57.         {
  58.             INT         char_len;
  59.             struct
  60.             {
  61.                 char    len;
  62.                 char    dec;
  63.             } num_size;
  64.         } len_info;
  65.  
  66.         char        filler[14];
  67.     } FIELD_REC;
  68.  
  69. typedef struct dbf_head
  70.     {
  71.         char        dbf_id;
  72.         char        last_update[3];
  73.         LONG        last_rec;
  74.         INT         data_offset;
  75.         INT         rec_size;
  76.         char        filler[20];
  77.     } DBF_HEAD;
  78.  
  79. typedef struct index_chain
  80.     {
  81.         struct  index_chain *prev;
  82.         LONG    record;
  83.         union
  84.         {
  85.             char    textKey[100];
  86.             long    longKey;
  87.             float   floatKey;
  88.             int     logicalKey;
  89.         } key;
  90.         struct  index_chain *next;
  91.     } INDEX_CHAIN;
  92.  
  93. typedef struct index_header
  94.     {
  95.         int         index_field;
  96.         INDEX_CHAIN *current_record;
  97.         INDEX_CHAIN *index_chain;
  98.     } INDEX_HEADER;
  99.  
  100. typedef struct relation
  101.     {
  102.         struct  relation *next;
  103.         int     field_num;
  104.         int     into_area;
  105.     } RELATION;
  106.  
  107. typedef struct field_data
  108.     {
  109.         struct  field_data *next;
  110.         int     field_type;
  111.  
  112.         union
  113.         {
  114.             char    boolean;
  115.             char    text[256];
  116.             long    long_number;
  117.             struct
  118.             {
  119.                 int     month;
  120.                 int     day;
  121.                 int     year;
  122.             } date;
  123.             struct
  124.             {
  125.                 float   number;
  126.                 char    format[10];
  127.             } float_number;
  128.         } data;
  129.     } FIELD_DATA;
  130.  
  131. typedef struct area
  132.     {
  133.         char        db_name[80];
  134.         char        alias[40];
  135.         int         database;
  136.         LONG        current_record;
  137.         int         num_fields;
  138.         LONG        field_list_offset;
  139.         LONG        data_records_offset;
  140.         DBF_HEAD    dbf_head;
  141.         FIELD_REC   *field_rec;
  142.         RELATION    *relation_list;
  143.         INDEX_HEADER index;
  144.         void        *record_hold;
  145.         FIELD_DATA  *field_data;
  146.         int         deleted;
  147.     } AREA;
  148.  
  149. class dBASE
  150.     {
  151.         AREA        data;
  152.         int         eof_met;            // boolean
  153.         int         bof_met;            // boolean
  154.         int         file_locked;        // boolean
  155.         int         record_locked;      // boolean
  156.         long        dbSize;
  157.         int         db_err;     // this is used by routines that don't return `int'
  158.         int         net_err;    // hold any network errors there may be...
  159.  
  160.         int         getStructure(void);
  161.         FIELD_DATA  *newDBField(void);
  162.         void        clearDBFields(void);
  163.         int         writeRecord(void);
  164.         int         b2f(void);
  165.         void        *getRecord(void);
  166.         int         f2b(void);
  167.         void        grab(char *from, char *to, char *dest);
  168.         int         lock(int file,long offset,long length,int function);
  169.  
  170. public:
  171.         int         update;
  172.         FIELD_DATA  *field_list;
  173.  
  174.         // constructor/destructor cannot have return types (not even `void')
  175.  
  176.                     dBASE();
  177.                     ~dBASE() { deactivate(); }  // use public function...
  178.  
  179.         int         activate(char *db_name,char *alias);
  180.         int         deactivate(void);
  181.         int         gotoRecord(LONG rec_number,int = FALSE);
  182.         int         goTop(void);
  183.         int         goBottom(void);
  184.         int         getRecordLen(void);
  185.         LONG        currRecord(void);
  186.         LONG        lastRecord(void);
  187.         int         deleteRecord(void);
  188.         int         recallRecord(void);
  189.         void        listInfo(void);
  190.         char        *alias(void);
  191.         int         newRecord(int = FALSE);
  192.         void        printDBFields(void);
  193.         void        skip(LONG = 1L);    // default to one record forward
  194.         int         eof(void);
  195.         int         bof(void);
  196.         int         indexOn(int);
  197.         int         indexOn(char *);
  198.         void        killIndex(void);
  199.         int         fLock(void);
  200.         int         fUnLock(void);
  201.         int         rLock(void);
  202.         int         rUnLock(void);
  203.         int         getDBErr(void);
  204.         int         getNetErr(void);
  205.         FIELD_DATA  *getField(char *);
  206.         FIELD_DATA  *getField(int);
  207.     };
  208.  
  209.  
  210. -------------------------- end CBASE.HPP ------------------------
  211. -- 
  212. Bob Hood    thor@arrakis.denver.co.us     H: 303-980-8392  W: 303-632-2180
  213. ---------------------------------------------------------------------------
  214. When people are free to do as they please, they usually imitate each other.
  215.