-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdbf.h
More file actions
235 lines (200 loc) · 7.17 KB
/
dbf.h
File metadata and controls
235 lines (200 loc) · 7.17 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef DBF_H
#define DBF_H
// Copyright (C) 2012 Ron Ostafichuk
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <errno.h>
#include <math.h>
#include <ctime>
using namespace std;
typedef unsigned char uint8;
typedef short int uint16;
typedef int uint32;
#define MAX_FIELDS 255
#define DBF_DELETED_RECORD_FLAG '*' // found by reading with hex editor
#define MAX_RECORD_SIZE 0xffff*50 // not idea if this is correct, but good enough for my needs
struct fileHeader
{
uint8 u8FileType;
uint8 u8LastUpdateYear;
uint8 u8LastUpdateMonth;
uint8 u8LastUpdateDay;
uint32 uRecordsInFile;
uint16 uPositionOfFirstRecord;
uint16 uRecordLength; // includes the delete flag (byte) at start of record
uint8 Reserved16[4*4]; // 16 bytes reserved, must always be set to zeros
uint8 uTableFlags;
uint8 uCodePage;
uint8 Reserved2[2]; // 2 bytes reserved, must put zeros in all reserved fields
};
// after the file header, we can have n field definition records, terminated by the byte 0x0D
// must build as a union to make sure bytes are aligned correctly
struct fieldDefinition
{
char cFieldName[11];
char cFieldType;
uint32 uFieldOffset; // location of field from start of record
uint8 uLength; // Length of Field in bytes
uint8 uNumberOfDecimalPlaces;
uint8 FieldFlags;
uint8 uNextAutoIncrementValue[4]; // 32 bit int
uint8 uAutoIncrementStep;
uint8 Reserved8[8]; // should always be zero
};
// terminated by the byte 0x0D then 263 bytes of 0x00
// then the records start
class DBF
{
public:
DBF();
~DBF();
int open(string sFileName,bool bAllowWrite=false); // open an existing dbf file
int close();
int markAsDeleted(int nRecord); // mark this record as deleted
int create(string sFileName,int nNumFields); // create a new dbf file with space for nNumFields
int assignField(fieldDefinition myFieldDef,int nField); // used to assign the field info ONLY if num records in file = 0 !!!
int appendRecord(string *sValues, int nNumValues); // used to append records to the end of the dbf file
int getFieldIndex(string sFieldName);
int loadRec(int nRecord); // load the record into memory
bool isRecordDeleted(); // check if loaded record is deleted
string readField(int nField); // read the request field as a string always from the loaded record!
double readFieldAsDouble(int nField); // read the request field as a double to get higher performance for 'B' type fields only!
void dumpAsCSV(); // output fields and records as csv to std output
int GetNumRecords()
{
return m_FileHeader.uRecordsInFile;
}
int GetNumFields()
{
return m_nNumFields;
}
string GetFieldName(int nField)
{
return string(m_FieldDefinitions[nField].cFieldName);
}
string convertInt(int number)
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
string convertNumber(uint8 *n, int nSize)
{
// convert any size of number (represented by n[] ) into a string
long long nResult = 0;
for( int i = 0 ; i < nSize ; i++ )
{
nResult += (((unsigned long long) n[i]) << (i*8) );
}
stringstream ss; //create a stringstream
ss << nResult; //add number to the stream
return ss.str(); //return a string with the contents of the stream
}
int ConvertStringToInt(string sInteger,int nSize, char *cRecord)
{
// convert the given string into an integer of nSize bytes (2 or 4 or 8 only)
if( nSize == 2 )
{
union {
short int i;
uint8 n[4];
} u;
stringstream ss;
ss << sInteger;
ss >> u.i;
for( int i = 0 ; i < nSize ; i++ )
cRecord[i] = u.n[i];
return 0;
}
else if( nSize == 4 )
{
union {
int i;
uint8 n[4];
} u;
stringstream ss;
ss << sInteger;
ss >> u.i;
for( int i = 0 ; i < nSize ; i++ )
cRecord[i] = u.n[i];
return 0;
}
else if( nSize ==8 )
{
union {
long i;
uint8 n[8];
} u;
stringstream ss;
ss << sInteger;
ss >> u.i;
for( int i = 0 ; i < nSize ; i++ )
cRecord[i] = u.n[i];
return 0;
}
// fail, clear the record
for( int i = 0 ; i < nSize ; i++ )
cRecord[i] = 0;
return 1; // fail
}
int ConvertStringToFloat(string sFloat,int nSize, char *cRecord)
{
// convert the given string into a float or a double
if( nSize == 4 )
{
union {
float f;
uint8 n[4];
} u;
stringstream ss;
ss.precision(8); // ensure string conversion maintains single precision
ss << sFloat;
ss >> u.f;
for( int i = 0 ; i < nSize ; i++ )
cRecord[i] = u.n[i];
return 0;
} else if( nSize == 8 )
{
union {
double d;
uint8 n[8];
} u;
stringstream ss;
ss.precision(17); // ensure string conversion maintains double precision
ss << sFloat;
ss >> u.d;
for( int i = 0 ; i < nSize ; i++ )
cRecord[i] = u.n[i];
return 0;
}
// fail, clear the record
for( int i = 0 ; i < nSize ; i++ )
cRecord[i] = 0;
return 1; // fail
}
private:
FILE * m_pFileHandle;
string m_sFileName;
bool m_bStructSizesOK; // this must be true for engine to work!
bool m_bAllowWrite;
fileHeader m_FileHeader;
fieldDefinition m_FieldDefinitions[MAX_FIELDS]; // allow a max of 255 fields
int m_nNumFields; // number of fields in use
int updateFileHeader();
char *m_pRecord;
};
#endif // DBF_H