File: xbfields.cpp

package info (click to toggle)
xbase64 3.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,844 kB
  • ctags: 2,180
  • sloc: cpp: 14,547; sh: 8,678; makefile: 112
file content (672 lines) | stat: -rwxr-xr-x 19,838 bytes parent folder | download | duplicates (3)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*  xbfields.cpp

    Xbase64 project source code

    This file contains the basic X-Base routines for reading and writing
    Xbase fields.

    Copyright (C) 1997,2003  Gary A Kunkel
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


    Contact:
    
     Email:
    
      xdb-devel@lists.sourceforge.net
      xdb-users@lists.sourceforge.net
      
      
     Regular Mail:
     
       XBase Support
       149C South Main St
       Keller Texas, 76248     
       USA
*/

#ifdef __WIN32__
#include <xbase64/xbwincfg.h>
#else
#include <xbase64/xbconfig.h>
#endif

#include <xbase64/xbase64.h>

/*! \file xbfields.cpp
*/
/************************************************************************/
/* This function returns true if the data is valid logical data         */
//! Determines if data is valid logical data.
/*! Determines if the data in buf is valid for a logical field value.
    
    \param buf data to be tested
    \returns TRUE (non-zero) if valid, FALSE (zero) if not.
*/
xbShort xbDbf::ValidLogicalData(const char * buf) {
  if( buf[0] )
    if( buf[0] == 'T' || buf[0] == 't' || buf[0] == 'F' || buf[0] == 'f' ||
        buf[0] == 'Y' || buf[0] == 'y' || buf[0] == 'N' || buf[0] == 'n' ||
        buf[0] == '?' )
      return 1;
  return 0; 
}
/************************************************************************/
/* This function returns true if the data is valid numeric data         */
//! Determines if data is valid numeric data.
/*! Determines if the data in buf is valid for a numeric field value.

    \param buf
    \returns TRUE (non-zero) if valid, FALSE (zero) if not.
*/
xbShort xbDbf::ValidNumericData(const char * buf) {
  const char *p;

  p = buf;
  while( *p ){
    if( *p != '+' && *p != '-' && *p != '.' && *p != '0' && *p != '1' &&
        *p != '2' && *p != '3' && *p != '4' && *p != '5' && *p != '6' &&
        *p != '7' && *p != '8' && *p != '9' )
      return 0;
    else
      p++;
  }
  return 1;
}
/************************************************************************/
/* This function returns a fields length */
//! Returns the length of the specified field.
/*! Returns the length of the field specified by FieldNo.

    \param FieldNo Number of field.
    \returns Length of the specified field in bytes.
*/
xbShort xbDbf::GetFieldLen( xbShort FieldNo )
{
  if( FieldNo >= 0 && FieldNo < NoOfFields ){
    if( SchemaPtr[FieldNo].Type == 'C' && SchemaPtr[FieldNo].NoOfDecs > 0 )
      return SchemaPtr[FieldNo].LongFieldLen;
    else    
      return SchemaPtr[FieldNo].FieldLen;
  }
  else
    return 0;
}
/************************************************************************/
/* This function returns a fields decimal length */
//! Returns the number of decimals in the specified field.
/*! Returns the number of decimals in the field specified by FieldNo.

    \param FieldNo Number of field.
    \returns Length of the specified field in bytes.
*/

xbShort xbDbf::GetFieldDecimal( xbShort FieldNo )
{
  if( FieldNo >= 0 && FieldNo < NoOfFields )
    return SchemaPtr[FieldNo].NoOfDecs;
  else
    return 0;
}
/************************************************************************/
/* This function returns a fields type */
//! Returns the type of the specified field.
/*! Returns the type of the field specified by FieldNo.

    \param FieldNo Number of field.
    \returns Type of specified field.
*/
char xbDbf::GetFieldType( xbShort FieldNo ) const
{
  if( FieldNo >= 0 && FieldNo < NoOfFields )
    return SchemaPtr[FieldNo].Type;
  else
    return 0;
}
/************************************************************************/
/* This function returns a fields name */
//! Returns the name of the specified field.
/*! Returns a pointer to the name for the field specified by FieldNo.

    \param FieldNo Number of field.
    \returns A pointer to the name of the field.
*/
char * xbDbf::GetFieldName( xbShort FieldNo )
{
  if( FieldNo >= 0 && FieldNo < NoOfFields )
    return SchemaPtr[FieldNo].FieldName;
  else
    return 0;
}
/************************************************************************/
/* This function returns the field ID number for a given field
   or -1 if the field is not one of the fields of the database  */
//! Returns the field number of the specified field.
/*! Returns the field number for the named field.

    \param name Name of field.
    \returns Number of field named name.
*/
xbShort xbDbf::GetFieldNo( const char * name ) const
{
  int i, len1, len2;

  if(( len1 = strlen( name )) > 10 )
    return -1;

  for( i = 0; i < NoOfFields; i++ ){
    len2 = strlen( SchemaPtr[i].FieldName );
    if( len1 == len2 )

//#ifndef __WIN32__
#ifdef HAVE_STRCASECMP
      if(!strcasecmp( SchemaPtr[i].FieldName, name )) 
#else
      if(!stricmp( SchemaPtr[i].FieldName, name )) 
#endif

      return i;
   }
   return -1;
}
/************************************************************************/
/*
   Helpers
*/

//! Get the value of the specified field.
/*! Get the value of the field referenced by Name and place its value
    in buf.
    
    \param Name Name of field.
    \param buf Buffer to hold field value.  Must be large enough to hold
               the entire field value.  Use GetFieldLen() to determine
               the length of the field, if necessary.
    \param RecBufSw
    \returns One of the following:
*/
xbShort xbDbf::GetField(const char *Name, char *buf,
         const xbShort RecBufSw ) const
{
   return GetField(GetFieldNo(Name), buf, RecBufSw);
}

/************************************************************************/
//! Get the value of the specified field.
/*! Get the value of the field specified by Name and place its value
    in buf.
    
    \param Name Name of field.
    \param buf Buffer to hold field value.  Must be large enough to hold
               the entire field value.  Use GetFieldLen() to determine
               the length of the field, if necessary.
    \returns One of the following:
*/
xbShort xbDbf::GetField(const char *Name, char *buf) const
{
  return GetField(GetFieldNo(Name), buf);
}
/************************************************************************/
//! Get the raw value of the specified field.
/*! Get the value of the field specified by Name and place its value
    in buf.
    
    \param Name Name of field.
    \param buf Buffer to hold field value.  Must be large enough to hold
               the entire field value.  Use GetFieldLen() to determine
               the length of the field, if necessary.
    \returns One of the following:
*/
xbShort xbDbf::GetRawField(const char *Name, char *buf) const
{
  return GetRawField(GetFieldNo(Name), buf);
}

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

// FIXME this function doesn't follow look and feel of the rest of the lib
//  GAK

static char __buf[1024];

static void trim(char *s) {
  int len = strlen(s)-1;
  if (len > 0) {
    while ((len != 0) && (s[len] == ' '))
      len--;
    s[len+1] = 0;
  }
}

//! Get the value of the specified field.
/*! Returns the value of the field specified by Name.

    \param Name Name of field.
    \returns Value of the specified field.
*/
const char *xbDbf::GetField(const char *Name) const {
  GetField(GetFieldNo(Name), __buf);
  trim(__buf);
  return __buf;
}

//! Get the value of the specified field.
/*! Returns the value of the field specified by FieldNo.

    \param FieldNo Number of field.
    \returns Value of the specified field.
*/
const char *xbDbf::GetField(xbShort FieldNo) const {
  GetField(FieldNo, __buf);
  trim(__buf);
  return __buf;
}
/************************************************************************/
/* This function fills a buffer with data from the record buffer
   for a particular field number. 

   Use GetFieldNo to get a number based on a field's name 

   If successful, this function returns the field size.
*/

//! Get the value of the specified field.
/*! Get the value of the field specified by FieldNo and place its value
    in buf.
    
    \param FieldNo Number of field.
    \param buf Buffer to hold field value.  Must be large enough to hold
               the entire field value.  Use GetFieldLen() to determine
               the length of the field, if necessary.
    \param RecBufSw
    \returns The length of the field.
*/
xbShort xbDbf::GetField( xbShort FieldNo, char * buf, xbShort RecBufSw) const
{
  xbShort length;        
  if( FieldNo < 0 || FieldNo >= NoOfFields ) {
    buf[0] = 0x00;
    return 0x00;
  }

// Check for existence of a long field length
  if( SchemaPtr[FieldNo].Type == 'C' && SchemaPtr[FieldNo].NoOfDecs > 0 )
    length = SchemaPtr[FieldNo].LongFieldLen;
  else
    length = SchemaPtr[FieldNo].FieldLen;

  if( RecBufSw )
    memcpy( buf, SchemaPtr[FieldNo].Address2, length );
  else
    memcpy( buf, SchemaPtr[FieldNo].Address, length );
  buf[length] = 0x00;
  return( length ); 
}
/************************************************************************/
xbShort xbDbf::GetField( xbShort FieldNo, xbString & sf, xbShort RecBufSw) const
{
  xbShort length;        
  if( FieldNo < 0 || FieldNo >= NoOfFields ) {
    sf = "";
    return 0;
  }

  // Check for existence of a long field length
  if( SchemaPtr[FieldNo].Type == 'C' && SchemaPtr[FieldNo].NoOfDecs > 0 )
    length = SchemaPtr[FieldNo].LongFieldLen;
  else
    length = SchemaPtr[FieldNo].FieldLen;

  if( RecBufSw )
    sf.assign( xbString(SchemaPtr[FieldNo].Address2, length), 0, length );
  else
    sf.assign( xbString(SchemaPtr[FieldNo].Address, length), 0, length );

  return( length ); 
}
/************************************************************************/
/* This function fills a field in the record buffer with data from
   a buffer for a particular field.

   Use GetFieldNo to get a number based on a field's name 

   Field type N or F is loaded as right justified, left blank filled.
   Other fields are loaded as left justified, right blank filled.

   This method does check the data's validity.

   If successful, this function returns 0, if invalid data, it returns -1
   or XB_INVALID_FIELDNO
*/

//! Put a value into the specified field.
/*!
*/
xbShort xbDbf::PutField(const char *Name, const char *buf) {
  return PutField(GetFieldNo(Name), buf);
}
/************************************************************************/
//! Put a raw value into the specified field.
/*!
*/
xbShort xbDbf::PutRawField(const char *Name, const char *buf) {
  return PutRawField(GetFieldNo(Name), buf);
}
/************************************************************************/
//! Put a value into the specified field.
/*!
*/
xbShort xbDbf::PutField(const xbShort FieldNo, const char *buf) {
  xbShort len, i;
  char * startpos;
  char * tp;           /*  target pointer */
  const char * sp;        /*  source pointer */

  if( FieldNo < 0 || FieldNo >= NoOfFields )
    return XB_INVALID_FIELDNO;

  if( DbfStatus != XB_UPDATED ){
    DbfStatus = XB_UPDATED;
    memcpy( RecBuf2, RecBuf, RecordLen );
  }
  
  if( SchemaPtr[FieldNo].Type == 'L' && !ValidLogicalData( buf ))
    return XB_INVALID_DATA;

  else if(( SchemaPtr[FieldNo].Type == 'F' || SchemaPtr[FieldNo].Type == 'N' )
       && !ValidNumericData( buf )) 
    return XB_INVALID_DATA;

  else if( SchemaPtr[FieldNo].Type == 'D' ){
    xbDate d;
    if( !d.DateIsValid( buf ))
      return XB_INVALID_DATA;
  }

  if( SchemaPtr[FieldNo].Type == 'C' && SchemaPtr[FieldNo].NoOfDecs > 0 )
    memset( SchemaPtr[FieldNo].Address, 0x20, SchemaPtr[FieldNo].LongFieldLen );
  else
    memset( SchemaPtr[FieldNo].Address, 0x20, SchemaPtr[FieldNo].FieldLen );

  len = strlen( buf );

  if(( SchemaPtr[FieldNo].Type == 'N' || SchemaPtr[FieldNo].Type == 'F')
       && len > SchemaPtr[FieldNo].FieldLen )
    return XB_INVALID_DATA;
  else if( len > SchemaPtr[FieldNo].FieldLen )
    len = SchemaPtr[FieldNo].FieldLen;

  if( SchemaPtr[FieldNo].Type == 'F' || SchemaPtr[FieldNo].Type == 'N' 
      || SchemaPtr[FieldNo].Type == 'M') {

    const char *sdp = strchr( buf, '.' ); /*  source decimal point */
    len = 0;
    sp =buf;
    while( *sp && *sp != '.' ) { len++; sp++; }
    if( SchemaPtr[FieldNo].NoOfDecs > 0 ){
      /* do the right of decimal area */
      tp = SchemaPtr[FieldNo].Address;
      tp += SchemaPtr[FieldNo].FieldLen - SchemaPtr[FieldNo].NoOfDecs - 1;
      *tp++ = '.';
      sp = sdp;
      if( sp ) sp++;
      for( i = 0; i < SchemaPtr[FieldNo].NoOfDecs; i++ )
        if( sp && *sp ) *tp++ = *sp++; else *tp++ = '0'; 

      startpos= SchemaPtr[FieldNo].Address +
                SchemaPtr[FieldNo].FieldLen -
                SchemaPtr[FieldNo].NoOfDecs - len - 1; 
    }
    else
    {
      startpos=SchemaPtr[FieldNo].Address+SchemaPtr[FieldNo].FieldLen-len; 
    }
  }
  else
    startpos = SchemaPtr[FieldNo].Address;

  memcpy( startpos, buf, len );
  return 0;
}

/************************************************************************/
//! Put a raw value into the specified field.
/*!
*/
xbShort xbDbf::PutRawField(const xbShort FieldNo, const char *buf) {
  xbShort len;
  char * startpos;

  if( FieldNo < 0 || FieldNo >= NoOfFields )
    return XB_INVALID_FIELDNO;

  if( DbfStatus != XB_UPDATED ){
    DbfStatus = XB_UPDATED;
    memcpy( RecBuf2, RecBuf, RecordLen );
  }
  
  startpos = SchemaPtr[FieldNo].Address;
  len = SchemaPtr[FieldNo].FieldLen;
  memcpy( startpos, buf, len );
   
  return 0;
}

/************************************************************************/
//! Get the value of the specified field.
/*!
*/
xbShort xbDbf::GetField( xbShort FieldNo, char *buf) const {
  return GetField(FieldNo, buf, 0);
}
/************************************************************************/
//! Get the raw value of the specified field.
/*!
*/
xbShort xbDbf::GetRawField( xbShort FieldNo, char *buf ) const {
  return GetField(FieldNo, buf, 0);
}
/************************************************************************/
//! Get the long value of the specified field.
/*!
*/
xbLong xbDbf::GetLongField( xbShort FieldNo ) const
{
  char buf[18];
  memset( buf, 0x00, 18 );
  GetField( FieldNo, buf );
  return atol( buf );
}
/************************************************************************/
//! Get the long value of the specified field.
/*!
*/
xbLong xbDbf::GetLongField( const char * FieldName ) const
{
  return( GetLongField( GetFieldNo( FieldName )));
}
/************************************************************************/
//! Put a long value into the specified field.
/*!
*/
xbShort xbDbf::PutLongField( xbShort FieldNo, xbLong Val )
{
  char buf[18];
  memset( buf, 0x00, 18 );
  sprintf( buf, "%ld", Val );
  return( PutField( FieldNo, buf ));
}
/************************************************************************/
//! Put a long value into the specified field.
/*!
*/
xbShort xbDbf::PutLongField(const char *FieldName, xbLong Val) {
  return ( PutLongField( GetFieldNo( FieldName ), Val ));
}
/************************************************************************/
//! Get the float value of the specified field.
/*!
*/
xbFloat xbDbf::GetFloatField( xbShort FieldNo )
{
  char buf[21];
  memset( buf, 0x00, 21 );
  if( GetField( FieldNo, buf ) != 0 )
    return atof( buf );
  else
    return 0;
}
/************************************************************************/
//! Get the float value of the specified field.
/*!
*/
xbFloat xbDbf::GetFloatField(const char * FieldName) {
  xbShort fnum;
  if((fnum = GetFieldNo(FieldName)) != -1)
    return GetFloatField(fnum);
  else
    return 0;
}
/************************************************************************/
//! Put a float value into the specified field.
/*!
*/
xbShort xbDbf::PutFloatField( xbShort FldNo, xbFloat f )
{
  char buf[25];
  char buf2[12];
  memset( buf, 0x00, 25 );
  memset( buf2, 0x00, 12 );
  sprintf( buf, "%d.%df", GetFieldLen( FldNo ), GetFieldDecimal( FldNo ));
  strcpy( buf2, "%-" );
  strcat( buf2, buf );
  sprintf( buf, buf2, f );

  /* remove trailing space */
  xbShort i = 0;
  while( i < 25 ) 
    if( buf[i] == 0x20 ){
      buf[i] = 0x00;
      break;
    } else
      i++;
  return PutField( FldNo, buf );
}
/************************************************************************/
//! Put a float value into the specified field.
/*!
*/
xbShort xbDbf::PutFloatField(const char *FieldName, xbFloat f) {
 xbShort fnum;
 if ((fnum = GetFieldNo(FieldName)) != -1)
   return PutFloatField(fnum, f);
 else
   return 0;
}
/************************************************************************/
//! Get the double value of the specified field.
/*!
*/
xbDouble xbDbf::GetDoubleField( xbShort FieldNo, xbShort RecBufSw )
{
  char buf[21];
  memset( buf, 0x00, 21 );
  if( GetField( FieldNo, buf, RecBufSw ) != 0 )
    return strtod( buf, NULL );
  else
    return 0;
}
/************************************************************************/
//! Get the double value of the specified field.
/*!
*/
xbDouble xbDbf::GetDoubleField(const char *FieldName) {
  xbShort fnum;
  if ((fnum = GetFieldNo(FieldName)) != -1)
    return GetDoubleField(fnum);
  else
    return 0;
}
/************************************************************************/
//! Put a double value into the specified field.
/*!
*/
xbShort xbDbf::PutDoubleField( xbShort FieldNo, xbDouble d) {
  return PutFloatField(FieldNo, (xbFloat)d);
}
/************************************************************************/
//! Put a double value into the specified field.
/*!
*/
xbShort xbDbf::PutDoubleField(const char *FieldName, xbDouble d) {
  xbShort fnum;
  if ((fnum = GetFieldNo(FieldName)) != -1)
    return PutFloatField(fnum, (xbFloat)d);
  else
    return 0;
}
/************************************************************************/
//! Get the logical value of the specified field.
/*!
*/
xbShort xbDbf::GetLogicalField( xbShort FieldNo )
{
  char buf[3];
  if( GetFieldType( FieldNo ) != 'L' ) return -1;
  memset( buf, 0x00, 3 );
  GetField( FieldNo, buf );
  if( buf[0] == 'Y' || buf[0] == 'y' || buf[0] == 'T' || buf[0] == 't' )
    return 1;
  else
    return 0;
}
/************************************************************************/
//! Get the logical value of the specified field.
/*!
*/
xbShort xbDbf::GetLogicalField( const char * FieldName )
{
  xbShort fnum;
  if(( fnum = GetFieldNo( FieldName )) != -1 )
    return GetLogicalField( fnum );
  else
    return -1;
}
/************************************************************************/
//! Get the string value of the specified field.
/*!
*/
char * xbDbf::GetStringField( const char * FieldName )
{
  return GetStringField(GetFieldNo(FieldName));
}
/************************************************************************/
//! Get the string value of the specified field.
/*!
*/
char * xbDbf::GetStringField( xbShort FieldNo )
{
  /* allocate memory if needed */
  if( !SchemaPtr[FieldNo].fp )
    SchemaPtr[FieldNo].fp = new char[GetFieldLen(FieldNo)+1];

  if( !SchemaPtr[FieldNo].fp )
    return 0;

  GetField( FieldNo, SchemaPtr[FieldNo].fp );
  return SchemaPtr[FieldNo].fp;
}
/************************************************************************/