home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / DBAPI.ZIP / DBAPI.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-25  |  4.1 KB  |  145 lines

  1. #ifndef __DBAPI_H__
  2.  
  3. #include <stdio.h>
  4. #include <share.h>
  5. #include <malloc.h>
  6.  
  7. /*
  8. DBase file handling functions.
  9.  
  10. Copyright (C) 1994-1997 Madis Kaal <mast@forex.ee>
  11.  
  12. http://zerblatt.forex.ee/~mast
  13. */
  14. #define byte unsigned char
  15. #define word unsigned short
  16. #define dword unsigned long
  17.  
  18. typedef struct {
  19.     byte year;            /* binary year */
  20.     byte month;            /* month */
  21.     byte day;            /* and day */
  22. } DBFHDATE;
  23.  
  24. typedef struct {
  25.     byte version;        /* 0x03 for normal, 0x83 if with DBT */
  26.     DBFHDATE update;    /* last update date */
  27.     dword nrecords;        /* number of records in file */
  28.     word hdrlen;        /* header structure length */
  29.     word reclen;        /* record length */
  30.     byte res[20];        /* reserved space, after that, field descriptors */
  31. } DBF3HEADER;
  32.  
  33. /*
  34. ** Field descriptor array is terminated with 0x0d. If this marker is
  35. ** found as first name of field descriptor, the next byte is begin
  36. ** of first record in file.
  37. */
  38. typedef struct {
  39.     byte name[11];        /* ASCIIZ field name 10 chars max. */
  40.     byte type;            /* C, N, L, D or M */
  41.     dword faddress;
  42.     byte len;            /* length of field */
  43.     byte deccount;        /* decimal count */
  44.     byte res[14];        /* reserved space */
  45. } DBF3FIELD;
  46.  
  47. typedef struct {
  48.     byte version;        /* 0x02 */
  49.     word nrecords;        /* number of records in file */
  50.     DBFHDATE update;    /* last update date */
  51.     word reclen;        /* record length */
  52. } DBF2HEADER;
  53.  
  54. typedef struct {
  55.     byte name[11];        /* ASCIIZ field name 10 chars max. */
  56.     byte type;            /* C, N or L */
  57.     byte len;            /* length of field */
  58.     byte res[2];        /* reserved space, used by DBase */
  59.     byte deccount;        /* decimal count */
  60. } DBF2FIELD;
  61.  
  62. /*
  63. ** All database records are preceeded with ' ' if field is not
  64. ** deleted and with '*' if field is deleted. This additional character
  65. ** is INCLUDED in record length in file header.
  66. */
  67.  
  68. typedef struct _dbffield {
  69.     struct _dbffield *next;
  70.     DBF3FIELD f;
  71. } DBFFIELD;
  72.  
  73. typedef struct {
  74.     FILE *fp;           /* FILE we used to get this header */
  75.     int written;        /* NZ if file was changed... */
  76.     byte ver;            /* version number from file */
  77.     DBFHDATE update;    /* last update (from file) */
  78.     dword nrecords;        /* number of records */
  79.     word reclen;        /* record length */
  80.     word nfields;        /* number of fields */
  81.     word hdrlen;        /* length of header record */
  82.     DBFFIELD *fields;
  83. } DBFFILE;
  84.  
  85. /*
  86. ** The field contents are dependant of the type and their layout is:
  87. **
  88. ** C  Character   Ascii characters
  89. ** N  Numeric     - . 0 1 2 3 4 5 6 7 8 9
  90. ** L  Logical     ? Y y N n T t F f (? means uninitialized)
  91. ** M  Memo        10 digits representing DBT block number
  92. ** D  Date        8 digits, representing YYYYMMDD
  93. **
  94. ** Data fields are packed into records with no separators nor
  95. ** record terminators.
  96. **
  97. ** A record, that has 0x1a as first character (after 'record deleted' byte)
  98. ** marks end of file (and must be present for full compatibility).
  99. */
  100.  
  101. typedef struct {
  102.     long nextfree;                /* next free block */
  103.     unsigned short unused;
  104.     unsigned short blocksize;    /* memo block size */
  105.     char crap[504];                /* filler up to 512 bytes */
  106. } FPMEMOHEADER;
  107.  
  108. typedef struct {
  109.     long datatype;                /* 1=memo, 0=picture */
  110.     long memosize;                /* size of this memo */
  111.     unsigned char memo[];        /* memo data begins here, can take */
  112. } FPMEMOBLOCK;                    /* several conseq. blocks */
  113.  
  114. typedef struct {
  115.     FILE *fp;
  116.     unsigned short blocksize;
  117. } FPMEMOFILE;
  118.  
  119. #define dbapiwordswap(a) (((unsigned short)a<<8)|((unsigned short)a>>8))
  120. #define dbapilongswap(a) ( (dbapiwordswap(((unsigned long)a<<16)))|(dbapiwordswap(((unsigned long)a>>16))) )
  121.  
  122. #ifdef __cplusplus
  123. extern "C" {
  124. #endif
  125.  
  126. extern DBFFILE *dbfcreate(char *fn,int ver,DBFFIELD *fields);
  127. extern DBFFILE *dbfopen(char *fn);
  128. extern void dbfclose(DBFFILE *dbf);
  129. extern char *dbread(char *buf,dword recno,DBFFILE *dbf);
  130. extern char *dbwrite(char *buf,dword recno,DBFFILE *dbf);
  131. extern DBFFIELD *dbaddfld(DBFFIELD *base,char *name,int type,
  132.     word len,int dec);
  133. extern void dbkillflds(DBFFIELD *fields);
  134.  
  135. extern long fpmemofindblock(FPMEMOFILE *mf,long blockno);
  136. extern FPMEMOFILE *fpmemoopen(char *fn);
  137. extern void fpmemoclose(FPMEMOFILE *mf);
  138.  
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142.  
  143. #define __DBAPI_H__
  144. #endif
  145.