-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbio.h
293 lines (243 loc) · 9.08 KB
/
xbio.h
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* gSAFE - LIB
general Sql dAtabase FrontEnd
http://hyperprog.com/gsafe/
(C) 2005-2025 Péter Deák (hyper80@gmail.com)
License: Apache 2.0
XBase Import/Export lib
xbio.h
*/
/* This module is not depends from gSAFE
* Can be used separatedly (Just add xbio.h/xbio.cpp to your project)
*
* WARNING: This file's .cpp is depends on XBase library (http://linux.techass.com/projects/xdb/)
* If you don't want to use the XBase support just remove
* the xbio* files from your project, or undefine (remove) the ENABLE_XBASE_SUPPORT macro!
*/
#ifdef ENABLE_XBASE_SUPPORT
#ifndef GSAFE_XBASE_IO_H
#define GSAFE_XBASE_IO_H
#include <QtCore>
/** \defgroup xbaseio xbaseio */
/* @{ */
#define CPCONV_BUFFERLENGTH 256
#define XBASESCHEMA_MAXFIELD 64
class xbXBase;
class xbDbf;
class xbSchema;
enum HXBaseFieldType {
Error,
Numeric,
Char,
Date,
Float,
Logical,
Memo
};
/** The codepage converter abstract class.
* The HXBaseFileHandler class use it to set the coding of the dbf file. */
class HCodepageConvert
{
public:
HCodepageConvert(void);
virtual QString conv_from(char * from) = 0;
virtual void conv_to(QString from,char * buffer,int bufflen) = 0;
virtual ~HCodepageConvert();
};
/** The codpage converter class for IBM 852 codepage. */
class HCodepage852 : public HCodepageConvert
{
public:
HCodepage852(void) {}
virtual QString conv_from(char * from);
virtual void conv_to(QString from,char * buffer,int bufflen);
};
/** The codpage converter class for IBM 850 codepage. */
class HCodepage850 : public HCodepageConvert
{
public:
HCodepage850(void) {}
virtual QString conv_from(char * from);
virtual void conv_to(QString from,char * buffer,int bufflen);
};
/** The XBase interface base class. Makes a interface to XBase lib, make possible to
* create/read/write .DBF files.
* @see HXBaseFileReader
* @see HXBaseFileWriter */
class HXBaseFileHandler : public QObject
{
Q_OBJECT
protected:
HCodepageConvert *c;
char *buffer;
xbXBase *xbase;
xbDbf *dbf;
bool opened;
bool deleteconvertobject;
public:
/** Creates a standard XBase file handler object.
* Use HXBaseFileReader or HXBaseFileWriter instead! */
HXBaseFileHandler(HCodepageConvert *conv,bool deletecobj=false);
/** Destructor */
~HXBaseFileHandler(void);
/** Returns the number of fields */
int fieldCount(void);
/** Returns the name of the specified field */
QString fieldName(int n);
/** Returns the type of the specified field */
HXBaseFieldType fieldType(int n);
/** Returns the total record number of the opened file */
long recordCount(void);
signals:
/** This signal is emitted, when error occured. The "error" string contains the error message. */
void errorSignal(QString error);
};
/** You can read one XBase files with this class.
* \code
HXBaseFileReader reader(new HCodepage852(),true);
connect(&reader,SIGNAL(errorSignal(QString)),this,SLOT(slotError(QString)));
reader.open("DATA.DBF");
r = reader.firstRecord();
while(r == 0)
{
toDo( reader.getFieldStr("STREET") );
r = reader.nextRecord();
}
reader.close();
\endcode
@see HXBaseFileWriter */
class HXBaseFileReader : public HXBaseFileHandler
{
Q_OBJECT
public:
/** Creates a XBase file reader object
* @param conv The codepage converter of the class
* (put a HCodepageConvert descendant object according to the coding of the dbf file)
* @param deletecobj if this parameter true the "conv" object is deleted if not used. */
HXBaseFileReader(HCodepageConvert *conv,bool deletecobj=false);
/** Destructor */
~HXBaseFileReader(void);
/** Opens a XBase file.
* @param name the path and name of the file
* @return 0 if success */
int open(QString name);
/** Close the currently opened file */
int close(void);
/** Jump to the specified record, and makes it current
* @param idx the index of the necessary record
* @return 0 if success */
int toRecord(long idx);
/** Jump to the first record, and makes it current
\code
int r = reader.firstRecord();
while(r == 0)
{
toDo( reader.getFieldStr("FIELD1") );
r = reader.nextRecord();
}
\endcode
* @return 0 if success
* @see nextRecord() */
int firstRecord(void);
/** Jump to the last record, and makes it current
* @return 0 if success
* @see firstRecord() */
int lastRecord(void);
/** Jump to the next record, and makes it current
* @return 0 if success (if we reach the end got 1)
* @see firstRecord()*/
int nextRecord(void);
/** Jump to the next record, and makes it current
* @return 0 if success (if we reach the end got 1)
* @see nextRecord()*/
int prevRecord(void);
/** Returns the index of the current record */
long getCurrentRecordIdx(void);
/** Get a value of a named field from the current record */
QString getFieldStr (QString fname);
/** Get a value of a named field from the current record */
long getFieldDecimal(QString fname);
/** Get a value of a named field from the current record */
double getFieldFloat (QString fname);
/** Get a value of a named field from the current record */
bool getFieldBool (QString fname);
/** Get a value of a named field from the current record */
QDate getFieldDate (QString fname);
QString getCellStr (long rec,QString fname);
long getCellDecimal(long rec,QString fname);
double getCellFloat (long rec,QString fname);
bool getCellBool (long rec,QString fname);
QDate getCellDate (long rec,QString fname);
};
/** You can create and write one XBase files with this class.
* - Define the scheme
* - Create the file
* - Write the records
* - Close the file
*
* \code
HXBaseFileWriter writer(new HCodepage852(),true);
writer.defineField("NAME",Char,20);
writer.defineField("MONEY",Numeric,10);
writer.create("OUTPUT.DBF",3); //Opens the file
writer.setFieldStr("NAME","Big Joe");
writer.setFieldDecimal("MONEY",176);
writer.appendRecord();
writer.setFieldStr("NAME","Alice Newmann");
writer.setFieldDecimal("MONEY",3169);
writer.appendRecord();
writer.close();
\endcode
* @see HXBaseFileReader*/
class HXBaseFileWriter : public HXBaseFileHandler
{
Q_OBJECT
protected:
xbSchema *schema;
int defined_fnum;
void setLastDefinedField(int idx);
public:
/** Creates a XBase file writer object
* @param conv The codepage converter of the class
* (put a HCodepageConvert descendant object according to the coding of the dbf file)
* @param deletecobj if this parameter true the "conv" object is deleted if not used. */
HXBaseFileWriter(HCodepageConvert *conv,bool deletecobj=false);
/** Destructor */
~HXBaseFileWriter(void);
public:
/** Define a field in the schema of XBase file.
* You can only use this function before create() !
* The field specified with this function will be appended to the existing field list.
* @param name the name of the field (limited to 10 char. XBase limit)
* @param type the type of the field. See HXBaseFieldType enum
* @param length the length of the field.
* @param nofdecimals the number of decimals */
void defineField(QString name,HXBaseFieldType type,int length=0,int nofdecimals=0);
/** Creates the XBase file. You have to specify the schema before with the defineField() function.
* After calling this function the dbf is opened. (Do not call the open()!)
* @param name the path and name of the file
* @param version the version of the file (eg: 3 or 4)*/
int create(QString name,int version);
/** Opens a XBase file to write. Do not call create() if you call this func!
* @param name the path and name of the file
* @return 0 if success */
int open(QString name);
/** Closes the currently opened XBase file */
int close(void);
/** Empty the current record buffer. */
int resetRecord(void);
/** Append the current record buffer to the end of the file, and empty the buffer. */
int appendRecord(void);
/** Set a value of a named field in the current record buffer */
int setFieldStr (QString fname,QString str);
/** Set a value of a named field in the current record buffer */
int setFieldDecimal(QString fname,long decimal);
/** Set a value of a named field in the current record buffer */
int setFieldFloat (QString fname,double val);
/** Set a value of a named field in the current record buffer */
int setFieldBool (QString fname,bool logical);
/** Set a value of a named field in the current record buffer */
int setFieldDate (QString fname,QDate date);
};
/* @} */
#endif // GSAFE_XBASE_IO_H
#endif // ENABLE_XBASE_SUPPORT