File: dbf.h

package info (click to toggle)
dbf2mysql 1.14a-5
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 320 kB
  • sloc: ansic: 4,680; makefile: 147
file content (183 lines) | stat: -rw-r--r-- 5,488 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* header-file for dbf.c
   declares routines for reading and writing xBase-files (.dbf), and
   associated structures

   Maarten Boekhold (boekhold@cindy.et.tudelft.nl) 29 oktober 1995
*/

#ifndef _DBF_H
#define _DBF_H

#include <sys/types.h>

/**********************************************************************

		The DBF-part

***********************************************************************/

#define DBF_FILE_MODE	0644

/* byte offsets for date in dbh_date */

#define DBH_DATE_YEAR	0
#define DBH_DATE_MONTH	1
#define DBH_DATE_DAY	2

/* maximum fieldname-length */

#define DBF_NAMELEN	11

/* magic-cookies for the file */

#define DBH_NORMAL	0x03
#define DBH_MEMO	0x83

/* magic-cookies for the fields */

#define DBF_ERROR	-1
#define DBF_VALID	0x20
#define DBF_DELETED	0x2A
#define DBF_EOF         0x1A

/* Types of MEMO files; normal dbase III+,IV:DBT, FoxPro:FPT */

#define DBF_MTYPE_NONE   0
#define DBF_MTYPE_DBT3   1
#define DBF_MTYPE_DBT4   2
#define DBF_MTYPE_DBT5   3
#define DBF_MTYPE_FPT    4

#define DBF_DBT_BLOCK_SIZE 512

/* diskheader */

typedef struct {
	u_char	dbh_dbt;		/* indentification field */
	u_char	dbh_year;		/* last modification-date */
	u_char	dbh_month;
	u_char	dbh_day;
	u_char	dbh_records[4];	/* number of records */
	u_char	dbh_hlen[2];	/* length of this header */
	u_char	dbh_rlen[2];	/* length of a record */
	u_char	dbh_stub[20];	/* misc stuff we don't need */
} dbf_header;

/* disk field-description */

typedef struct {
	char	dbf_name[DBF_NAMELEN];	/* field-name terminated with \0 */
	u_char	dbf_type;		/* field-type */
	u_char	dbf_reserved[4];	/* some reserved stuff */
	u_char	dbf_flen;		/* field-length */
	u_char	dbf_dec;		/* number of decimal positions if
						       type is 'N' */
	u_char	dbf_stub[13];		/* stuff we don't need */
        u_char  dbf_isIndexed;          /* exists in index flag */
} dbf_field;

/* disk data structures for working with memo files */
typedef union {
    struct {
	u_char  next_block[4];
	u_char  reserve1[4];
	u_char  reserve2[8];
	u_char  version;
    } dbiii;
    struct {
	u_char  next_block[4];
	u_char  blocksize[4];
	u_char  filename[8];
	u_char  version;
	u_char  reserve1[3];
	u_char  blocklen[2];
    } dbiv;
    struct {
	u_char  next_block[4];
	u_char  reserve1[2];
	u_char  blocksize[2];
    } fpt;
} dbf_memo_header;

typedef struct {
    u_char	header[4];
    u_char	length[4];
} dbf_memo_block_header;

/* memory field-description */

typedef struct {
    char	db_name[DBF_NAMELEN];	/* field-name terminated with \0 */
    u_char	db_type;		/* field-type */
    u_char	db_flen;		/* field-length */
    u_char	db_dec;			/* number of decimal positions */
    u_char	db_idx;                 /* exists in index flag */
} f_descr;

/* memory dfb-header */

typedef struct {
    int		db_fd;		/* file-descriptor */
    u_long	db_offset;	/* current offset in file */
    u_char	db_memo;	/* memo-file present (and type) */
    u_char	db_year;	/* last update as YYMMDD */
    u_char	db_month;
    u_char	db_day;
    u_long	db_hlen;	/* length of the diskheader, for
				   calculating the offsets */
    u_long	db_records;	/* number of records */
    u_long	db_currec;	/* current record-number starting at 0 */
    u_short	db_rlen;	/* length of the record */
    u_char	db_nfields;	/* number of fields */
    u_char	*db_buff;	/* record-buffer to save malloc()'s */
    f_descr	*db_fields;	/* pointer to an array of field-descriptions */
    char	*db_description;	/* Pointer to string description of table type */
    int		db_memofd;		/* memo file-descriptor */
    u_char	*mb_buffer;	/* memo block buffer to save malloc()'s */    
    u_long      mb_bufsiz;	/* Current size of memo block buffer */
    u_long      mb_blksiz;	/* memo file block size */
    u_long      mb_curr_blk;	/* memo file current block number */
    u_long      mb_next_blk;	/* memo file next used block number */
    u_long      mb_free_blk;	/* memo file next free block number */
} dbhead;

/* structure that contains everything a user wants from a field, including
   the contents (in ASCII). Warning! db_flen may be bigger than the actual
   length of db_name! This is because a field doesn't have to be completely
   filled */
 
typedef struct {
    char	db_name[DBF_NAMELEN];	/* field-name terminated with \0 */
    u_char	db_type;		/* field-type */
    u_char	db_flen;		/* field-length */
    u_char	db_dec;			/* number of decimal positions */
    u_long	db_blen;		/* Length of db_contents Buffer */
    char*	db_contents;		/* contents of the field in ASCII */
} field;

/* prototypes for functions */

extern dbhead*	dbf_open(char *file ,int flags);
extern int	dbf_write_head(dbhead *dbh);
extern int	dbf_put_fields(dbhead *dbh);
extern int	dbf_add_field(dbhead *dbh, char *name, u_char type,
			      u_char length, u_char dec);
extern dbhead * dbf_open_new(char *name, int flags);
extern void	dbf_close(dbhead **dbh);
extern int	dbf_get_record(dbhead *dbh, field *fields, u_long rec);
extern field*	dbf_build_record(dbhead *dbh);
extern void	dbf_free_record(dbhead *dbh, field* fields);
extern int 	dbf_put_record(dbhead *dbh, field *rec, u_long where);

/*********************************************************************

		The endian-part

***********************************************************************/

extern long get_long(u_char *cp);
extern void put_long(u_char *cp, long lval);
extern short get_short(u_char *cp);
extern void put_short(u_char *cp, short lval);

#endif	/* _DBF_H */