summaryrefslogtreecommitdiff
path: root/src/core/xbxbase.cpp
blob: 7dd3ac7b75d2b855f07aed389025245e0c06d348 (plain)
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* xbxbase.cpp

XBase64 Software Library

Copyright (c) 1997,2003,2014,2022 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/

#include "xbase.h"

namespace xb{

/*************************************************************************/
//! @brief Class Constructor.
xbXBase::xbXBase() {
  SetEndianType();
  #ifdef XB_LOGGING_SUPPORT
  xLog = new xbLog();
  #endif
}
/*************************************************************************/
//! @brief Class Deconstructor.
xbXBase::~xbXBase(){
  CloseAllTables();
  #ifdef XB_LOGGING_SUPPORT
  delete xLog;
  #endif
}

/*************************************************************************/
//! @brief Close all tables / files.
/*!
   This closes everything.
   \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbXBase::CloseAllTables(){

  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  xbInt16 iOpenTableCnt = GetOpenTableCount();
  try{
    xbDbf *d;
    for( xbInt16 i = 0; i < iOpenTableCnt; i++ ){
      d = (xbDbf *) GetDbfPtr( 1 );
      if( d ){
        if(( iRc = d->Close()) != XB_NO_ERROR ){
          iErrorStop = 10;
          throw iRc;
        }
      } else {
        iRc = XB_INVALID_OBJECT;
        iErrorStop = 20;
        throw iRc;
      }
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbxbase::CloseAllTables() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, iRc );
    WriteLogMessage( sMsg.Str() );
    WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
/*************************************************************************/
#ifdef XB_LOGGING_SUPPORT
//! @brief Get fully qualified log file name.
/*!
   \returns Returns the fully qualified log file name.
*/
const xbString & xbXBase::GetLogFqFileName() const {
  return xLog->GetFqFileName();
}

//! @brief Get the log file name.
/*!
   \returns Returns the log file name.
*/
const xbString & xbXBase::GetLogFileName() const {
  return xLog->GetFileName();
}

//! @brief Get the log directory.
/*!
   \returns Returns the log directory.
*/
const xbString & xbXBase::GetLogDirectory() const {
  return xLog->GetDirectory();
}

//! @brief Get the log directory.
/*!
   \returns xbTrue - Logging enabled.<br>xbFalse - Logging disables.
*/
xbBool xbXBase::GetLogStatus() const {
  return xLog->LogGetStatus();
}

//! @brief Set the log file name.
/*!
  \param sLogFileName - Log File Name.
  \return void
*/
void xbXBase::SetLogFileName( const xbString & sLogFileName ){
  xLog->SetFileName( sLogFileName );
}

//! @brief Set the log directory.
/*!
  \param sLogDirectory - Log File Directory.
  \return void
*/
void xbXBase::SetLogDirectory( const xbString & sLogDirectory ){
  xLog->SetDirectory( sLogDirectory );
}

//! @brief Set the logfile size.
/*!
  \param lSize - Log File Size.
  \return void
*/
void xbXBase::SetLogSize( size_t lSize ) {
  xLog->LogSetLogSize( lSize );
}


//! @brief Write message to logfile.
/*!
  \param sLogMessage - Message to write.
  \param iOpt 0 = stdout<br>
              1 = Syslog<br>
              2 = Both<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbXBase::WriteLogMessage( const xbString & sLogMessage, xbInt16 iOpt ){
  return xLog->LogWrite( sLogMessage, iOpt );
}

//! @brief Write message to logfile.
/*!
  \param lCnt - Number of bytes to write.
  \param p - Pointer to bytes to write to log file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbXBase::WriteLogBytes( xbUInt32 lCnt, const char *p ){
  return xLog->LogWriteBytes( lCnt, p );
}

//! @brief Enable message logging.
void xbXBase::EnableMsgLogging() {
  xLog->LogSetStatus( xbTrue );
}

//! @brief Disable message logging.
void xbXBase::DisableMsgLogging() {
  xLog->LogSetStatus( xbFalse );
}

//! @brief Flush log file updates to disk.
xbInt16 xbXBase::FlushLog() {
  return xLog->xbFflush();
}
#else

// if logging not compiled in, these stubs are called with no effect
const xbString & xbXBase::GetLogFqFileName() const {
  return sNullString;
}
const xbString & xbXBase::GetLogFileName() const {
  return sNullString;
}
const xbString & xbXBase::GetLogDirectory() const {
  return sNullString;
}
void xbXBase::SetLogFileName( const xbString & sLogFileName ){
  return;
}
void xbXBase::SetLogDirectory( const xbString & sLogDirectory ){
  return;
}
xbBool xbXBase::GetLogStatus() const {
  return xbFalse;
}
xbInt16 xbXBase::WriteLogMessage( const xbString & sLogMessage ){
  return XB_NO_ERROR;
}
xbInt16 xbXBase::WriteLogBytes( xbUInt32 lCnt, const char *p ){
  return XB_NO_ERROR;
}
void xbXBase::EnableMsgLogging() {
  return;
}
void xbXBase::DisableMsgLogging() {
  return;
}
xbInt16 xbXBase::FlushLog() {
  return;
}
void xbXBase::SetLogSize( size_t lSize ) {
  return;
}
#endif   // XB_LOGGING_SUPPORT

/*************************************************************************/
#ifdef XB_FUNCTION_SUPPORT

//! @brief Get information regarding expression functions.
/*!
  \param sExpLine An expression beginning with function name.
  \param cReturnType Output - return type of function.
  \param iCalc Used to calculate the function return value is<br>
       1 = use value specified in lReturnLenVal<br>
       2 = use length of operand specified in col 4<br>
       3 = use valued of numeric operand specified in col 4<br>
       4 = length of parm 1 * numeric value parm<br>
       5 = larger length of parm 2 or length of parm 3<br>
       6 = if two or more parms, use numeric value from second parm, otherwise use col4 value
  \param lReturnLenVal Used in combination with iReturnLenCalc.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbXBase::GetFunctionInfo( const xbString &sExpLine, char &cReturnType, xbInt16 &iCalc, xbInt32 &lReturnLenVal ) const{

  xbUInt32 iLen;
  const   char *s;
  if( sExpLine.Len() == 0 )
    return XB_INVALID_FUNCTION;

  s = sExpLine;
  iLen = 0;
  while( *s && *s != '(' && *s != ' ' ) { s++; iLen++; }
  xbString sFunction( sExpLine, iLen );
  cReturnType = 0x00;
  char cFunc1 = sFunction[1];

  if( cFunc1 < 'L' ){
    // std::cout << "less than L\n";
    if( cFunc1 < 'D' ){
       // std::cout << "less than D\n";
      if( sFunction == "ABS" ){
         // { "ABS",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "ALLTRIM" ){
        // { "ALLTRIM",   'C',      2,   1 },
        cReturnType   = 'C';
        iCalc         = 2;
        lReturnLenVal = 1;
      } else if( sFunction == "ASC" ){
        // { "ASC",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "AT" ){
        // { "AT",        'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "CDOW" ){
        // { "CDOW",      'C',      1,   9 },
        cReturnType   = 'C';
        iCalc         = 1;
        lReturnLenVal = 9;
      } else if( sFunction == "CHR" ){
        // { "CHR",       'C',      1,   1 },
        cReturnType   = 'C';
        iCalc         = 1;
        lReturnLenVal = 1;
      } else if( sFunction == "CMONTH" ){
        // { "CMONTH",    'C',      1,   9 },
        cReturnType   = 'C';
        iCalc         = 1;
        lReturnLenVal = 9;
      } else if( sFunction == "CTOD" ){
        // { "CTOD",      'D',      1,   8 },
        cReturnType   = 'D';
        iCalc         = 1;
        lReturnLenVal = 8;
      }
    } else {
      // std::cout << ">= D\n";
      if( sFunction == "DATE" ){
        // { "DATE",      'D',      1,   8 },
        cReturnType   = 'D';
        iCalc         = 1;
        lReturnLenVal = 8;
      } else if( sFunction == "DAY" ){
        // { "DAY",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "DEL" ){
        // { "DEL",       'C',      1,   1 },
        cReturnType   = 'C';
        iCalc         = 1;
        lReturnLenVal = 1;
      } else if( sFunction == "DELETED" ){
        //  { "DELETED",   'L',      1,   1 },
        cReturnType   = 'L';
        iCalc         = 1;
        lReturnLenVal = 1;
      } else if( sFunction == "DESCEND" ){
        // { "DESCEND",   '1',      2,   1 },
        cReturnType   = '1';
        iCalc         = 2;
        lReturnLenVal = 1;
      } else if( sFunction == "DOW" ){
        // { "DOW",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "DTOC" ){
        //   { "DTOC",      'C',      1,   8 },
        cReturnType   = 'C';
        iCalc         = 1;
        lReturnLenVal = 8;
      } else if( sFunction == "DTOS" ){
        // { "DTOS",      'C',      1,   8 },
        cReturnType   = 'C';
        iCalc         = 1;
        lReturnLenVal = 8;
      } else if( sFunction == "EXP" ){
        // { "EXP",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "IIF" ){
        // { "IIF",       'C',      5,   0 },
        cReturnType   = 'C';
        iCalc         = 5;
        lReturnLenVal = 0;
      } else if( sFunction == "INT" ){
        // { "INT",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "ISALPHA" ){
        // { "ISALPHA",   'L',      1,   1 },
        cReturnType   = 'L';
        iCalc         = 1;
        lReturnLenVal = 1;
      } else if( sFunction == "ISLOWER" ){
        // { "ISLOWER",   'L',      1,   1 },
        cReturnType   = 'L';
        iCalc         = 1;
        lReturnLenVal = 1;
      } else if( sFunction == "ISUPPER" ){
        // { "ISUPPER",   'L',      1,   1 },
        cReturnType   = 'L';
        iCalc         = 1;
        lReturnLenVal = 1;
      }
    }
  } else {
    // std::cout << ">= L\n";
    if( cFunc1 < 'R' ) {
      // std::cout << " < R\n";
      if( sFunction == "LEFT" ){
      // { "LEFT",      'C',      3,   2 },
        cReturnType   = 'C';
        iCalc         = 3;
        lReturnLenVal = 2;
      } else if( sFunction == "LEN" ){
        // { "LEN",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 3;
      } else if( sFunction == "LOG" ){
        // { "LOG",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "LOWER" ){
        //  { "LOWER",     'C',      2,   1 },
        cReturnType   = 'C';
        iCalc         = 2;
        lReturnLenVal = 1;
      } else if( sFunction == "LTRIM" ){
        //   { "LTRIM",     'C',      2,   1 },
        cReturnType   = 'C';
        iCalc         = 2;
        lReturnLenVal = 1;
      } else if( sFunction == "MAX" ){
        // { "MAX",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "MIN" ){
        //   { "MIN",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "MONTH" ){
        // { "MONTH",     'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      }
    } else if( cFunc1 == 'R' ){
      // std::cout << "==R\n";
      if( sFunction == "RECNO" ){
        //   { "RECNO",     'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "RECCOUNT" ){
        //  { "RECCOUNT",  'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "REPLICATE" ){
        // { "REPLICATE", 'C',      4,   0 },
        cReturnType   = 'C';
        iCalc         = 4;
        lReturnLenVal = 0;
      } else if( sFunction == "RIGHT" ){
        // { "RIGHT",     'C',      3,   2 },
        cReturnType   = 'C';
        iCalc         = 3;
        lReturnLenVal = 2;
      } else if( sFunction == "RTRIM" ){
        //  { "RTRIM",     'C',      2,   1 },
        cReturnType   = 'C';
        iCalc         = 2;
        lReturnLenVal = 1;
      }
    }  else if( cFunc1 == 'S' ){
      // std::cout << "==S\n";
      if( sFunction == "SPACE" ){
        // { "SPACE",     'C',      3,   1 },
        cReturnType   = 'C';
        iCalc         = 3;
        lReturnLenVal = 1;
      } else if( sFunction == "SQRT" ){
        //  { "SQRT",      'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      } else if( sFunction == "STOD" ){
        // { "STOD",      'D',      1,   8 },
        cReturnType   = 'D';
        iCalc         = 1;
        lReturnLenVal = 8;
      } else if( sFunction == "STR" ){
        //  { "STR",       'C',      6,  10 },
        cReturnType   = 'C';
        iCalc         = 6;
        lReturnLenVal = 10;
      } else if( sFunction == "STRZERO" ){
        // { "STRZERO",   'C',      3,   2 },
        cReturnType   = 'C';
        iCalc         = 3;
        lReturnLenVal = 2;
      } else if( sFunction == "SUBSTR" ){
        //  { "SUBSTR",    'C',      3,   3 },
        cReturnType   = 'C';
        iCalc         = 3;
        lReturnLenVal = 3;
      }
    }  else {
      //  std::cout << ">S\n";
      if( sFunction == "TRIM" ){
        //  { "TRIM",      'C',      2,   1 },
        cReturnType   = 'C';
        iCalc         = 2;
        lReturnLenVal = 1;
      } else if( sFunction == "UPPER" ){
        // { "UPPER",     'C',      2,   1 },
        cReturnType   = 'C';
        iCalc         = 2;
        lReturnLenVal = 1;
      } else if( sFunction == "VAL" ){
        // { "VAL",       'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 3;
      } else if( sFunction == "YEAR" ){
        //  { "YEAR",      'N',      1,   4 },
        cReturnType   = 'N';
        iCalc         = 1;
        lReturnLenVal = 4;
      }
    }
  }
  if( cReturnType == 0x00 )
    return XB_INVALID_FUNCTION;
  else
    return XB_NO_ERROR;
}
#endif
/*************************************************************************/
//! @brief Cross platform sleep function.
/*!
  \param lMillisecs Milliseconds to sleep.
*/
void xbXBase::xbSleep( xbInt32 lMillisecs ){
  #ifdef WIN32 
  Sleep( lMillisecs );
  #else
  usleep( (xbInt64) lMillisecs * 1000 );
  #endif

}
/***********************************************************************/
//! @brief Cross memcmp function.
/*!
  \param s1 Left operand to compare.
  \param s2 Right operand to compare.
  \param n Number of bytes to compare.
  \returns 1 s1 > s2<br>
           0 s1 == s2<br>
           -1 s1 < s2
*/
xbInt16 xbXBase::xbMemcmp( const unsigned char *s1, const unsigned char *s2, size_t n ){
  // The standard memcmp function was found not to behave the same across all platforms
  for( size_t i = 0; i < n; i++ ){
    if(  s1[i] > s2[i] ) 
      return 1;
    else if( s1[i] < s2[i] ) 
      return -1;
  }
  return 0;
}

/***********************************************************************/
//! @brief Open highest qualified class available for dbf file.
/*!
  This routine opens the highest available version of the dbf file.
  Defaults to XB_READ_WRITE and XB_MULTI_USER mode.
  \returns param dbf - Output pointer to dbf file opened or null if error
*/
xbDbf* xbXBase::Open( const xbString &sTableName, xbInt16 &iRc ){
  return Open( sTableName, "", XB_READ_WRITE, XB_MULTI_USER, 0, iRc );
}
/***********************************************************************/
//! @brief Open highest qualified class available for dbf file.
/*!
  This routine can open various versions of the dbf file dependent on the iVersion field

  \param sTableName - Table name to open.
  \param sAlias - Optional alias name.
  \param iOpenMode - XB_READ_WRITE or XB_READ
  \param iShareMode - XB_SINGLE_USER or XB_MULTI_USER
  \param iRequestVersion 0 - Highest available
                         4 - Version  four dbf
                         3 - Version three dbf
  \param iRc - Return code from open request
  \returns param dbf - Output pointer to dbf file opened or null if error
*/


xbDbf* xbXBase::Open( const xbString &sTableName, const xbString &sAlias, xbInt16 iOpenMode, 
                      xbInt16 iShareMode, xbInt16 iRequestVersion, xbInt16 &iRc ){

  xbInt16 iErrorStop = 0;
  xbDbf * pDbf = 0;
  iRc = 0;
  xbString sFqFileName;

  try{

    if( sTableName.Len() == 0 ){
      iErrorStop = 10;
      iRc = XB_FILE_NOT_FOUND;
      throw iRc;
    }
    xbFile *f = new xbFile(this);
    f->SetFileName( sTableName );
    if(( iRc = f->FileExists( f->GetFqFileName())) != xbTrue ){
      iErrorStop = 20;
      iRc = XB_FILE_NOT_FOUND;
      throw iRc;
    }
    unsigned char cFileTypeByte;
    if(( iRc = f->GetXbaseFileTypeByte( f->GetFqFileName(), cFileTypeByte )) != XB_NO_ERROR ){
      iErrorStop = 30;
      throw iRc;
    }

    xbInt16 iTblVsn = f->DetermineXbaseTableVersion( cFileTypeByte );
    f->xbFclose();
    sFqFileName.Set( f->GetFqFileName() );
    delete f;

    if( iTblVsn == 4 && ( iRequestVersion == 0 || iRequestVersion == 4 )){
      #ifdef XB_DBF4_SUPPORT
        pDbf = new xbDbf4( this );
        iRc = pDbf->Open( sFqFileName, sAlias, iOpenMode, iShareMode );        
      #else
        // std::cout << "Dbase IV file support not build into library. See XB_DBF4_SUPPORT" << std::endl;
        iErrorStop = 40;
        iRc = XB_FILE_TYPE_NOT_SUPPORTED;
        throw iRc;
      #endif
    }
    else if( iTblVsn == 3 && ( iRequestVersion == 0 || iRequestVersion == 3 )){

      #ifdef XB_DBF3_SUPPORT
        pDbf = new xbDbf3( this );
        iRc = pDbf->Open( sFqFileName, sAlias, iOpenMode, iShareMode );  
      #else
        //std::cout << "Dbase III file support not build into library. See XB_DBF3_SUPPORT" << std::endl;
        iErrorStop = 50;
        iRc = XB_FILE_TYPE_NOT_SUPPORTED;
        throw iRc;
      #endif

    } else {
      iErrorStop = 60;
      iRc = XB_FILE_TYPE_NOT_SUPPORTED;
      throw iRc;
    }

    if( iRc != XB_NO_ERROR ){
      iErrorStop = 70;
      throw iRc;
    }
  }
  catch (xbInt16 iRc ){

    std::cout << "Openerror\n";

    xbString sMsg;
    sMsg.Sprintf( "xbxbase::Open() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, iRc );
    WriteLogMessage( sMsg.Str() );
    WriteLogMessage( GetErrorMessage( iRc ));

  }
  return pDbf;

}

xbInt16 xbXBase::OpenHighestVersion( const xbString &sTableName, const xbString &sAlias, 
    xbDbf **dbf )
{

  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  
  xbDbf * pwDbf;
  
  try{
    xbFile f(this);
    if( sTableName.Len() == 0 ){
      iErrorStop = 10;
      iRc = XB_FILE_NOT_FOUND;
      throw iRc;
    }
    f.SetFileName( sTableName );
    if(( iRc = f.FileExists( f.GetFqFileName() )) != xbTrue ){
      iErrorStop = 20;
      iRc = XB_FILE_NOT_FOUND;
      throw iRc;
    }
    unsigned char cFileTypeByte;
    if(( iRc = f.GetXbaseFileTypeByte( f.GetFqFileName(), cFileTypeByte )) != XB_NO_ERROR ){
      iErrorStop = 30;
      return iRc;
    }
    if( f.DetermineXbaseTableVersion( cFileTypeByte ) == 4 ){
      #ifdef XB_DBF4_SUPPORT
        pwDbf = new xbDbf4( this );
        pwDbf->Open( f.GetFqFileName(), sAlias );
        *dbf = pwDbf;
        pwDbf = 0;
      #else
        // std::cout << "Dbase IV file support not build into library. See XB_DBF4_SUPPORT" << std::endl;
        iErrorStop = 40;
        iRc = XB_FILE_TYPE_NOT_SUPPORTED;
        throw iRc;
      #endif

    } else if( f.DetermineXbaseTableVersion( cFileTypeByte ) == 3 ){
      #ifdef XB_DBF3_SUPPORT
        *dbf = new xbDbf3( this );
      #else
        //std::cout << "Dbase III file support not build into library. See XB_DBF3_SUPPORT" << std::endl;
        iErrorStop = 50;
        iRc = XB_FILE_TYPE_NOT_SUPPORTED;
        throw iRc;
      #endif

    } else {
      iErrorStop = 60;
      iRc = XB_FILE_TYPE_NOT_SUPPORTED;
      throw iRc;
    }

  }
  catch (xbInt16 iRc ){

    std::cout << "OpenHighestVersion error\n";

    xbString sMsg;
    sMsg.Sprintf( "xbxbase::OpenHighestVersion() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, iRc );
    WriteLogMessage( sMsg.Str() );
    WriteLogMessage( GetErrorMessage( iRc ));

  }
  return iRc;
}
/***********************************************************************/
}        /* namespace */