-
Notifications
You must be signed in to change notification settings - Fork 9
/
dbf_rec.c
210 lines (176 loc) · 4.18 KB
/
dbf_rec.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 1993 Brad Eacker,
* (Music, Intuition, Software, and Computers)
* All Rights Reserved
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/flock_compat.h"
#include <stdio.h>
#include <fcntl.h>
#include "dbf.h"
int get_piece(dbhead_t *dbh, long offset, char *cp, int len);
int put_piece(dbhead_t *dbh, long offset, char *cp, int len);
/*
* get a record off the database
*/
char *get_dbf_record(dbhead_t *dbh, long rec_num)
{
long offset;
char *cp;
if (rec_num > dbh->db_records) {
return NULL;
}
cp = (char *)emalloc(dbh->db_rlen);
/* go to the correct spot on the file */
offset = dbh->db_hlen + (rec_num - 1) * dbh->db_rlen;
if (get_piece(dbh, offset, cp, dbh->db_rlen) != dbh->db_rlen) {
efree(cp);
cp = NULL;
}
if (cp)
dbh->db_cur_rec = rec_num;
return cp;
}
int
get_piece(dbhead_t *dbh, long offset, char *cp, int len)
{
/* go to the correct spot on the file */
if ( lseek(dbh->db_fd, offset, 0) < 0 ) {
return -1;
}
/* read the record into the allocated space */
return read(dbh->db_fd, cp, len);
}
/*
* put a record to the database
*/
long put_dbf_record(dbhead_t *dbh, long rec_num, char *cp)
{
long offset;
if (rec_num == 0) {
rec_num = dbh->db_records;
}
if (rec_num > dbh->db_records) {
return 0L;
}
/* go to the correct spot on the file */
offset = dbh->db_hlen + (rec_num - 1) * dbh->db_rlen;
if (put_piece(dbh, offset, cp, dbh->db_rlen) != dbh->db_rlen) {
rec_num = -1;
}
return rec_num;
}
int put_piece(dbhead_t *dbh, long offset, char *cp, int len)
{
/* go to the correct spot on the file */
if ( lseek(dbh->db_fd, offset, 0) < 0 ) {
return -1;
}
/* write the record into the file */
return write(dbh->db_fd, cp, len);
}
int put_dbf_eof_marker(dbhead_t *dbh)
{
char eof = 0x1A;
if (put_piece(dbh, dbh->db_hlen + dbh->db_records * dbh->db_rlen, &eof, sizeof(eof)) != sizeof(eof)) {
php_error_docref(NULL, E_WARNING, "unable to write end-of-file marker");
return -1;
}
return 0;
}
int del_dbf_record(dbhead_t *dbh, long rec_num)
{
int ret = 0;
char *cp;
if (rec_num > dbh->db_records)
return -1;
if ((cp = get_dbf_record(dbh, rec_num))) {
*cp = DELETED_RECORD;
ret = put_dbf_record(dbh, rec_num, cp);
efree(cp);
}
return ret;
}
int pack_dbf(dbhead_t *dbh)
{
long out_off, in_off;
int rec_cnt, new_cnt;
char *cp;
cp = (char *)emalloc(dbh->db_rlen);
in_off = out_off = dbh->db_hlen;
new_cnt = 0;
rec_cnt = dbh->db_records;
while (rec_cnt > 0) {
if (get_piece(dbh, in_off, cp, dbh->db_rlen) != dbh->db_rlen) {
php_error_docref(NULL, E_WARNING, "unable to read from the file");
efree(cp);
return -1;
}
if (*cp != DELETED_RECORD) {
/* write the record into the file */
if (put_piece(dbh, out_off, cp, dbh->db_rlen) != dbh->db_rlen) {
php_error_docref(NULL, E_WARNING, "unable to write to the file");
efree(cp);
return -1;
}
out_off += dbh->db_rlen;
new_cnt++;
}
in_off += dbh->db_rlen;
rec_cnt--;
}
efree(cp);
/* Try to truncate the file to the right size. */
if (ftruncate(dbh->db_fd, out_off) != 0) {
php_error_docref(NULL, E_WARNING, "dbase_pack() couldn't truncate the file to the right size. Some deleted records may still be left in there");
return -1;
}
dbh->db_records = new_cnt;
return 0;
}
/* routine to get a field from a record */
char *get_field_val(char *rp, dbfield_t *fldp, char *cp)
{
int flen = fldp->db_flen;
if ( !cp )
cp = (char *)emalloc(flen + 1);
strncpy(cp, &rp[fldp->db_foffset], flen);
cp[flen] = '\0';
return cp;
}
char *get_binary_field_val(char *rp, dbfield_t *fldp, char *cp)
{
int flen = fldp->db_flen;
if ( !cp )
cp = (char *)emalloc(flen);
memcpy(cp, &rp[fldp->db_foffset], flen);
return cp;
}
void put_field_val(char *rp, dbfield_t *fldp, char *cp)
{
strncpy(&rp[fldp->db_foffset], cp, fldp->db_flen);
}
/* check for record validity */
int is_valid_rec(char *cp)
{
if (cp && (*cp == VALID_RECORD))
return 1;
else
return 0;
}
/* get the next record */
char *dbf_get_next(dbhead_t *dbh)
{
return get_dbf_record(dbh, dbh->db_cur_rec + 1);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/