gSAFE  1.3.8
xbio.cpp
1 /*
2  gSAFE - LIB
3  general Sql dAtabase FrontEnd
4 
5  (C) 2011 Peter Deak (hyper80@gmail.com)
6 
7  License: GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
8 
9  XBase Import/Export lib
10  xbio.cpp
11 */
12 
13 /* WARNING: This file is depends on XBase library (http://linux.techass.com/projects/xdb/)
14  * If you don't want to use the XBase support just remove
15  * the xbio* files from your project, or undefine (remove) the ENABLE_XBASE_SUPPORT macro!
16  */
17 
18 #include "xbio.h"
19 
20 #ifdef ENABLE_XBASE_SUPPORT
21 
22 #include <xbase.h>
23 
24 int cp852from128idx_utf16value[] =
25 {199 ,252 ,233 ,226 ,228 ,367 ,263 ,231 ,322 ,235 ,336 ,337 ,238 ,377 ,196 ,262 ,201 ,313 ,314 ,244 ,
26  246 ,317 ,318 ,346 ,347 ,214 ,220 ,356 ,357 ,321 ,215 ,269 ,225 ,237 ,243 ,250 ,260 ,261 ,381 ,382 ,
27  280 ,281 ,172 ,378 ,268 ,351 ,171 ,187 ,9617,9618,9619,9474,9508,193 ,194 ,282 ,350 ,9571,9553,9559,
28  9565,379 ,380 ,9488,9492,9524,9516,9500,9472,9532,258 ,259 ,9562,9556,9577,9574,9568,9552,9580,164 ,
29  273 ,272 ,270 ,203 ,271 ,327 ,205 ,206 ,283 ,9496,9484,9608,9604,354 ,366 ,9600,211 ,223 ,212 ,323 ,
30  324 ,328 ,352 ,353 ,340 ,218 ,341 ,368 ,253 ,221 ,355 ,180 ,173 ,733 ,731 ,711 ,728 ,167 ,247 ,184 ,
31  176 ,168 ,729 ,369 ,344 ,345 ,9632,160 };
32 
33 int cp850from128idx_utf16value[] =
34 {199 ,252 ,233 ,226 ,228 ,224 ,229 ,231 ,234 ,235 ,232 ,239 ,238 ,236 ,196 ,197 ,201 ,230 ,198 ,244 ,
35  246 ,242 ,251 ,249 ,255 ,214 ,220 ,248 ,163 ,216 ,215 ,402 ,225 ,237 ,243 ,250 ,241 ,209 ,170 ,186 ,
36  191 ,174 ,172 ,189 ,188 ,161 ,171 ,187 ,9617,9618,9619,9474,9508,193 ,194 ,192 ,169 ,9571,9553,9559,
37  9565,162 ,165 ,9488,9492,9524,9516,9500,9472,9532,227 ,195 ,9562,9556,9577,9574,9568,9552,9580,164 ,
38  240 ,208 ,202 ,203 ,200 ,305 ,205 ,206 ,207 ,9496,9484,9608,9604,166 ,204 ,9600,211 ,223 ,212 ,210 ,
39  245 ,213 ,181 ,254 ,222 ,218 ,219 ,217 ,253 ,221 ,175 ,180 ,173 ,177 ,8215,190 ,182 ,167 ,247 ,184 ,
40  176 ,168 ,183 ,185 ,179 ,178 ,9632,160 };
41 
42 QString HCodepage852::conv_from(char * from)
43 {
44  int i,l=strlen(from);
45  QString f;
46  f.fill(' ',l);
47  for(i=0;i<l;++i)
48  if((unsigned int)(from[i]) < 128u) //lower ascii not converted
49  f[i] = QChar((unsigned int)from[i]);
50  else
51  f[i] = QChar(cp852from128idx_utf16value[(unsigned char)from[i]-128u]);
52  return f;
53 }
54 
55 void HCodepage852::conv_to(QString from,char *buffer,int bufflen)
56 {
57  int i,ii,l=from.length();
58  for(i=0;i<l && i<bufflen;++i)
59  if((unsigned int)(from[i].unicode()) < 128u) //lower ascii not converted
60  buffer[i] = (unsigned char)from[i].unicode();
61  else
62  {
63  for(ii=0;ii<128;++ii)
64  if((int)cp852from128idx_utf16value[ii] == (int)from[i].unicode())
65  break;
66  buffer[i] = ii + 128;
67  }
68  if(i<bufflen)
69  buffer[i]='\0';
70 }
71 
72 QString HCodepage850::conv_from(char * from)
73 {
74  int i,l=strlen(from);
75  QString f;
76  f.fill(' ',l);
77  for(i=0;i<l;++i)
78  if((unsigned int)(from[i]) < 128u) //lower ascii not converted
79  f[i] = QChar((unsigned int)from[i]);
80  else
81  f[i] = QChar(cp850from128idx_utf16value[(unsigned char)from[i]-128u]);
82  return f;
83 }
84 
85 void HCodepage850::conv_to(QString from,char *buffer,int bufflen)
86 {
87  int i,ii,l=from.length();
88  for(i=0;i<l && i<bufflen;++i)
89  if((unsigned int)(from[i].unicode()) < 128u) //lower ascii not converted
90  buffer[i] = (unsigned char)from[i].unicode();
91  else
92  {
93  for(ii=0;ii<128;++ii)
94  if((int)cp850from128idx_utf16value[ii] == (int)from[i].unicode())
95  break;
96  buffer[i] = ii + 128;
97  }
98  if(i<bufflen)
99  buffer[i]='\0';
100 }
101 
102 // ///////////////////////////////////////////////////////////////////////////////////// //
103 
105 {
106  c = conv;
107  xbase = new xbXBase();
108  dbf = new xbDbf(xbase);
109  opened = false;
110  buffer= new char[CPCONV_BUFFERLENGTH];
111  deleteconvertobject = deletecobj;
112 }
113 
115 {
116  delete dbf;
117  delete xbase;
118  delete []buffer;
119  if(deleteconvertobject)
120  delete c;
121 }
122 
124 {
125  if(!opened)
126  return -1;
127 
128  return dbf->FieldCount();
129 }
130 
132 {
133  if(!opened)
134  return -1;
135 
136  xbULong recs;
137  recs = dbf->NoOfRecords();
138  return (long)recs;
139 }
140 
141 
143 {
144  if(!opened)
145  return "";
146  return dbf->GetFieldName((xbShort)n);
147 }
148 
149 HXBaseFieldType HXBaseFileHandler::fieldType(int n)
150 {
151  if(!opened)
152  return Error;
153 
154  char t;
155  t = dbf->GetFieldType((xbShort)n);
156  switch(t)
157  {
158  case 'C': return Char;
159  case 'D': return Date;
160  case 'L': return Logical;
161  case 'N': return Numeric;
162  case 'F': return Float;
163  case 'M': return Memo;
164  default: return Error;
165  }
166  return Error;
167 }
168 
169 // ///////////////////////////////////////////////////////////////////////////////////// //
170 
172 :HXBaseFileHandler(conv,deletecobj)
173 {
174 }
175 
177 {
178  if(opened)
179  close();
180 }
181 
182 int HXBaseFileReader::open(QString name)
183 {
184  xbShort rc;
185 
186  if(opened)
187  {
188  emit errorSignal(QString("HXBaseFileReader::open : A database file already opened!"));
189  return 1;
190  }
191 
192  if(( rc = dbf->OpenDatabase(name.toLocal8Bit().constData())) != XB_NO_ERROR )
193  {
194  emit errorSignal(QString("HXBaseFileReader::open : Error opening \"%1\" file!").arg(name));
195  return 1;
196  }
197  opened = true;
198  return 0;
199 }
200 
202 {
203  if(!opened)
204  {
205  emit errorSignal(QString("HXBaseFileReader::open : There is no opened database!"));
206  return 1;
207  }
208  dbf->CloseDatabase();
209  opened = false;
210  return 0;
211 }
212 
214 {
215  xbShort rc;
216  if(!opened)
217  return 1;
218  rc = dbf->GetRecord(idx);
219  if(rc != XB_NO_ERROR)
220  {
221  emit errorSignal(QString("HXBaseFileReader::toRecord: Cannot seek to record: !").arg(idx));
222  return 1;
223  }
224  return 0;
225 }
226 
228 {
229  if(!opened)
230  return -1;
231 
232  xbShort rc;
233  rc = dbf->GetFirstRecord();
234  if( rc == XB_NO_ERROR )
235  return 0;
236  return 1;
237 }
238 
240 {
241  if(!opened)
242  return -1;
243 
244  xbShort rc;
245  rc = dbf->GetLastRecord();
246  if( rc == XB_NO_ERROR )
247  return 0;
248  return 1;
249 }
250 
252 {
253  if(!opened)
254  return -1;
255 
256  xbShort rc;
257  rc = dbf->GetNextRecord();
258  if(rc == XB_NO_ERROR)
259  return 0;
260  return 1;
261 }
262 
264 {
265  if(!opened)
266  return -1;
267 
268  xbShort rc;
269  rc = dbf->GetPrevRecord();
270  if(rc == XB_NO_ERROR)
271  return 0;
272  return 1;
273 }
274 
276 {
277  return dbf->GetCurRecNo();
278 }
279 
280 QString HXBaseFileReader::getFieldStr(QString fname)
281 {
282  if(!opened)
283  return "";
284  xbShort no;
285  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
286  //buffer = dbf->GetFieldType(no);
287  dbf->GetField(no,buffer);
288  return c->conv_from(buffer).trimmed();
289 }
290 
292 {
293  if(!opened)
294  return 0;
295  xbShort no;
296  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
297  //buffer = dbf->GetFieldType(no);
298  return dbf->GetLongField(no);
299 }
300 
301 double HXBaseFileReader::getFieldFloat(QString fname)
302 {
303  if(!opened)
304  return 0;
305  xbShort no;
306  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
307  //buffer = dbf->GetFieldType(no);
308  return dbf->GetDoubleField(no);
309 }
310 
311 bool HXBaseFileReader::getFieldBool (QString fname)
312 {
313  if(!opened)
314  return 0;
315  xbShort no;
316  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
317  //buffer = dbf->GetFieldType(no);
318  return (bool)(dbf->GetLogicalField(no));
319 }
320 
321 QDate HXBaseFileReader::getFieldDate(QString fname)
322 {
323  QDate date;
324 
325  if(!opened)
326  return QDate();
327 
328  xbShort no;
329  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
330  //buffer = dbf->GetFieldType(no);
331  dbf->GetField(no,buffer);
332 
333  date = QDate::fromString(c->conv_from(buffer),"yyyyMMdd");
334  if(date.isValid())
335  return date;
336 
337  date = QDate::fromString(c->conv_from(buffer),"yyyy.MM.dd");
338  if(date.isValid())
339  return date;
340 
341  date = QDate::fromString(c->conv_from(buffer),"yyyy-MM-dd");
342  if(date.isValid())
343  return date;
344 
345  return QDate();
346 }
347 
348 QString HXBaseFileReader::getCellStr(long rec,QString fname)
349 {
350  if(toRecord(rec))
351  return QString();
352  return getFieldStr(fname);
353 }
354 
355 long HXBaseFileReader::getCellDecimal(long rec,QString fname)
356 {
357  if(toRecord(rec))
358  return 0;
359  return getFieldDecimal(fname);
360 }
361 
362 double HXBaseFileReader::getCellFloat(long rec,QString fname)
363 {
364  if(toRecord(rec))
365  return 0;
366  return getFieldFloat(fname);
367 }
368 
369 bool HXBaseFileReader::getCellBool(long rec,QString fname)
370 {
371  if(toRecord(rec))
372  return false;
373  return getFieldBool(fname);
374 }
375 
376 QDate HXBaseFileReader::getCellDate(long rec,QString fname)
377 {
378  if(toRecord(rec))
379  return QDate();
380  return getFieldDate(fname);
381 }
382 
383 
384 // ///////////////////////////////////////////////////////////////////////////////////// //
385 
387 :HXBaseFileHandler(conv,deletecobj)
388 {
389  defined_fnum = 0;
390  schema = new xbSchema[XBASESCHEMA_MAXFIELD];
391  setLastDefinedField(defined_fnum);
392 }
393 
395 {
396  delete schema;
397 }
398 
399 void HXBaseFileWriter::setLastDefinedField(int idx)
400 {
401  strncpy(schema[idx].FieldName,"",10);
402  schema[idx].Type = 0;
403  schema[idx].FieldLen = 0;
404  schema[idx].NoOfDecs = 0;
405 }
406 
407 int HXBaseFileWriter::open(QString name)
408 {
409  xbShort rc;
410 
411  if(opened)
412  {
413  emit errorSignal(QString("HXBaseFileWriter::open : A database file already opened!"));
414  return 1;
415  }
416 
417  if(( rc = dbf->OpenDatabase(name.toLocal8Bit().constData())) != XB_NO_ERROR )
418  {
419  emit errorSignal(QString("HXBaseFileWriter::open : Error opening \"%1\" file!").arg(name));
420  return 1;
421  }
422  opened = true;
423  return 0;
424 }
425 
427 {
428  if(!opened)
429  {
430  emit errorSignal(QString("HXBaseFileWriter::open : There is no opened database!"));
431  return 1;
432  }
433  dbf->CloseDatabase();
434  opened = false;
435  return 0;
436 }
437 
438 void HXBaseFileWriter::defineField(QString name,HXBaseFieldType type,int length,int nofdecimals)
439 {
440  if(opened)
441  {
442  emit errorSignal(QString("HXBaseFileWriter::defineField : A database file already opened, You can ONLY define field before creation!"));
443  return;
444  }
445 
446  int localtype;
447  int locallength=length;
448  if(type == Numeric) { localtype = XB_NUMERIC_FLD; }
449  if(type == Char) { localtype = XB_CHAR_FLD; }
450  if(type == Date) { localtype = XB_DATE_FLD; locallength = 8; }
451  if(type == Float) { localtype = XB_FLOAT_FLD; }
452  if(type == Logical) { localtype = XB_LOGICAL_FLD; locallength = 1; }
453  if(type == Memo)
454  {
455  emit errorSignal(QString("HXBaseFileWriter::defineField : The field has \"Memo\" type! I don't support it, sorry :-("));
456  return;
457  }
458  if(type == Error)
459  {
460  emit errorSignal(QString("HXBaseFileWriter::defineField : The field has \"Error\" type!"));
461  return;
462  }
463 
464  strncpy(schema[defined_fnum].FieldName,name.toLocal8Bit().constData(),10);
465  schema[defined_fnum].Type = localtype;
466  schema[defined_fnum].FieldLen = locallength;
467  schema[defined_fnum].NoOfDecs = nofdecimals;
468 
469  setLastDefinedField(++defined_fnum);
470 }
471 
472 int HXBaseFileWriter::create(QString name,int version)
473 {
474  xbShort rc;
475 
476  if(opened)
477  {
478  emit errorSignal(QString("HXBaseFileWriter::open : A database file already opened!"));
479  return 1;
480  }
481  dbf->SetVersion( version );
482  if(( rc = dbf->CreateDatabase( name.toLocal8Bit().constData() , schema , XB_OVERLAY )) != XB_NO_ERROR )
483  {
484  emit errorSignal(QString("HXBaseFileWriter::open : Error opening \"%1\" file!").arg(name));
485  return 1;
486  }
487  opened = true;
488  resetRecord();
489  return 0;
490 }
491 
492 
494 {
495  if(!opened)
496  return 1;
497  dbf->BlankRecord();
498  return 0;
499 }
500 
502 {
503  if(!opened)
504  return 1;
505 
506  xbShort rc;
507  if(( rc = dbf->AppendRecord()) != XB_NO_ERROR )
508  {
509  emit errorSignal(QString("HXBaseFileWriter::appendRecord : Append record is unsuccessful!"));
510  return 1;
511 
512  }
513  resetRecord();
514  return 0;
515 }
516 
517 int HXBaseFileWriter::setFieldStr(QString fname,QString str)
518 {
519  if(!opened)
520  return 1;
521 
522  xbShort no;
523  int max=CPCONV_BUFFERLENGTH;
524 
525  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
526  if(dbf->GetFieldLen(no) < CPCONV_BUFFERLENGTH)
527  max = dbf->GetFieldLen(no);
528  c->conv_to(str,buffer,max);
529  dbf->PutField(no,buffer);
530  return 0;
531 }
532 
533 int HXBaseFileWriter::setFieldDecimal(QString fname,long decimal)
534 {
535  if(!opened)
536  return 1;
537  xbShort no;
538  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
539  dbf->PutLongField(no,decimal);
540  return 0;
541 }
542 
543 int HXBaseFileWriter::setFieldFloat(QString fname,double val)
544 {
545  if(!opened)
546  return 1;
547 
548  xbShort no;
549  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
550  dbf->PutDoubleField(no,val);
551  return 0;
552 }
553 
554 int HXBaseFileWriter::setFieldBool(QString fname,bool logical)
555 {
556  if(!opened)
557  return 1;
558 
559  xbShort no;
560  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
561  dbf->PutField(no,logical ? "T" : "F");
562  return 0;
563 }
564 
565 int HXBaseFileWriter::setFieldDate(QString fname,QDate date)
566 {
567  if(!opened)
568  return 1;
569 
570  xbShort no;
571  no = dbf->GetFieldNo(fname.toLocal8Bit().constData());
572  c->conv_to(date.toString("yyyyMMdd"),buffer,8);
573  dbf->PutField(no,buffer);
574  return 0;
575 }
576 
577 #endif // ENABLE_XBASE_SUPPORT
578 
579 //end code.
int resetRecord(void)
Definition: xbio.cpp:493
HXBaseFileWriter(HCodepageConvert *conv, bool deletecobj=false)
Definition: xbio.cpp:386
long getFieldDecimal(QString fname)
Definition: xbio.cpp:291
HXBaseFileReader(HCodepageConvert *conv, bool deletecobj=false)
Definition: xbio.cpp:171
QDate getFieldDate(QString fname)
Definition: xbio.cpp:321
int toRecord(long idx)
Definition: xbio.cpp:213
int close(void)
Definition: xbio.cpp:426
int open(QString name)
Definition: xbio.cpp:182
int nextRecord(void)
Definition: xbio.cpp:251
int create(QString name, int version)
Definition: xbio.cpp:472
bool getFieldBool(QString fname)
Definition: xbio.cpp:311
int prevRecord(void)
Definition: xbio.cpp:263
HXBaseFileHandler(HCodepageConvert *conv, bool deletecobj=false)
Definition: xbio.cpp:104
~HXBaseFileReader(void)
Definition: xbio.cpp:176
~HXBaseFileHandler(void)
Definition: xbio.cpp:114
int setFieldFloat(QString fname, double val)
Definition: xbio.cpp:543
int open(QString name)
Definition: xbio.cpp:407
long getCurrentRecordIdx(void)
Definition: xbio.cpp:275
void errorSignal(QString error)
HXBaseFieldType fieldType(int n)
Definition: xbio.cpp:149
int close(void)
Definition: xbio.cpp:201
QString getFieldStr(QString fname)
Definition: xbio.cpp:280
QString fieldName(int n)
Definition: xbio.cpp:142
int setFieldBool(QString fname, bool logical)
Definition: xbio.cpp:554
~HXBaseFileWriter(void)
Definition: xbio.cpp:394
int firstRecord(void)
Definition: xbio.cpp:227
long recordCount(void)
Definition: xbio.cpp:131
int setFieldDecimal(QString fname, long decimal)
Definition: xbio.cpp:533
int appendRecord(void)
Definition: xbio.cpp:501
int setFieldDate(QString fname, QDate date)
Definition: xbio.cpp:565
void defineField(QString name, HXBaseFieldType type, int length=0, int nofdecimals=0)
Definition: xbio.cpp:438
double getFieldFloat(QString fname)
Definition: xbio.cpp:301
int fieldCount(void)
Definition: xbio.cpp:123
int setFieldStr(QString fname, QString str)
Definition: xbio.cpp:517
int lastRecord(void)
Definition: xbio.cpp:239