summaryrefslogtreecommitdiff
path: root/src/core/xbixbase.cpp
blob: 8b64fd6923706d5215d81408b4140a67b7e1bb27 (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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/* xbixbase.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

   Base index class

*/

#include "xbase.h"
#ifdef XB_INDEX_SUPPORT

namespace xb{

/***********************************************************************/
//! @brief Class constructor.
/*!
  /param dbf Pointer to dbf instance.
*/
xbIx::xbIx( xbDbf *dbf ) : xbFile( dbf->GetXbasePtr()) {
  this->dbf = dbf;
  vpCurTag  = NULL;
  cNodeBuf  = NULL;
  bLocked   = xbFalse;
}
/***********************************************************************/
//! @brief Class Destructor.
xbIx::~xbIx(){}


/***********************************************************************/
//! @brief Add Keys for record number
/*!
  For a given a record number, add keys to each tag in the index file
  if it was updated

  \param ulRecNo Record number to add keys for
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbIx::AddKeys( xbUInt32 ulRecNo ){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbInt16 i = 0;
  xbInt16 iKeySts;

  try{
    void * vpTag;
    xbInt16 iTagCount = GetTagCount();

    for( i = 0; i < iTagCount; i++ ){
      vpTag = GetTag( i );
      iKeySts = GetKeySts( vpTag );

      if( iKeySts == 1 || iKeySts == 2 ){
        if(( iRc = UpdateTagKey( 'A', vpTag, ulRecNo )) != XB_NO_ERROR ){
          iErrorStop = 100;
          throw iRc;
        }
      }
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbIx::AddKeys() Exception Caught. Error Stop = [%d] iRc = [%d] Tag=[%d]", iErrorStop, iRc, i );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/***********************************************************************/
//! @brief Allocate memory for index node.
/*!
  Allocate an index node.

  \param ulBufSize Size of buffer to allocate
  \returns null on error<br>Pointer to newly allocated xbIxNode on success
*/
xbIxNode * xbIx::AllocateIxNode( xbUInt32 ulBufSize, xbInt16 )
{
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{
    xbIxNode * pNode = (xbIxNode *) calloc( 1, sizeof( xbIxNode ));
    if( pNode == NULL ){
      iErrorStop = 100;
      iRc = XB_NO_MEMORY;
      throw iRc;
    }
    if( ulBufSize == 0 )
      ulBufSize = GetBlockSize();

    pNode->ulBufSize = ulBufSize;
    pNode->cpBlockData = (char *) calloc( 1, ulBufSize );
    if( pNode->cpBlockData == NULL ){
      free( pNode );
      iErrorStop = 110;
      iRc = XB_NO_MEMORY;
      throw iRc;
    }
    return pNode;
 }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbIx::AllocateIxNode() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return NULL;
}


/***********************************************************************/
//! @brief Binary search for given value on an index node.
/*!

  Binary search for key lookups
  \param cKeyType Key type
  \param npNode Pointer to index node for search
  \param lKeyItemLen Lenth of key plus pointer values
  \param vpKey Pointer to key value
  \param lSearchKeyLen length of key to search
  \param iCompRc output return code from the CompareKey routine.  CompareKey returns an 
          integer value less than, equal to or greater than zero in when comparing values

  \param bDescending xbTrue for descending index key lookup.<br>
                     xbFalse for ascending index key lookup.
  \return The position in the node the key was found, if multiples it returns the first occurrence.
          If the key is not found, it returns the slot it should be in.
*/

xbInt16 xbIx::BSearchBlock( char cKeyType, xbIxNode *npNode, xbInt32 lKeyItemLen, const void *vpKey, 
                        xbInt32 lSearchKeyLen, xbInt16 &iCompRc, xbBool bDescending ) const {
  xbInt32 lLo = 0;
  xbInt32 lHi  = 0;
  xbInt32 lMid = 0;
  xbInt32 lKeyCnt = GetKeyCount( npNode );


  if( !bDescending ){
    lHi = lKeyCnt - 1;

    while( lLo <= lHi ){
      lMid = (lLo + lHi) / 2;
      iCompRc = CompareKey( cKeyType, GetKeyData( npNode, lMid, lKeyItemLen ), vpKey, (size_t) lSearchKeyLen );
      if( iCompRc > 0 ) 
        lHi = lMid - 1;
      else if( iCompRc < 0 )
        lLo = lMid + 1; 
      else{  // found  match, look for leftmost occurrence

        xbInt32 lFoundPos = lMid;
        lMid--;
        while( lMid >= 0 && CompareKey( cKeyType, GetKeyData( npNode, lMid, lKeyItemLen ), vpKey, (size_t) lSearchKeyLen ) == 0 ){
          lFoundPos = lMid;
          lMid--;
        }
        iCompRc = 0;
        lLo = lFoundPos;
        lHi = -1;
      }
    }
    // update the compare key results
    if( lMid != lLo ){
      if( lLo >= lKeyCnt )
        iCompRc = 1;
      else
        iCompRc = CompareKey( cKeyType, GetKeyData( npNode, lLo, lKeyItemLen ), vpKey, (size_t) lSearchKeyLen );
    }
    return (xbInt16) lLo;

  }  else {   // descending key

    lLo = lKeyCnt - 1;
    while( lLo >= lHi && lHi != -1 ){
      lMid = (lLo + lHi) / 2;
      iCompRc = CompareKey( cKeyType, GetKeyData( npNode, lMid, lKeyItemLen ), vpKey, (size_t) lSearchKeyLen );

      if( iCompRc > 0 ) {
        lHi = lMid + 1;
      }
      else if( iCompRc < 0) {
        lLo = lMid - 1;
      }
      else{  // found  match, look for leftmost occurrence

        xbInt32 lFoundPos = lMid;
        lMid--;
        while( lMid >= 0 && CompareKey( cKeyType, GetKeyData( npNode, lMid, lKeyItemLen ), vpKey, (size_t) lSearchKeyLen ) == 0 ){
          lFoundPos = lMid;
          lMid--;
        }
        iCompRc = 0;
        lHi = lFoundPos;
        lLo = -1;
      }
    }

    // std::cout << "BSB1 lo = " << lLo << " mid = " << lMid << " hi = " << lHi << " keycnt = " << lKeyCnt << " iCompRc = " << iCompRc << "\n";    // key=" << (char *) vpKey << "\n";
    if( lLo < 0 && iCompRc < 0 ) 
      iCompRc = 1;
    else if( iCompRc != 0 ) {
      iCompRc = CompareKey( cKeyType, GetKeyData( npNode, (lLo < 0 ? 0 : lLo), lKeyItemLen ), vpKey, (size_t) lSearchKeyLen );
    }
    // std::cout << "BSB2 lo = " << lLo << " mid = " << lMid << " hi = " << lHi << " keycnt = " << lKeyCnt << " iCompRc = " << iCompRc << "\n";    // key=" << (char *) vpKey << "\n";
    return (xbInt16) lHi;
  }

  // should never get here
  //  return (xbInt16) 0;
}

/***********************************************************************/
//! @brief Check for duplicate keys.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::CheckForDupKeys(){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbInt16 i = 0;

  try{
    void * vpTag;
    xbInt16 iTagCount = GetTagCount();
    for( i = 0; i < iTagCount; i++ ){
      vpTag = GetTag( i );
      if(( iRc = CheckForDupKey( vpTag )) < XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      }
    }
  }
  catch (xbInt16 iRc ){
    if( iRc != XB_KEY_NOT_UNIQUE ){
      xbString sMsg;
      sMsg.Sprintf( "xbIxBase::CheckForDupKeys() Exception Caught. Error Stop = [%d] iRc = [%d] Tag=[%d]", iErrorStop, iRc, i );
      xbase->WriteLogMessage( sMsg );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;
}
/***********************************************************************/
//! @brief Close index file.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbIx::Close(){

  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;

  try{
    if(( iRc = xbFclose()) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbIx::Close() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/***********************************************************************/
//! @brief Compare keys.
/*!
  \param cKeyType C - Character compare.<br>
                  N - Numeric BCD compare.<br>
                  D - Numeric compare.<br>
                  F - Numeric compare.<br>
  \param v1 Left compare.<br>v2 - Right Compare.
  \param iSearchKeyLen Length of key compare.
  \returns 1 - Left operand is greater then right operand.<br>
           0 - Left operand is equal to right operand.<br>
          -1 - Left operand is less than right operand.
*/
inline xbInt16 xbIx::CompareKey( char cKeyType, const void *v1, const void *v2, size_t iSearchKeyLen ) const{
  if( cKeyType == 'C' ){                // character compare
    return memcmp( v1, v2, iSearchKeyLen );
  } else if( cKeyType == 'N' ){         // numeric bcd compare, mdx bcd numeric indices
    xbBcd bcdk1( v1 );
    return bcdk1.Compare( v2 );
  } else if( cKeyType == 'D' || cKeyType == 'F' ){         // julian date compare, ndx float numeric indices
    xbDouble *d1 = (xbDouble *) v1;
    xbDouble *d2 = (xbDouble *) v2;
    if( *d1 < *d2 )
      return -1;
    else if( *d1 > *d2 )
      return 1;
    else
      return 0;
  } else {
//    std::cout << "Unhandled key type [" << cKeyType << "]\n";
  }
  return XB_NO_ERROR;
}
/***********************************************************************/
//! @brief Create Keys for record number
/*!
  \param iOpt 0 Build a key for FindKey usage, only rec buf 0.<br>
              1 Append Mode, Create key for an append, only use rec buf 0, set updated switch.<br>
              2 Update Mode, Create old version and new version keys, check if different, set update switch appropriately.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::CreateKeys( xbInt16 iOpt ) {
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbInt16 i = 0;

  try{
    void * vpTag;
    xbInt16 iTagCount = GetTagCount();

    for( i = 0; i < iTagCount; i++ ){
      vpTag = GetTag( i );
      if(( iRc = CreateKey( vpTag, iOpt )) < XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      }
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbIx::CreateKeys() Exception Caught. Error Stop = [%d] iRc = [%d] Tag=[%d]", iErrorStop, iRc, i );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return XB_NO_ERROR;
}
/***********************************************************************/
//! @brief Delete keys for record number
/*!
  Delete keys to each tag in the index file if it was updated as determined
  by CreateKeys function

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
//xbInt16 xbIx::DeleteKeys( xbUInt32 ulRecNo ){

xbInt16 xbIx::DeleteKeys(){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbInt16 i = 0;

  try{
    void * vpTag;
    xbInt16 iTagCount = GetTagCount();

    for( i = 0; i < iTagCount; i++ ){
      vpTag = GetTag( i );
      if( GetKeySts( vpTag ) > 1 ){    // 0 = no update 1 = add 2 = update, 3 = delete
        if(( iRc = UpdateTagKey( 'D', vpTag )) != XB_NO_ERROR ){
          iErrorStop = 100;
          throw iRc;
        }
      }
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbIx::DeleteKeys() Exception Caught. Error Stop = [%d] iRc = [%d] Tag=[%d]", iErrorStop, iRc, i );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/***********************************************************************/
#ifdef XB_DEBUG_SUPPORT
//! @brief Dump anode for debug purposes.
/*!
  \param pNode Pointer to node to dump.
  \param iOption 0 = stdout<br>
                 1 = Syslog<br>
                 2 = Both<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbIx::DumpNode( void *, xbIxNode *pNode, xbInt16 iOption ) const
{
  xbString s;
  s.Sprintf( "Dump Node Block=[%d] CurKey=[%d]", pNode->ulBlockNo, pNode->iCurKeyNo );
  xbase->WriteLogMessage( s, iOption );

  return XB_NO_ERROR;
}
#endif   // XB_DEBUG_SUPPORT

/***********************************************************************/
//! @brief Find double key
/*!
  \param vpTag Pointer to tag to search.
  \param dKey Double value to search for.
  \param iRetrieveSw xbTrue - Retrieve the record if key found.<br>
               xbFalse - Don't retrieve record, check for key existence only.
  \returns XB_NO_ERROR - Key found.<br>
           XB_NOT_FOUND - Key not found.<br>
           <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::FindKey( void *vpTag, xbDouble dKey, xbInt16 iRetrieveSw ){
  return FindKey( vpTag, &dKey, 8, iRetrieveSw );
}
/***********************************************************************/
//! @brief Find string key
/*!
  \param vpTag Pointer to tag to search.
  \param sKey String data to search for.
  \param iRetrieveSw xbTrue - Retrieve the record if key found.<br>
               xbFalse - Don't retrieve record, check for key existence only.
  \returns XB_NO_ERROR - Key found.<br>
           XB_NOT_FOUND - Key not found.<br>
           <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16  xbIx::FindKey( void *vpTag, const xbString &sKey, xbInt16 iRetrieveSw ){
  return FindKey( vpTag, sKey.Str(), (xbInt32) sKey.Len(), iRetrieveSw );
}
/***********************************************************************/
//! @brief Find character key
/*!
  \param vpTag Pointer to tag to search.
  \param cKey String data to search for.
  \param lKeyLen Length of key to search for.
  \param iRetrieveSw xbTrue - Retrieve the record if key found.<br>
               xbFalse - Don't retrieve record, check for key existence only.
  \returns XB_NO_ERROR - Key found.<br>
           XB_NOT_FOUND - Key not found.<br>
           <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16  xbIx::FindKey( void *vpTag, const char *cKey, xbInt32 lKeyLen, xbInt16 iRetrieveSw ){
  return FindKey( vpTag, (void *) cKey, lKeyLen, iRetrieveSw );
}
/***********************************************************************/
//! @brief Find bcd key
/*!
  \param vpTag Pointer to tag to search.
  \param bcd BCD data to search for.
  \param iRetrieveSw xbTrue - Retrieve the record if key found.<br>
               xbFalse - Don't retrieve record, check for key existence only.

  \returns XB_NO_ERROR - Key found.<br>
           XB_NOT_FOUND - Key not found.<br>
           <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16  xbIx::FindKey( void *vpTag, const xbBcd &bcd, xbInt16 iRetrieveSw ){
  return FindKey( vpTag, bcd.GetBcd(), 12, iRetrieveSw );
}
/***********************************************************************/
//! @brief Find date key
/*!
  \param vpTag Pointer to tag to search.
  \param dtKey Date data to search for.
  \param iRetrieveSw xbTrue - Retrieve the record if key found.<br>
               xbFalse - Don't retrieve record, check for key existence only.
  \returns XB_NO_ERROR - Key found.<br>
           XB_NOT_FOUND - Key not found.<br>
           <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16  xbIx::FindKey( void *vpTag, const xbDate &dtKey, xbInt16 iRetrieveSw ){
  xbDouble d = (xbDouble) dtKey.JulianDays();
  return FindKey( vpTag, &d, 8, iRetrieveSw );
}
/***********************************************************************/
//! @brief Free all nodes in a linked list.
/*!
  \param np Pointer to first node in linked list to free.
  \returns NULL.
*/
xbIxNode *xbIx::FreeNodeChain( xbIxNode *np ){

  // routine returns NULL
  if( np ){
    // free memory for a given chain of nodes
    xbIxNode * np2;

    // Clear the previous node's next pointer
    if( np->npPrev )
      np->npPrev->npNext = NULL;

    // Clear out the tree
    while( np ){
      np2 = np->npNext;
      NodeFree( np );
      np = NULL;
      np = np2;
    }
  }
  return NULL;
}
/***********************************************************************/
//! @brief Read block for block number.
/*!
  Routine to read a node/block out of an index file and store in xbIxNode structure
  \param vpTag Pointer to tag.
  \param ulBlockNo Block number to read off disk.
  \param iOpt 
    0 = Node is read into block buffer, not added to the node chain<br>
    1 = Node is read into new xbIxNode, then added to the node chain, and sets CurNode with new node<br>
    2 = Node is read into new xbIxNode, not added to the node chain<br>
        CurNode points to new node<br>
   \param ulAddlBuf Additional buffer size added to memory 
   \returns <a href="xbretcod_8h.html">Return Codes</a>

*/

xbInt16 xbIx::GetBlock( void *vpTag, xbUInt32 ulBlockNo, xbInt16 iOpt, xbUInt32 ulAddlBuf ){
  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  xbIxNode *np = NULL;
  try{

    if( !vpTag && iOpt == 1 ){
      iErrorStop = 100;
      throw iRc;
    }

    // set target location of block read to read
    char  *cp;
    if( iOpt == 0 )
      cp = cNodeBuf;
    else{
      if(( np = AllocateIxNode(GetBlockSize() + ulAddlBuf )) == NULL ){
        iErrorStop = 110;
        iRc = XB_NO_MEMORY;
        throw( iRc );
      }
      cp = np->cpBlockData;
    }
    if(( iRc = ReadBlock( ulBlockNo, GetBlockSize(), cp )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }
    if( iOpt == 0 )
      return iRc;
    np->ulBlockNo = ulBlockNo;
    np->iCurKeyNo = 0;
    if( iOpt == 1 )
      AppendNodeChain( vpTag, np );
    else if( iOpt == 2 ){
      std::cout << "Future use stub. xbIxbase::GetBlock() option 2 not coded.\n";
      iErrorStop = 130;
      iRc = XB_INVALID_OPTION;
      throw iRc;
      // SetCurNode( vpTag, np );
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbIx::GetBlock() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
    if( np ) NodeFree( np );
  }
  return iRc;
}

/***********************************************************************/
//! @brief Get pointer to current tag.
/*!
  \returns Pointer to current tag.
*/

void *xbIx::GetCurTag() const {
  return vpCurTag;
}
/***********************************************************************/
//! @brief Get pointer to dbf.
/*!
  \returns Pointer to dbf.
*/
xbDbf *xbIx::GetDbf() const {
  return this->dbf;
}
/***********************************************************************/
//! @brief Get the first key for the current tag.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetFirstKey(){
  return GetFirstKey( vpCurTag, 0 );
}

/***********************************************************************/
//! @brief Get the first key for a given tag.
/*!
  \param vpTag Tag for get first key operation.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetFirstKey( void *vpTag ){
  return GetFirstKey( vpTag, 0 );
}
/***********************************************************************/
//! @brief Get the last key for the current tag.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetLastKey(){
  return GetLastKey( 0, vpCurTag, 0 );
}
/***********************************************************************/
//! @brief Get the last key for a given tag.
/*!
  \param vpTag Tag for get last key operation.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetLastKey( void *vpTag ){
  return GetLastKey( 0, vpTag, 0 );
}

/***********************************************************************/
//! @brief Get the file lock status.
/*!
  \returns xbTrue - Index file is locked.<br>xbFalse - Index file is not locked.
*/
xbBool xbIx::GetLocked() const {
  return bLocked;
}

/***********************************************************************/
//! @brief Get the key count for number of keys on a node.
/*!
  \param np Given node for key count.
  \returns Number of keys on the node.
*/
xbInt32 xbIx::GetKeyCount( xbIxNode *np ) const {
  // assumes the first four bytes of the block is a four byte number
  // representing the number of keys contained on the block
  return eGetInt32( np->cpBlockData );
}
/***********************************************************************/
//! @brief Get key data for a given key number.
/*!
  \param np Given node for key retrieval.
  \param iKeyNo Which key to pull.
  \param iKeyItemLen Length of key plus pointers.
  \returns Pointer to a given key.
*/
char * xbIx::GetKeyData( xbIxNode *np, xbInt16 iKeyNo, xbInt16 iKeyItemLen ) const {
  if( !np ) return NULL;
  char *p = np->cpBlockData;
  xbUInt32 ulKeyCnt = eGetUInt32( p );
  if( iKeyNo < 0 || iKeyNo > (xbInt16) ulKeyCnt ) return NULL;
  xbInt16 iOffset = 12 + (iKeyNo * iKeyItemLen);
  p+=iOffset;
  return p;
}
/***********************************************************************/
//! @brief Get the next key for the current tag.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetNextKey(){
  return GetNextKey( vpCurTag, 0 );
}
/***********************************************************************/
//! @brief Get the next key for the given tag.
/*!
  \param vpTag Tag for next key operation.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetNextKey( void *vpTag ){
  return GetNextKey( vpTag, 0 );
}
/***********************************************************************/
//! @brief Get the prev key for the current tag.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetPrevKey(){
  return GetPrevKey( vpCurTag, 0 );
}
/***********************************************************************/
//! @brief Get the previous key for the given tag.
/*!
  \param vpTag Tag for previous key operation.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::GetPrevKey( void *vpTag ){
  return GetPrevKey( vpTag, 0 );
}
/***********************************************************************/
//! @brief Free an index node
/*!
  \param ixNode Pointer to index node to free.
  \returns void
*/
void xbIx::NodeFree( xbIxNode *ixNode ){
  if( ixNode ){
    if( ixNode->cpBlockData ){
      free( ixNode->cpBlockData );
      ixNode->cpBlockData = NULL;
    }
    free( ixNode );
    ixNode = NULL;
  }
}
/***********************************************************************/
//! @brief Open an index file.
/*!
  MDX files are opened automatically and don't need opened.
  NDX files that are associated with the DBF file are opened automatically.

  Non production indexes that haven't been opened will need to be opened to be used.
  \param sFileName Index file name to open.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbIx::Open( const xbString & sFileName ){

  // There are no locking requirements when opening an NDX index
  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;

  try{
    /* copy the file name to the class variable */
    this->SetFileName( sFileName );

    if( !FileExists()){
      iErrorStop = 100;
      iRc = XB_FILE_NOT_FOUND;
      throw iRc;
    }
    /* open the file */
    if(( iRc = xbFopen( dbf->GetOpenMode(), dbf->GetShareMode())) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }
    if(( iRc = ReadHeadBlock()) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }
    SetCurTag( (xbInt16) 0 );     // default the first tag as the current tag
  }

  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbIx::Open( %s ) Exception Caught. Error Stop = [%d] iRc = [%d]", sFileName.Str(), iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
/***********************************************************************/
//! @brief Set the current tag.
/*!
  \param vpCurTag Pointer to tag to set as current.
  \returns void
*/
void xbIx::SetCurTag( void *vpCurTag ){
  this->vpCurTag = vpCurTag;
}
/***********************************************************************/
//! @brief Set the dbf pointer.
/*!
  \param dbf Dbf pointer to set.
  \returns void
*/
void xbIx::SetDbf( xbDbf *dbf ){
  this->dbf = dbf;
}
/***********************************************************************/
//! @brief Set the file lock status.
/*!
  \param bLocked xbTrue - Set to locked.<br>xbFalse - Set to unlocked.
  \returns void
*/
void xbIx::SetLocked( xbBool bLocked ){
  this->bLocked = bLocked;
}
/***********************************************************************/
}              /* namespace       */
#endif         /*  XB_INDEX_SUPPORT */