summaryrefslogtreecommitdiff
path: root/src/core/xbdbf.cpp
blob: 3d133690d95dc7fb2416cd94b8addd80c6b0b745 (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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
/* xbdbf.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 Constructor
/*!
  \param x Pointer to xbXbase
*/
xbDbf::xbDbf( xbXBase * x ) : xbFile( x ){
  xbase         = x;
  SchemaPtr     = NULL;
  RecBuf        = NULL;
  RecBuf2       = NULL;
  InitVars();
}
/************************************************************************/
void xbDbf::InitVars()
{
  iNoOfFields      = 0;
  iDbfStatus       = XB_CLOSED;
  ulCurRec         = 0L;
  cVersion         = 0x00;
  cUpdateYY        = 0x00;
  cUpdateMM        = 0x00;
  cUpdateDD        = 0x00;
  ulNoOfRecs       = 0L;
  uiHeaderLen      = 0x00;
  uiRecordLen      = 0x00;
  cTransactionFlag = 0x00;
  cEncryptionFlag  = 0x00;
  cIndexFlag       = 0x00;
  cLangDriver      = 0x00;
  iFileVersion     = 0;            /* Xbase64 file version */
  iAutoCommit      = -1;

  SetFileName  ( "" );
  sAlias.Set   ( "" ); 
  SetDirectory ( GetDataDirectory());

  #ifdef XB_LOCKING_SUPPORT
  iLockFlavor      = -1;
  bTableLocked     = xbFalse;
  bHeaderLocked    = xbFalse;
  ulAppendLocked   = 0;
  SetAutoLock( -1 );
  lloRecLocks.SetDupKeys( xbFalse );
  #endif // XB_LOCKING_SUPPORT

  #ifdef XB_INDEX_SUPPORT
  ixList        = NULL;
  pCurIx        = NULL;
  vpCurIxTag    = NULL;
  sCurIxType    = "";
  ClearTagList();
  #endif // XB_INDEX_SUPPORT

  #ifdef XB_MEMO_SUPPORT
  Memo          = NULL;
  #endif // XB_MEMO_SUPPORT

  #ifdef XB_INF_SUPPORT
  llInfData.Clear();
  #endif // XB_INF_SUPPORT
}

/************************************************************************/
//! @brief Destructor
xbDbf::~xbDbf(){

  // is there is an uncommited update, discard it.
  // as we don't know if it is an append or an update
  if( iDbfStatus == XB_UPDATED )
    Abort();

  if( iDbfStatus != XB_CLOSED )
    Close();

  if( SchemaPtr ){
    free( SchemaPtr );
    SchemaPtr = NULL;
  }
  if( RecBuf ){
    free( RecBuf );
    RecBuf = NULL;
  }
  if( RecBuf2){
    free( RecBuf2 );
    RecBuf2 = NULL;
  }
  Close();
}
/************************************************************************/
//! @brief Abort any uncommited changes for the current record buffer.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::Abort(){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{
    if( iDbfStatus == XB_UPDATED ){
      #ifdef XB_MEMO_SUPPORT
      if( MemoFieldsExist()){
        if(( iRc = Memo->Abort()) != XB_NO_ERROR ){
          iErrorStop = 100;
          throw iRc;
        }
      }
      #endif
      memcpy( RecBuf, RecBuf2, uiRecordLen );
      iDbfStatus = XB_OPEN;
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::Abort() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Add an index to the internal list of indices for this table.
/*!
    The index list is used during any table update process to update any open
    index file.  Index files can contain one or more tags.

  \param ixIn Pointer to index object for a given index file.
  \param sFmt NDX or MDX.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
  
*/

xbInt16 xbDbf::AddIndex( xbIx * ixIn, const xbString &sFmt ){

  xbIxList *ixt;   // this
  if(( ixt = (xbIxList *) malloc( sizeof( xbIxList ))) == NULL )
    return XB_NO_ERROR;

  ixt->ix   = ixIn;
  ixt->next = NULL;
  ixt->sFmt = new xbString( sFmt );
  ixt->sFmt->ToUpperCase();

  if( ixList ){
    xbIxList *ixn = ixList;   // next
    while( ixn->next ){
      ixn = ixn->next;
    }
    ixn->next = ixt;
  } else {
    ixList = ixt;
  }
  return XB_NO_ERROR;
}
#endif // XB_INDEX_SUPPORT


/************************************************************************/
//! @brief Append the current record to the data file.
/*!
  This method attempts to append the contents of the current record buffer
  to the end of the DBF file, updates the file date, number of records in the file
  and updates any open indices associated with this data file.<br>

  To add a record, an application would typically blank the record buffer,
  update various fields in the record buffer, then append the record.<br>

  The append method performs the following tasks:<br>
  1)  Create new index key values<br>
  2)  Lock the table<br>
  3)  Lock append bytes<br>
  4)  Lock indices<br>
  5)  Read the dbf header<br>
  6)  Check for dup keys<br>
  7)  Calc last update date, no of recs<br>
  8)  Add keys<br>
  9)  Unlock indices<br>
  10) Update file header<br>
  11) Unlock file header<br>
  12) Append record<br>
  13) Unlock append bytes<br>

Note: Locking memo files is not needed as the  memo file updates are handled outside of the append method.<br>

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::AppendRecord(){
  xbInt16 iErrorStop = 0;
  xbInt16 iRc = XB_NO_ERROR;
  xbUInt32 ulSaveCurRec = 0;

  try{
    #ifdef XB_INDEX_SUPPORT
    xbIxList *ixList = GetIxList();
    // do this step first before anything is locked, reduce lock time as much as possible
    while( ixList ){

      // std::cout << "xbDbf::CreateKeys(x)\n";
      if(( iRc = ixList->ix->CreateKeys( 1 )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      }

      ixList = ixList->next;
    }
    #endif // XB_INDEX_SUPPORT

    // lock everything up for an update
    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockHeader( XB_LOCK )) != XB_NO_ERROR ){
        if( iRc == XB_LOCK_FAILED )  {
          return iRc;
        } else {
          iErrorStop = 110;
          throw iRc;
        }
      }
      if(( iRc = LockAppend( XB_LOCK )) != XB_NO_ERROR ){
        if( iRc == XB_LOCK_FAILED ){
          LockHeader( XB_UNLOCK );
          return iRc;
        } else {
         iErrorStop = 120;
         throw iRc;
        }
      }

      #ifdef XB_INDEX_SUPPORT
      if(( iRc = LockIndices( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 130;
        throw iRc;
      }
      #endif // XB_INDEX_SUPPORT

    }
    #endif // XB_LOCKING_SUPPORT
    if(( iRc = ReadHeader( 1, 1 )) != XB_NO_ERROR ){
      iErrorStop = 140;
      throw iRc;
    }
    #ifdef XB_INDEX_SUPPORT
    ixList = GetIxList();

    while( ixList ){
      if(( iRc = ixList->ix->CheckForDupKeys()) != 0 ){
        if( iRc < 0 ){
          iErrorStop = 150;
          throw iRc;
        }
        return XB_KEY_NOT_UNIQUE;
      }
      ixList = ixList->next;
    }

    #endif // XB_INDEX_SUPPORT

    // calculate the latest header information
    xbDate d;
    cUpdateYY = (char) d.YearOf() - 1900;
    cUpdateMM = (char) d.MonthOf();
    cUpdateDD = (char) d.DayOf( XB_FMT_MONTH );
    ulSaveCurRec = ulCurRec;
    ulNoOfRecs++;
    ulCurRec  = ulNoOfRecs;

    #ifdef XB_INDEX_SUPPORT


    ixList = GetIxList();
    while( ixList ){
      if(( iRc = ixList->ix->AddKeys( ulCurRec )) != XB_NO_ERROR ){
        iErrorStop = 160;
        throw iRc;
      }
      ixList = ixList->next;
    }

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock ){
      if(( iRc = LockIndices( XB_UNLOCK )) != XB_NO_ERROR ){
        iErrorStop = 170;
        throw iRc;
      }
    }
    #endif     // XB_LOCKING_SUPPORT
    #endif     // XB_INDEX_SUPPORT

    // rewrite the header record
    if(( iRc = WriteHeader( 1, 1 )) != XB_NO_ERROR ){
      iErrorStop = 180;
      throw iRc;
    }

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock ){
      if(( iRc = LockHeader( XB_UNLOCK )) != XB_NO_ERROR ){
        iErrorStop = 190;
        throw iRc;
      }
    }
    #endif

    // write the last record
    if(( iRc = xbFseek( (uiHeaderLen+((xbInt64)(ulNoOfRecs-1)*uiRecordLen)), 0 )) != XB_NO_ERROR ){
      iErrorStop = 200;
      throw iRc;
    }

    if(( iRc = xbFwrite( RecBuf, uiRecordLen, 1 )) != XB_NO_ERROR ){
      iErrorStop = 210;
      throw iRc;
    }

    // write the end of file marker
    if(( iRc = xbFputc( XB_CHAREOF )) != XB_NO_ERROR ){
      iErrorStop = 220;
      throw iRc;
    }

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock ){
      if(( iRc = LockAppend( XB_UNLOCK )) != XB_NO_ERROR ){
         iErrorStop = 230;
         throw( iRc );
      }
    }
    #endif // XB_LOCKING_SUPPORT

  }
  catch (xbInt16 iRc ){
    if( ulSaveCurRec != 0 ){
      ulCurRec = ulSaveCurRec;
      ulNoOfRecs--;
    }
    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock ){
      #ifdef XB_INDEX_SUPPORT
      LockIndices( XB_UNLOCK );
      #endif // XB_INDEX_SUPPORT
      LockAppend( XB_UNLOCK );
      LockHeader( XB_UNLOCK );
    }
    #endif // XB_LOCKING_SUPPORT

    if( iRc != XB_LOCK_FAILED && iRc != XB_KEY_NOT_UNIQUE ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::Append() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }

  if( iRc == XB_NO_ERROR )
    iDbfStatus = XB_OPEN;
  return iRc;
}

/************************************************************************/
#ifdef XB_INF_SUPPORT
//! @brief Asscoiate a non production index to a DBF file. 
/*!

  The original Dbase (TM) software supported non production indices (NDX) and production indices (MDX).
  The production indices are opened automatically when the DBF file is opened but the non-production
  indices are not.  This method is specific to the Xbas64 library and providex a means to link non production
  NDX index files to the DBF file so they will be opened automatically when the DBF file is opened.<br>

  This routine requires INF support be enabled when building the library.<br>
  This routine creates a file with the same name as the DBF file, but with an extension of INF.<br>


  \param sIxType Currently only NDX. Future versions will support additional non prod index types.
  \param sIxName The index name.
  \param iOpt 0 - Add index to .INF if not already there<br>
              1 - Remove index from .INF if there

  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/

xbInt16 xbDbf::AssociateIndex( const xbString &sIxType, const xbString &sIxName, xbInt16 iOpt ){

  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;

  try{
    xbString sIxTypeIn = sIxType;
    sIxTypeIn.Trim();
    xbString sIxNameIn = sIxName;
    sIxNameIn.Trim();

    if( sIxTypeIn != "NDX" || sIxName == "" )
      return XB_INVALID_INDEX;

    if(( iRc = LoadInfData()) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }

    // check if entry exists
    xbLinkListNode<xbString> * llN = llInfData.GetHeadNode();
    xbBool bFound = xbFalse;
    xbString s;

    while( llN && !bFound ){
      s = llN->GetKey();
      if( s.Len() > 0 ){
        if( sIxNameIn == s )
          bFound = xbTrue;
      }
      llN = llN->GetNextNode();
    }

    xbBool bUpdated = xbFalse;
    if( iOpt == 0 && !bFound ){
      s.Sprintf( "%s%c%c", sIxName.Str(), 0x0d, 0x0a );
      llInfData.InsertAtEnd( s );
      bUpdated = xbTrue;

    } else if( iOpt == 1 && bFound ){
      llInfData.RemoveByVal( s );
      bUpdated = xbTrue;
    }

    if( bUpdated ){
      if(( iRc = SaveInfData()) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw iRc;
      }
    }

 } catch( xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::AssociateIndex() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
#endif // XB_INF_SUPPORT

/************************************************************************/
//! @brief Blank the record buffer.
/*!

  This method would typically be called to initialize the record buffer before
  updates are applied to append a new record.

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::BlankRecord()
{
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{

    if( iDbfStatus == XB_CLOSED ){
      iErrorStop = 100;
      iRc = XB_NOT_OPEN;
      throw iRc;
    }

    if( iDbfStatus == XB_UPDATED ){
      if(  GetAutoCommit() == 1 ){
        if(( iRc = Commit()) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw iRc;
        }
      } else {
        if(( iRc = Abort()) != XB_NO_ERROR ){
          iErrorStop = 120;
          throw iRc;
        }
      }
    }
    ulCurRec = 0;
    memset( RecBuf,  0x20, uiRecordLen );
    memset( RecBuf2, 0x20, uiRecordLen );
  }

  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::BlankRecord() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }

  return iRc;
}
/************************************************************************/
#ifdef XB_INDEX_SUPPORT
/*!
  This method is used to check an index tag's intgerity.

  \param iTagOpt 0 - Check current tag<br>
                 1 - Check all tag<br>

  \param iOutputOpt Output message destination<br>
                 0 = stdout<br>
                 1 = Syslog<br>
                 2 = Both<br>

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::CheckTagIntegrity( xbInt16 iTagOpt, xbInt16 iOutputOpt ){
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{
    if( iTagOpt == 0 ){
      if( pCurIx )
        return pCurIx->CheckTagIntegrity( vpCurIxTag, iOutputOpt );
      else
        return XB_INVALID_TAG;

    } else {

      xbLinkListNode<xbTag *> *llN = GetTagList();
      xbTag *pTag;

      while( llN ){
        pTag = llN->GetKey();
        if(( iRc = pTag->GetIx()->CheckTagIntegrity( pTag->GetVpTag(), iOutputOpt )) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw iRc;
        }
        llN = llN->GetNextNode();
      }
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::CheckTagIntegrity() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/************************************************************************/
/*!
  This method is used to reindex / rebuild index tag.

  \param iTagOpt 0 - Reindex current tag<br>
                 1 - Reindex all tags<br>

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::Reindex( xbInt16 iTagOpt ){
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  void    *vp;

  try{
    if( iTagOpt == 0 ){
      if( pCurIx ){

        iRc = pCurIx->Reindex( &vpCurIxTag );
        if( iRc != XB_NO_ERROR ){
          iErrorStop = 100;
          throw iRc;
        }
        return iRc;
      } else {
        return XB_INVALID_TAG;
      }

    } else {

      xbLinkListNode<xbTag *> *llN = GetTagList();
      xbTag *pTag;

      while( llN ){
        pTag = llN->GetKey();
        vp   = pTag->GetVpTag();
        if(( iRc = pTag->GetIx()->Reindex( &vp )) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw iRc;
        }
        llN = llN->GetNextNode();
      }
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::Reindex() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/************************************************************************/
// @brief Clear the index tag list.
/*
  Protected method. Clears the list inf index tags.
  \returns void.
*/
void xbDbf::ClearTagList(){

  xbTag *pTag;
  xbBool bDone = xbFalse;
  while( llTags.GetNodeCnt() > 0 && !bDone ){
    if( llTags.RemoveFromFront( pTag ) != XB_NO_ERROR ){
      bDone = xbTrue;
    } else {
      if( pTag )
        delete pTag;
    }
  }
}
#endif // XB_INDEX_SUPPORT

/************************************************************************/
//! @brief Close DBF file/table.
/*!
  This routine flushes any remaining updates to disk, closes the DBF file and
  any associated memo and index files.

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::Close(){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{

    if(iDbfStatus == XB_CLOSED)
      return XB_NO_ERROR;

    else if( iDbfStatus == XB_UPDATED ){

      if(  GetAutoCommit() == 1 ){
        if(( iRc = Commit()) != XB_NO_ERROR ){
          iErrorStop = 100;
          throw iRc;
        }
      } else {
        if(( iRc = Abort()) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw iRc;
        }
     }
   }

    if(SchemaPtr){
      free( SchemaPtr );
      SchemaPtr = NULL;
    }
    if(RecBuf){
      free( RecBuf );
      RecBuf = NULL;
    }
    if(RecBuf2){
      free( RecBuf2 );
      RecBuf2 = NULL;
    }

    #ifdef XB_MEMO_SUPPORT
    if( iMemoFieldCnt > 0 ){
      Memo->CloseMemoFile();
      delete Memo;
      Memo = NULL;
    }
    #endif

    // close any open index files, remove from the ix list
    #ifdef XB_INDEX_SUPPORT
    while( ixList ){
      ixList->ix->Close();
      RemoveIndex( ixList->ix );
    }
    #endif

    if(( iRc = xbase->RemoveTblFromTblList( this )) != XB_NO_ERROR ){
      xbString sMsg;
      sMsg.Sprintf( "Alias = [%s]", sAlias.Str());
      xbase->WriteLogMessage( sMsg.Str() );
      iErrorStop = 120;
      throw iRc;
    }
    xbFclose();
    InitVars();
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::Close() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Close an open index file
/*!
   All index files are automatically closed when the DBF file is closed.
   Under normal conditions, it is not necessary to explicitly close an index file
   with this routine.  Any updates posted to a DBF file while an index is closed 
   will not be reflected in the closed index file.

  \param pIx Pointer to index object to close.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::CloseIndexFile( xbIx *pIx ){

  xbInt16 iErrorStop = 0;
  xbInt16 iRc = XB_NO_ERROR;

  try{

    // verify index is open and in the list
    xbBool bFound = xbFalse;
    xbIxList *p = GetIxList();
    while( p && !bFound ){
      if( pIx == p->ix )
        bFound = xbTrue;
      p = p->next;
    }
    if( !bFound ){
      iErrorStop = 100;
      iRc = XB_NOT_OPEN;
      throw iRc;
    }
    //  close it
    if(( iRc = pIx->Close()) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }
    //  remove it from the list
    if(( iRc = RemoveIndex( pIx )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }
    // refresh the tag list
    if(( iRc = UpdateTagList()) != XB_NO_ERROR ){
      iErrorStop = 130;
      throw iRc;
    }
    if( pIx == pCurIx ){
      pCurIx     = NULL;
      vpCurIxTag = NULL;
      sCurIxType = "";
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::CloseIndexFile() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
#endif // XB_INDEX_SUPPORT

/************************************************************************/
//! @brief Commit updates to disk 
/*!

  This routine commits any pending updates to disk.

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::Commit(){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  try{

    if( iDbfStatus == XB_UPDATED ){
      if( ulCurRec == 0 ){
        if(( iRc = AppendRecord()) != XB_NO_ERROR ){
          iErrorStop = 100;
          throw iRc;
        }
      } else {
        if(( iRc = PutRecord( ulCurRec )) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw iRc;
        }
      }
    }
  }
  catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::Commit() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;
}

/************************************************************************/
//! @brief Copy table (dbf) file structure.
/*!

  This routine will copy the structure of a dbf file and if successful
  return a pointer to the new table in an open state.

  \param dNewTable Reference to new table object.
  \param sNewTableName New table (dbf) name.
  \param sNewTableAlias Alias name of new table.
  \param iOverlay xbTrue - Overlay existing file.<br>
                  xbFalse - Don't overlay existing file.
  \param iShareMode XB_SINGLE_USER<br>
                    XB_MULTI_USER
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

//! Copy DBF structure
/*!
*/
xbInt16 xbDbf::CopyDbfStructure( xbDbf * dNewTable, const xbString &sNewTableName,
                 const xbString & sNewTableAlias, xbInt16 iOverlay, xbInt16 iShareMode ) {

// If successful, the table is returned in an open state after executing this method

  xbInt16  iRc = XB_NO_ERROR;
  xbInt16  iErrorStop = 0;
  xbSchema *newTableSchema = NULL;

  try{

    if( iDbfStatus == XB_CLOSED ){
      iErrorStop = 100;
      iRc = XB_DBF_FILE_NOT_OPEN;
      throw iRc;
    }

    if( !dNewTable ){
      iErrorStop = 110;
      iRc = XB_INVALID_OBJECT;
      throw iRc;
    }

    // Get the number of schema entries for this table
    xbInt32 lSchemaRecCnt = GetFieldCnt() + 1;

    // Allocate a Schema = No Of Fields + 1
    if((newTableSchema=(xbSchema *)malloc( (size_t) lSchemaRecCnt * sizeof(xbSchema)))==NULL){
      iErrorStop = 120;
      iRc = XB_NO_MEMORY;
      throw iRc;
    }

    //  Populate the Schema
    xbInt32 l;
    for( l = 0; l < lSchemaRecCnt-1; l++ ){
      memset( newTableSchema[l].cFieldName, 0x00, 11 );
      for( int x = 0; x < 10 && SchemaPtr[l].cFieldName[x]; x++ )
        newTableSchema[l].cFieldName[x] = SchemaPtr[l].cFieldName[x];
      newTableSchema[l].cType     = SchemaPtr[l].cType;
      newTableSchema[l].iFieldLen = SchemaPtr[l].cFieldLen;
      newTableSchema[l].iNoOfDecs = SchemaPtr[l].cNoOfDecs;
    }

    // set the last one to zeroes
    memset( newTableSchema[l].cFieldName, 0x00, 11 );
    newTableSchema[l].cType     = 0;
    newTableSchema[l].iFieldLen = 0;
    newTableSchema[l].iNoOfDecs = 0;

    dNewTable->SetVersion();
    #ifdef XB_MEMO_SUPPORT
    if( MemoFieldsExist())
      dNewTable->SetCreateMemoBlockSize( Memo->GetBlockSize() );
    #endif

    // Call the create a table function
    if(( iRc = dNewTable->CreateTable( sNewTableName, sNewTableAlias, newTableSchema, iOverlay, iShareMode )) != XB_NO_ERROR ){
      iErrorStop = 130;
      throw iRc;
    }
  }

  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbDbf::CopyDbfStructure() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }

  if( newTableSchema )
    free( newTableSchema );

  return iRc;
}

/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Create a new tag (index) for this dbf file (table).
/*!
  This routine creates a new tag (index) on a dbf file.  The library currently supports NDX and MDX
  indices.  If you don't have a specific need for an NDX file, use MDX.

  \param sIxType "MDX" or "NDX".
  \param sName Index or tag name.
  \param sKey Index key expression,
  \param sFilter Filter expression.  Not applicable for NDX indices.
  \param iDescending xbTrue for descending.  Not available for NDX indices.<br>
                     xbFalse - ascending
  \param iUnique xbTrue - Unique index<br>xbFalse - Not unique index.
  \param iOverLay xbTrue - Overlay if exists<br>
                  xbFalse - Don't overlay if it exists.
  \param pIxOut Pointer to pointer of output index object.
  \param vpTagOut Pointer to pointer of newly created tag,
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16  xbDbf::CreateTag( const xbString &sIxType, const xbString &sName, const xbString &sKey, const xbString &sFilter, 
                   xbInt16 iDescending, xbInt16 iUnique, xbInt16 iOverLay, xbIx **pIxOut, void **vpTagOut ){

  // this routine is used to open indices and link to files

  xbInt16 iErrorStop = 0;
  xbInt16 iRc = XB_NO_ERROR;

  #ifdef XB_LOCKING_SUPPORT
  xbBool   bLocked = xbFalse;
  #endif

  try{
    xbString sType = sIxType;
    sType.ToUpperCase();

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw( iRc );
      }
      bLocked = xbTrue;
    }
    #endif // XB_LOCKING_SUPPORT

    if( sIxType == "" ){
      iErrorStop = 110;
      iRc = XB_INVALID_OPTION;
      throw iRc;

    #ifdef XB_NDX_SUPPORT
    } else if( sIxType == "NDX" ){
      xbIxNdx *ixNdx = new xbIxNdx( this );

      if(( iRc = ixNdx->CreateTag( sName, sKey, sFilter, iDescending, iUnique, iOverLay, vpTagOut )) != XB_NO_ERROR ){
        iErrorStop = 120;
        throw iRc;
      }

      if(( iRc = AddIndex( ixNdx, sIxType )) != XB_NO_ERROR ){
        iErrorStop = 130;
        throw iRc;
      }
      *pIxOut = ixNdx;

      // Set the current tag if one not already set
      if( sCurIxType == "" ){
        sCurIxType = "NDX";
        pCurIx = ixNdx;
        vpCurIxTag = ixNdx->GetTag(0);
      }

    #endif

    #ifdef XB_MDX_SUPPORT
    } else if( sIxType == "MDX" ){

      if( GetVersion() == 3 ){     // MDX indexes were version 4 and higher
        iErrorStop = 140;
        iRc = XB_INVALID_INDEX;
        throw iRc;
      }

      xbIxMdx *ixMdx;
      xbString s;
      //  look through the index list and see if there is an mdx pointer we can grab
      xbBool bMdxFound = xbFalse;
      xbIxList *ixList = GetIxList();
      while( ixList && !bMdxFound ){
        s = ixList->sFmt->Str();
        if( s == "MDX" ){
          ixMdx = (xbIxMdx *) ixList->ix;
          bMdxFound = xbTrue;
        }
      }

      if( !bMdxFound )
        ixMdx = new xbIxMdx( this );

      if(( iRc = ixMdx->CreateTag( sName, sKey, sFilter, iDescending, iUnique, iOverLay, vpTagOut )) != XB_NO_ERROR ){
        iErrorStop = 150;
        throw iRc;
      }

      if( !bMdxFound ){
        if(( iRc = AddIndex( ixMdx, "MDX" )) != XB_NO_ERROR ){
          iErrorStop = 160;
          throw iRc;
        }

        cIndexFlag = 0x01;
        if(( iRc = WriteHeader( 1, 0 )) != XB_NO_ERROR ){
          iErrorStop = 170;
          throw iRc;
        }
      }
      *pIxOut = ixMdx;

      // set the current tag if one not already set
      if( sCurIxType == "" ){
        sCurIxType = "MDX";
        pCurIx = ixMdx;
        vpCurIxTag = ixMdx->GetTag(0);
      }

    #endif

    } else {
      iErrorStop = 200;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }

    if(( iRc = UpdateTagList()) != XB_NO_ERROR ){
      iErrorStop = 300;
      throw iRc;
    }

  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::CreateTag() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }

  #ifdef XB_LOCKING_SUPPORT
  if( bLocked )
    LockTable( XB_UNLOCK );
  #endif // XB_LOCKING_SUPPORT

  return iRc;
}
#endif // XB_INDEX_SUPPORT

/************************************************************************/
//! @brief Delete or undelete all records in a dbf file (table). 
/*!
  This routine deletes or un-deletes all records. The xbase file format contains
  a leading one byte character used for flagging a record as deleted.  When a record
  is deleted, it's not physically removed from the file, the first byte is flagged as deleted.
  
  \param iOption 0 - Delete all records.<br>
                 1 - Un-delete all deleted records.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::DeleteAll( xbInt16 iOption )
{
  xbInt16 iErrorStop = 0;
  xbInt16 iRc = XB_NO_ERROR;
  xbUInt32 ulRecCnt;

  try{
    if(( iRc = GetRecordCnt( ulRecCnt )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }
    if( ulRecCnt == 0 ) 
      return XB_NO_ERROR;
    for( xbUInt32 ul = 0; ul < ulRecCnt; ul++ ){
      if(( iRc = GetRecord( ul+1 )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw iRc;
      }
      if( iOption == 0 ){   /* delete all option */
        if( !RecordDeleted()){
          if(( iRc = DeleteRecord()) != XB_NO_ERROR ){
            iErrorStop = 120;
            throw iRc;
          }
          if(( iRc = Commit()) != XB_NO_ERROR ){
            iErrorStop = 130;
            throw iRc;
          }
        }
      } else {   /* undelete all option */
        if( RecordDeleted()){
          if(( iRc = UndeleteRecord()) != XB_NO_ERROR ){
            iErrorStop = 140;
            throw iRc;
          }
          if(( iRc = Commit()) != XB_NO_ERROR ){
            iErrorStop = 150;
            throw iRc;
          }
        }
      }
    }
  }
  catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbDbf::DeleteAll() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;
}

/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief 
/*!
  This routine deletes all indices associated with the dbf file.

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::DeleteAllIndexFiles(){

  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;

  #ifdef XB_LOCKING_SUPPORT
  xbBool   bLocked = xbFalse;
  #endif  // XB_LOCKING_SUPPORT

  #ifdef XB_INF_SUPPORT
  xbString sIxName;
  #endif // XB_INF_SUPPORT

  try{
    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw( iRc );
      }
      bLocked = xbTrue;
    }
    #endif // XB_LOCKING_SUPPORT

    // close any open index files, delete it, remove from the ix list
    while( ixList ){
      ixList->ix->Close();
      ixList->ix->xbRemove();
      #ifdef XB_INF_SUPPORT
      // if XB_INF_SUPPORT is enabled, all open non prod indices should be in here
      if( *ixList->sFmt != "MDX" ){    // production indices not stored in .INF dataset
        if(( iRc = ixList->ix->GetFileNamePart( sIxName )) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw iRc;
        }
        if(( iRc = AssociateIndex( *ixList->sFmt, sIxName, 1 )) != XB_NO_ERROR ){
          iErrorStop = 120;
          throw iRc;
        }
      }
      #endif
      RemoveIndex( ixList->ix );
    }
 } catch( xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::DeleteAllIndexFiles() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  #ifdef XB_LOCKING_SUPPORT
  if( bLocked )
    LockTable( XB_UNLOCK );
  #endif // XB_LOCKING_SUPPORT
  return iRc;
}
#endif   // XB_INDEX_SUPPORT

/************************************************************************/
//! @brief Delete all records.
/*!
  This routine deletes all the records in a table / dbf file.

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::DeleteAllRecords(){
  return DeleteAll( 0 );
}

/************************************************************************/
#ifdef XB_INF_SUPPORT
//! @brief Delete .INF File
/*!
  The original Dbase (TM) software supported non production indices (NDX) and production indices (MDX).
  The production indices are opened automatically when the DBF file is opened but the non-production
  indices are not.  This method is specific to the Xbas64 library and providex a means to link non production
  NDX index files to the DBF file so they will be opened automatically when the DBF file is opened.<br>

  This routine requires INF support be enabled when building the library.<br>
  This routine deletes the .INF file.<br>

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::DeleteInfData(){
  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;

  try{
    xbString sInfFileName;
    if(( iRc = GetInfFileName( sInfFileName )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }
    xbFile f( xbase );
    f.SetFileName( sInfFileName );
    if( f.FileExists()){
      if(( iRc = f.xbRemove()) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw iRc;
      }
    }
  } catch( xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::DeleteInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
#endif // XB_INF_SUPPORT

/************************************************************************/
//! @brief Delete the current record.
/*!
  This routine flags the current record for deletion if it's not already flagged.

  \returns XB_NO_ERROR<br>
           XB_INVALID_RECORD
*/

xbInt16 xbDbf::DeleteRecord(){
  if( RecBuf && ulCurRec > 0 ){
    if( RecBuf[0] != 0x2a){
      if( iDbfStatus != XB_UPDATED ){
        iDbfStatus = XB_UPDATED;
        memcpy( RecBuf2, RecBuf, uiRecordLen );  // save off original before making any updates
      }
      RecBuf[0] = 0x2a;
    }
    return XB_NO_ERROR;
  }
  else
    return XB_INVALID_RECORD;
}
/************************************************************************/
//! @brief Delete a table.
/*!
  This routine deletes a given table, associated index files if any, the
  memo file if any and the .INF file if any.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::DeleteTable(){

  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;

  #ifdef   XB_LOCKING_SUPPORT
  xbBool   bTableLocked = xbFalse;
  #endif

  try{

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      } else {
        bTableLocked = xbTrue;
      }
    }
    #endif  // XB_LOCKING_SUPPORT

    #ifdef XB_INDEX_SUPPORT
    if(( iRc = DeleteAllIndexFiles()) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }

    #ifdef XB_INF_SUPPORT
    if(( iRc = DeleteInfData()) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }
    #endif // XB_INF_SUPPORT
    #endif // XB_INDEX_SUPPORT

    #ifdef XB_MEMO_SUPPORT
    xbInt16 iMemoFldCnt = GetMemoFieldCnt();
    xbString sMemoFileName;
    if(iMemoFldCnt > 0 ){
      sMemoFileName = Memo->GetFqFileName();
    }
    #endif // XB_MEMO_SUPPORT

    if(( iRc = Close()) != XB_NO_ERROR ){
      iErrorStop = 130;
      throw iRc;
    }

    if(( iRc = xbRemove()) != XB_NO_ERROR ){
      iErrorStop = 140;
      throw iRc;
    }

    #ifdef XB_MEMO_SUPPORT
    if( iMemoFieldCnt > 0 ){
      xbFile f( xbase );
      if(( iRc = f.xbRemove( sMemoFileName )) != XB_NO_ERROR ){
        iErrorStop = 150;
        throw iRc;
      }
    }
    #endif // XB_MEMO_SUPPORT

  } catch( xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::DeleteTable() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  #ifdef XB_LOCKING_SUPPORT
  if( bTableLocked )
    LockTable( XB_UNLOCK );
  #endif // XB_LOCKING_SUPPORT
  return iRc;
}

/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Delete an index tag.

/*!
  This routine deletes an index tag
  \param sIxType Either "NDX" or "MDX".<br>
  \param sName Tag name to delete.<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::DeleteTag( const xbString &sIxType, const xbString &sName ){

  xbInt16 iErrorStop = 0;
  xbInt16 iRc = XB_NO_ERROR;
  xbIx *pIx = NULL;

  #ifdef XB_LOCKING_SUPPORT
  xbBool bTableLocked = xbFalse;
  #endif

  try{

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !GetTableLocked() ){
      if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      } else {
        bTableLocked = xbTrue;
      }
    }
    #endif   // XB_LOCKING_SUPPORT

    if( sIxType == "" ){
      iErrorStop = 110;
      iRc = XB_INVALID_OPTION;
      throw iRc;

    #ifdef XB_NDX_SUPPORT
    } else if( sIxType == "NDX" ){

      xbIxList *ixl = ixList;
      xbBool bDone = xbFalse;
      while( ixl && !bDone ){

        if( ixl->ix->GetTagName( NULL ) == sName ){
          bDone = xbTrue;

          // remove from .INF if it's there
          #ifdef XB_INF_SUPPORT 
          if(( iRc = AssociateIndex( "NDX", sName, 1 )) != XB_NO_ERROR ){
            iErrorStop = 120;
            throw iRc;
          }
          #endif // XB_INF_SUPPORT

          if(( iRc = ixl->ix->DeleteTag( ixl->ix->GetTag( 0 ))) != XB_NO_ERROR ){
            iErrorStop = 130;
            throw iRc;
          }

          if(( iRc = RemoveIndex( ixl->ix )) != XB_NO_ERROR ){
            iErrorStop = 140;
            throw iRc;
          }

          if( ixl->ix == pCurIx )
            SetCurTag( "", NULL, NULL );

        }
        ixl = ixl->next;
      }
    #endif

    #ifdef XB_MDX_SUPPORT
    } else if( sIxType == "MDX" ){
      xbIxList *ixl = ixList;
      xbIxList *ixlNext;
      xbIxList *ixlPrev = NULL;
      xbBool bDone = xbFalse;
      xbIxMdx  *pMdx;
      xbMdxTag *pMdxTag;
      xbInt16 iTagCnt = 0;

      while( ixl && !bDone ){
        ixlNext = ixl->next;
        pMdx = (xbIxMdx *) ixl->ix;
        iTagCnt = pMdx->GetTagCount();
        for( xbInt16 i = 0; i < iTagCnt && !bDone; i++ ){
          pMdxTag = (xbMdxTag *) pMdx->GetTag( i );
          if( pMdx->GetTagName( pMdxTag ) == sName ){
            bDone = xbTrue;
            iRc = pMdx->DeleteTag( pMdxTag );
            if( iRc > 0 ){
              // Successful delete of only tag in production mdx file - need to remove it from the list, update the dbf header
              cIndexFlag = 0x00;
              if(( iRc = WriteHeader( 1, 0 )) != XB_NO_ERROR ){
                iErrorStop = 150;
                throw iRc;
              }
              if(( iRc = RemoveIndex( ixl->ix )) != XB_NO_ERROR ){
                iErrorStop = 160;
                throw iRc;
              }
              if( ixlPrev == NULL ){
                // std::cout << "setting ixList to null or should be\n";
                ixList = ixlNext;
              } else {
                ixlPrev = ixlNext;
              }
            } else if( iRc < 0 ){
              iErrorStop = 170;
              throw iRc;
            }
            if( ixl->ix == pCurIx )
              SetCurTag( "", NULL, NULL );
          }
        }
        ixlPrev = ixl;
        ixl = ixlNext;
      }

      if( !bDone )
        return XB_INVALID_TAG;

    #endif

    } else {
      iErrorStop = 180;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }

    if(( iRc = UpdateTagList()) != XB_NO_ERROR ){
      iErrorStop = 190;
      throw iRc;
    }


  }
  catch (xbInt16 iRc ){
    if( pIx ) delete pIx;
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::DeleteTag() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && GetTableLocked() ){
      LockTable( XB_UNLOCK );
    }
    #endif   // XB_LOCKING_SUPPORT
  }

  #ifdef XB_LOCKING_SUPPORT
  if( bTableLocked ){
    LockTable( XB_UNLOCK );
  }
  #endif   // XB_LOCKING_SUPPORT


  return iRc;
}
#endif // XB_INDEX_SUPPORT
/************************************************************************/
//! @brief Dump dbf file header. 
/*!
  This routine dumps dbf header information to the console.

  \param iOption  1 = Print header only<br>
                  2 = Field data only<br>
                  3 = Header and Field data<br> 
                  4 = Header, Field and Memo header data if applicable
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::DumpHeader( xbInt16 iOption ) const {
  int i;
  int iMemoCtr = 0;

  if( iOption < 1 || iOption > 4 )
    return XB_INVALID_OPTION;

  if( iDbfStatus == XB_CLOSED )
    return XB_NOT_OPEN;

  std::cout << "\nDatabase file " << GetFqFileName() << std::endl << std::endl;

  if( iOption != 2 ){
    std::cout << "File header data:" << std::endl;

    xbInt16 sVer = DetermineXbaseTableVersion( cVersion );

    if( sVer == 3 )
      std::cout << "Dbase III file"  << std::endl;
    else if ( sVer == 4 )
      std::cout << "Dbase IV file"   << std::endl << std::endl;
    else if ( sVer == 5 )
      std::cout << "Dbase V file"    << std::endl << std::endl;
    else if ( sVer == 7 )
      std::cout << "Dbase VII file"  << std::endl << std::endl;
    else
      std::cout << "Unknown Version" << std::endl;

    /* display the bit stream */
    unsigned char c, tfv, displayMask = 1 << 7;
    tfv = cVersion;
    std::cout << "File descriptor bits = ";
    for( c = 1; c<= 8; c++ ){
      std::cout << (tfv & displayMask ? '1' : '0');
      tfv <<= 1;
    }
    std::cout << std::endl;

    std::cout << "Descriptor bits legend:"           << std::endl;
    std::cout << "  0-2 = version number"            << std::endl;
    std::cout << "    3 = presence of dBASE IV memo" << std::endl;
    std::cout << "  4-6 = SQL table presence"        << std::endl;
    std::cout << "    7 = Presence of any memo file (dBASE III PLUS or dBASE IV)" << std::endl << std::endl;

    std::cout << "Last update date     = " 
      << (int) cUpdateMM << "/" << (int) cUpdateDD   << "/" << (int) cUpdateYY % 100 << std::endl;  

    std::cout << "Header length        = " << uiHeaderLen << std::endl;
    std::cout << "Record length        = " << uiRecordLen << std::endl;
    std::cout << "Records in file      = " << ulNoOfRecs  << std::endl << std::endl << std::endl;

    std::cout << "Transaction Flag     = ";
    xbase->BitDump( cTransactionFlag );
    std::cout << std::endl;

    std::cout << "Encryption Flag      = ";
    xbase->BitDump( cEncryptionFlag );
    std::cout << std::endl;

    std::cout << "Index Flag           = ";
    xbase->BitDump( cIndexFlag );
    std::cout << std::endl;

    std::cout << "Lang Driver          = " << (int) cIndexFlag << " - ";
    xbase->BitDump( cIndexFlag );
    std::cout << std::endl;
    #ifdef XB_INDEX_SUPPORT
    std::cout << "Open Index Files     = " << GetPhysicalIxCnt() << std::endl;
    #endif  // XB_INDEX_SUPPORT
  }

  if( iOption != 1 ){
    char c;
    std::cout << "Field Name   Type  Length  Decimals IxFlag" << std::endl;
    std::cout << "----------   ----  ------  -------- ------" << std::endl;
    for( i = 0; i < iNoOfFields; i++ ){

      SchemaPtr[i].cIxFlag ? c = 'Y' : c = ' ';

      if( SchemaPtr[i].cType == 'C' && SchemaPtr[i].cNoOfDecs > 0 )
        printf( "%10s    %1c     %4d    %4d      %c\n", SchemaPtr[i].cFieldName,
               SchemaPtr[i].cType, SchemaPtr[i].cFieldLen, 0, c );
      else
        printf( "%10s    %1c     %4d    %4d      %c\n", SchemaPtr[i].cFieldName,
               SchemaPtr[i].cType, SchemaPtr[i].cFieldLen, SchemaPtr[i].cNoOfDecs, c );

      if( SchemaPtr[i].cType == 'M' )
        iMemoCtr++;
    }
  }
  std::cout << std::endl;

#ifdef XB_MEMO_SUPPORT
  if( iOption > 3 && iMemoCtr > 0 )
    Memo->DumpMemoHeader();
#endif

  return XB_NO_ERROR;
}
/************************************************************************/
//! Dump record
/*!
  Dump the contents of the specified record


    \param ulRecNo Record number of record to be dumped.
    \param iOutputDest 0 = stdout<br>
                1 = Syslog<br>
                2 = Both<br>

    \param iOutputFmt  0 = with field names<br>
                1 = 1 line per rec, no field names<br>
                2 = 1 line per rec, first line is a list of field names.
    \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::DumpRecord( xbUInt32 ulRecNo, xbInt16 iOutputDest, xbInt16 iOutputFmt ) {
  int i, iRc = XB_NO_ERROR;

  xbString sTemp;
  xbString s2;
  if( ulRecNo == 0 || ulRecNo > ulNoOfRecs )
    return XB_INVALID_RECORD;

  if( ulCurRec != ulRecNo ){
    iRc = GetRecord( ulRecNo );
    if( iRc != XB_NO_ERROR )
      return iRc;
  }

  if( iOutputFmt >= 1 ){
    if( iOutputFmt == 2 ){
      sTemp = "RecNo,DEL";
      for( i = 0; i < iNoOfFields; i++ ){
        s2 = SchemaPtr[i].cFieldName;
        s2.Trim();
        sTemp += ",";
        sTemp += s2;
      }
      xbase->WriteLogMessage( sTemp.Str(), iOutputDest );
    }

    if( RecordDeleted() )
      s2.Sprintf( "%ld,DEL", ulRecNo );
    else
      s2.Sprintf( "%ld,", ulRecNo );

    for( i = 0; i < iNoOfFields; i++ ){
      GetField( i, sTemp );
      sTemp.Trim();
      s2.Sprintf( "%s,'%s'", s2.Str(), sTemp.Str());
    }
    xbase->WriteLogMessage( s2.Str(),iOutputDest );
    return XB_NO_ERROR;
  }

  sTemp.Sprintf( "\nRec Number: %ld", ulRecNo );
  xbase->WriteLogMessage( sTemp.Str(),iOutputDest);

  if( RecordDeleted())
    xbase->WriteLogMessage( "Record flagged as deleted", iOutputDest );


  #ifdef XB_MEMO_SUPPORT
  xbString sMemo;
  #endif

  for( i = 0; i < iNoOfFields; i++ ){

    #ifdef XB_MEMO_SUPPORT
    GetField( i, sTemp );
    sTemp.Trim();

    if(SchemaPtr[i].cType == 'M'){
      GetMemoField( i, sMemo );
    if( sMemo.Len() > 70 )
      sMemo.Resize( 70 );
    s2.Sprintf ( "%c  %s  = '%s'", SchemaPtr[i].cType, SchemaPtr[i].cFieldName, sMemo.Str());
    xbase->WriteLogMessage( s2.Str(), iOutputDest);

    /*
      xbUInt32 ulMlen;
      if( MemoFieldExists( i )){
        Memo->GetMemoFieldLen( i, ulMlen );
        s2.Sprintf( "   Len = %d", ulMlen );
      } 
      xbase->WriteLogMessage( s2.Str(), iOutputDest);
    */

    }
    else{
      s2.Sprintf ( "%c  %s  = '%s'", SchemaPtr[i].cType, SchemaPtr[i].cFieldName, sTemp.Str());
      xbase->WriteLogMessage( s2.Str(), iOutputDest);
    }
    #else
    GetField( i, sTemp );
    sTemp.Trim();
    s2.Sprintf( "%s = '%s'", SchemaPtr[i].cFieldName, sTemp.Str());
    xbase->WriteLogMessage( s2.Str(), iOutputDest);
    #endif

  }
  return XB_NO_ERROR;
}


/************************************************************************/
#ifdef XB_DEBUG_SUPPORT


#ifdef XB_LOCKING_SUPPORT
//! @brief Dump the table lock status 
/*!
  Debugging routine.  Dumps the table lock status to the console.
  \returns void
*/

void xbDbf::DumpTableLockStatus() const {

  std::cout << "File Lock Retry Count = [" << GetLockRetryCount() << "]" << std::endl;
  std::cout << "File Lock Flavor      = [";
  switch (GetLockFlavor()){
    case 1:
      std::cout << "Dbase]" << std::endl;
      break;
    case 2:
      std::cout << "Clipper]" << std::endl;
      break;
    case 3:
      std::cout << "Fox]" << std::endl;
      break;
    case 9:
      std::cout << "Xbase64]" << std::endl;
      break;
    default:
      std::cout << "Unknown]" << std::endl;
      break;
  }
  std::cout << "File Auto Lock        = [";

  if( GetAutoLock())
    std::cout << "ON]" << std::endl;
  else
    std::cout << "OFF]" << std::endl;
  if( GetHeaderLocked())
    std::cout << "Header Locked         = [TRUE]\n";
  else
    std::cout << "Header Locked         = [FALSE]\n";

  if( GetTableLocked())
    std::cout << "Table Locked          = [TRUE]\n";
  else
    std::cout << "Table Locked          = [FALSE]\n";

  if( GetAppendLocked() > 0 )
    std::cout << "Append Locked         = [" << GetAppendLocked() << "]\n";
  else
    std::cout << "Append Locked         = [FALSE]\n";

  #ifdef XB_MEMO_SUPPORT
  if( GetMemoLocked())
    std::cout << "Memo Locked           = [TRUE]\n";
  else
    std::cout << "Memo Locked           = [FALSE]\n";
  #endif // XB_MEMO_SUPPORT

  xbLinkListNode<xbUInt32> * llN = GetFirstRecLock();
  if( llN ){
    while( llN ){
      std::cout << "Record Locked         = [" << llN->GetKey() << "]\n";
      llN = llN->GetNextNode();
    }
  } else {
      std::cout << "Record Locked         = [None]\n";
  }
}
#endif  // XB_LOCKING_SUPPORT
#endif //  XB_DEBUG_SUPPORT

/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Get the append locked bytes status 
/*!
  \returns The record number of the new record for the append lock operation.
*/

xbUInt32 xbDbf::GetAppendLocked() const {
  return this->ulAppendLocked;
}

#endif // XB_LOCKING_SUPPORT

/************************************************************************/
//! @brief Get auto commit setting. 
/*!

  This routine returns the table setting if set, otherwise returns the system
  level setting.
  
  \returns Not 0 - Auto commit on for this table.<br>
           0 -  Auto commit off for this table.
*/

xbInt16 xbDbf::GetAutoCommit() const {
  return GetAutoCommit( 1 );
}

/************************************************************************/
//! @brief Get auto commit setting.
/*!

  \param iOption  0 - Specific setting for this table<br>
                  1 - If this table should be auto updated (takes DBMS setting into account)
  \returns Not 0 - Auto commit on for this table.<br>
           0 -  Auto commit off for this table.
*/

xbInt16 xbDbf::GetAutoCommit( xbInt16 iOption ) const {
  if( iOption == 1 && iAutoCommit == -1 )
    return xbase->GetDefaultAutoCommit();
  else
    return iAutoCommit;
}


/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Get Auto Lock setting. 
/*!
  \returns Auto lock setting.
*/
xbInt16 xbDbf::GetAutoLock() const{
  return iAutoLock;
}
#endif // XB_LOCKING_SUPPORT

/************************************************************************/
#ifdef XB_MEMO_SUPPORT
//! @brief Get the memo file block size used when creating a memo file. 
/*!
  \returns Memo block size.
*/
xbUInt32 xbDbf::GetCreateMemoBlockSize() const {
  return ulCreateMemoBlockSize;
}
#endif  // XB_MEMO_SUPPORT

/************************************************************************/
//! @brief Get a pointer to the current index object. 
/*!
  \returns Pointer to current index.
*/
#ifdef XB_INDEX_SUPPORT
xbIx *xbDbf::GetCurIx() const {
  return pCurIx;
}
/************************************************************************/
//! @brief Get pointer to current tag for the current index.
/*!
  An index file can have one or more tags.  An NDX index has one tag. 
  An MDX file can have up to 47 tags.

  \returns Pointer to current tag.
*/
void *xbDbf::GetCurTag() const {
  return vpCurIxTag;
}
/************************************************************************/
//! @brief Get the current index type.
/*!
  \returns NDX for single tag index.<br>
           MDX for production multi tag index. 
*/
const xbString &xbDbf::GetCurIxType() const {
  return sCurIxType;
}

/************************************************************************/
//! @brief Get the current tag name.
/*!
  \returns Current Tag Name.<br>
*/

const xbString &xbDbf::GetCurTagName() const {

  if( pCurIx )
    return pCurIx->GetTagName( vpCurIxTag );
   else
    return sNullString;
}

/************************************************************************/
//! @brief GetFirstKey for tag.
/*!

  Position to the first key for the current tag
  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/
xbInt16 xbDbf::GetFirstKey(){
  if( pCurIx )
    return pCurIx->GetFirstKey( vpCurIxTag, 1 );
  else
    return XB_INVALID_TAG;
}

/************************************************************************/
//! @brief GetLastKey for tag.
/*!

  Position to the last key for the current tag
  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/
xbInt16 xbDbf::GetLastKey(){
  if( pCurIx )
    return pCurIx->GetLastKey( vpCurIxTag, 1 );
  else
    return XB_INVALID_TAG;
}

/************************************************************************/
//! @brief GetNextKey for tag.
/*!

  Position to the next key for the current tag
  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/
xbInt16 xbDbf::GetNextKey(){
  if( pCurIx )
    return pCurIx->GetNextKey( vpCurIxTag, 1 );
  else
    return XB_INVALID_TAG;
}

/************************************************************************/
//! @brief GetPrevKey for tag.
/*!

  Position to the previous key for the current tag
  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/
xbInt16 xbDbf::GetPrevKey(){
  if( pCurIx )
    return pCurIx->GetPrevKey( vpCurIxTag, 1 );
  else
    return XB_INVALID_TAG;
}

/************************************************************************/
//! @brief Find record for key.
/*!

  Find a key and position to record if key found

  \param sKey  String key to find
  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/
xbInt16 xbDbf::Find( xbString &sKey ){
  if( pCurIx )
    return pCurIx->FindKey( vpCurIxTag, sKey.Str(), (xbInt32) sKey.Len(), 1 );
  else
    return XB_INVALID_TAG;
}

/************************************************************************/
//! @brief Find record for key.
/*!

  Find a key and position to record if key found

  \param dtKey  Date key to find
  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/
xbInt16 xbDbf::Find( xbDate &dtKey ){

 if( pCurIx )
    return pCurIx->FindKey( vpCurIxTag, dtKey, 1 );
  else
    return XB_INVALID_TAG;

}
/************************************************************************/
//! @brief Find record for key.
/*!

  Find a key and position to record if key found

  \param dtKey  Date key to find
  \returns <a href="xbretcod_8h.html">Return Codes</a>

*/
xbInt16 xbDbf::Find( xbDouble &dKey ){

 if( pCurIx )
    return pCurIx->FindKey( vpCurIxTag, dKey, 1 );
  else
    return XB_INVALID_TAG;

}


#endif // XB_INDEX_SUPPORT

/************************************************************************/
//! @brief Return the current record number. 
/*!
  \returns Returns the current record number.
*/
xbUInt32 xbDbf::GetCurRecNo() const {
  return ulCurRec;
}

/************************************************************************/
//! @brief Return the current dbf status. 
/*!
  \returns  0 = closed<br>
            1 = open<br>
            2 = updates pending<br>
*/
xbInt16 xbDbf::GetDbfStatus() const {
  return iDbfStatus;
}
/************************************************************************/
//! @brief Return the number of fields in the table. 
/*!
  \returns The number of fields in the table.
*/
xbInt32 xbDbf::GetFieldCnt() const {
  return iNoOfFields;
}

/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Get the first first record lock.
/*!
  Get the first record lock from a linked list of record locks.
  \returns First record lock.
*/
xbLinkListNode<xbUInt32> * xbDbf::GetFirstRecLock() const {
  return lloRecLocks.GetHeadNode();
}
#endif // XB_LOCKING_SUPPORT
/************************************************************************/
//! @brief Get the first record.
/*!
  Get the first not deleted record.  This routines skips over any deleted records.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetFirstRecord()
{
  return GetFirstRecord( XB_ACTIVE_RECS );
}

/************************************************************************/
//! @brief Get the first record.
/*!

  \param iOption XB_ALL_RECS - Get the first record, deleted or not.<br>
         XB_ACTIVE_RECS - Get the first active record.<br>
         XB_DELETED_RECS - Get the first deleted record.<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetFirstRecord( xbInt16 iOption )
{
  if( ulNoOfRecs == 0 )
    return XB_EMPTY;

  xbInt16 iRc = GetRecord( 1L );
  while(  iRc == XB_NO_ERROR && 
        ((RecordDeleted() && iOption == XB_ACTIVE_RECS) || 
        (!RecordDeleted() && iOption == XB_DELETED_RECS)))
    if( ulCurRec < ulNoOfRecs )
      iRc = GetRecord( ulCurRec + 1 );
    else
      return XB_EOF;

  return iRc;
}

/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Return lock status of the table header 
/*! \returns DBF header lock status
*/

xbBool xbDbf::GetHeaderLocked() const {
  return this->bHeaderLocked;
}
#endif   // XB_LOCKING_SUPPORT

#ifdef XB_INDEX_SUPPORT
//! @brief Return pointer to list of index files for the table.
/*!
  \returns Returns an xbIxList * pointer to list of open index files.
*/

xbIxList *xbDbf::GetIxList() const{
  return ixList;
}
#endif // XB_INDEX_SUPPORT


/************************************************************************/
//! @brief Get the last record.
/*!
  Get the last not deleted record.  This routines skips over any deleted records.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetLastRecord()
{
  return GetLastRecord( XB_ACTIVE_RECS );
}
/************************************************************************/
//! @brief Get the last record.
/*!

  \param iOption XB_ALL_RECS - Get the last record, deleted or not.<br>
         XB_ACTIVE_RECS - Get the last active record.<br>
         XB_DELETED_RECS - Get the last deleted record.<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetLastRecord( xbInt16 iOption )
{
  if( ulNoOfRecs == 0 )
    return XB_EMPTY;

  xbInt16 iRc = GetRecord( ulNoOfRecs );
  while(  iRc == XB_NO_ERROR &&
        ((RecordDeleted() && iOption == XB_ACTIVE_RECS) || 
        (!RecordDeleted() && iOption == XB_DELETED_RECS)))
    if( ulCurRec > 1 )
      iRc = GetRecord( ulCurRec - 1 );
    else
      return XB_EOF;

  return iRc;
}


/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Get lock flavor.
/*!
  This routine is currently in place to provide structure for future locking
  schemes that may differ from the legacy DBase (TM) locking scheme.
  \returns Always 1.
*/

xbInt16 xbDbf::GetLockFlavor() const{
  if( iLockFlavor == -1 )
    return xbase->GetDefaultLockFlavor();
  else
    return iLockFlavor;
}
#endif   // XB_LOCKING_SUPPORT

/************************************************************************/
#ifdef XB_MEMO_SUPPORT
/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief  Get the lock status of the memo file.
/*!
  \returns Lock status of memo file.
*/
xbBool xbDbf::GetMemoLocked() const {
  if( MemoFieldsExist())
    return Memo->GetMemoLocked();
  else
    return xbFalse;
}
#endif  // XB_LOCKING_SUPPORT

/************************************************************************/
//! @brief Get pointer to Memo object.
/*!
  \returns This routine returns the pointer to the memo object.
*/

xbMemo * xbDbf::GetMemoPtr(){
  return Memo;
}

#endif  // XB_MEMO_SUPPORT


/************************************************************************/
#ifdef XB_INF_SUPPORT
//! @brief Return the .INF file name
/*!
  If NDXIDX support is enabled in the library, and a non production (ndx)
  has been associated with the dbf file, the .INF file name can be retrieved 
  with this routine.

  \param sInfFileName Output string containing .INF file name.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::GetInfFileName( xbString &sInfFileName ){

  sInfFileName = GetFqFileName();
  xbUInt32 lLen = sInfFileName.Len();
  if( lLen < 5 )
    return XB_FILE_NOT_FOUND;
  sInfFileName.PutAt(lLen-2, 'I');
  sInfFileName.PutAt(lLen-1, 'N');
  sInfFileName.PutAt(lLen,   'F');
  return XB_NO_ERROR;
}

/************************************************************************/
//! @brief Return first node of linked list of .INF items.
/*!
  \returns List of .INF entries.
*/

xbLinkListNode<xbString> * xbDbf::GetInfList() const{
  return llInfData.GetHeadNode();
}
#endif  // XB_INF_SUPPORT


/************************************************************************/
//! @brief Get the next record.
/*!
  Get the next not deleted record.  This routines skips over any deleted records.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetNextRecord(){
  return GetNextRecord( XB_ACTIVE_RECS );
}

/************************************************************************/
//! @brief Get the next record.
/*!
  \param iOption XB_ALL_RECS - Get the next record, deleted or not.<br>
         XB_ACTIVE_RECS - Get the next active record.<br>
         XB_DELETED_RECS - Get the next deleted record.<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetNextRecord( xbInt16 iOption ){
  if( ulNoOfRecs == 0 ) 
    return XB_EMPTY;
  else if( ulCurRec >= ulNoOfRecs ) 
    return XB_EOF;
  xbInt16 iRc = GetRecord( ulCurRec + 1 );
  while(  iRc == XB_NO_ERROR && 
        ((RecordDeleted() && iOption == XB_ACTIVE_RECS) || 
        (!RecordDeleted() && iOption == XB_DELETED_RECS)))
    if( ulCurRec < ulNoOfRecs )
      iRc = GetRecord( ulCurRec + 1 );
    else
      return XB_EOF;
  return iRc;
}
/************************************************************************/
//! @brief Get the next record.
/*!

  \param iOption XB_ALL_RECS - Get the next record, deleted or not.<br>
         XB_ACTIVE_RECS - Get the next active record.<br>
         XB_DELETED_RECS - Get the next deleted record.<br>
  \param ulStartRec Get next record, starting from ulStartRec.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::GetNextRecord( xbInt16 iOption , xbUInt32 ulStartRec ){

  if( iOption == 0 )
    return GetNextRecord();
  else if( iOption == 1 ){
    if( ulStartRec > 0 )
      ulCurRec = ulStartRec;
    xbInt16 iRc = GetNextRecord();
    while( iRc == XB_NO_ERROR && RecordDeleted())
      iRc = GetNextRecord();
    return iRc;
  }
  else
    return XB_INVALID_OPTION;
}


/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Physical count of open index files.
/*!

  Returns a physical count of open index files for the dbf file. An index file
  can contain one or more tags.
  \returns Count of open index files.
*/

xbInt32 xbDbf::GetPhysicalIxCnt() const {

  xbInt32 lCnt = 0;
  #ifdef XB_INDEX_SUPPORT
  xbIxList *p = ixList;
  while( p ){
    lCnt++;
    p = p->next;
  }
  #endif
  return lCnt;
}
#endif  // XB_INDEX_SUPPORT


/************************************************************************/
//! @brief Get the previous record.
/*!
  Get the previous not deleted record.  This routine skips over any deleted records.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::GetPrevRecord()
{
  return GetPrevRecord( XB_ACTIVE_RECS );
}

/************************************************************************/
//! @brief Get the previous record.
/*!

  \param iOption XB_ALL_RECS - Get the previous record, deleted or not.<br>
         XB_ACTIVE_RECS - Get the previous active record.<br>
         XB_DELETED_RECS - Get the previous deleted record.<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetPrevRecord( xbInt16 iOption ){
  if( ulNoOfRecs == 0 ) 
    return XB_EMPTY;
  else if( ulCurRec <= 1L ) 
    return XB_BOF;
  xbInt16 iRc = GetRecord( ulCurRec - 1 );
  while(  iRc == XB_NO_ERROR && 
        ((RecordDeleted() && iOption == XB_ACTIVE_RECS) || 
        (!RecordDeleted() && iOption == XB_DELETED_RECS)))
    if( ulCurRec > 1 )
      iRc = GetRecord( ulCurRec - 1 );
    else
      return XB_BOF;

  return iRc;
}


/************************************************************************/
//! @brief Get record for specified record number.
/*!
  Retrieve a record from disk and load it into the record buffer.  If auto commit
  is enabled and there are pending updates, this routine will flush the updates
  to disk before proceeding to ulRecNo.

  \param ulRecNo - Record number to retrieve.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetRecord( xbUInt32 ulRecNo ){
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{
    /* verify the file is open */
    if( iDbfStatus == XB_CLOSED ){
      iErrorStop = 100;
      iRc = XB_NOT_OPEN;
      throw iRc;
    }
    if( iDbfStatus == XB_UPDATED ){
      if(  GetAutoCommit() == 1 ){
        if(( iRc = Commit()) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw iRc;
        }
      } else {
        if(( iRc = Abort()) != XB_NO_ERROR ){
          iErrorStop = 120;
          throw iRc;
        }
     }
   }

    if( ulRecNo > ulNoOfRecs || ulRecNo == 0L ){
      iErrorStop = 130;
      iRc = XB_INVALID_RECORD;
      throw iRc;
    }

    if(( xbFseek( (uiHeaderLen+(( (xbInt64) ulRecNo-1L ) * uiRecordLen )), SEEK_SET )) != XB_NO_ERROR ){
      iErrorStop = 140;
      iRc = XB_SEEK_ERROR;
      throw iRc;
    }

    if( xbFread( RecBuf, uiRecordLen, 1 ) != XB_NO_ERROR ){
      iErrorStop = 150;
      iRc = XB_READ_ERROR;
      throw iRc;
    }
    ulCurRec = ulRecNo;
  }

  catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbDbf::GetRecord()  Exception Caught. Error Stop = [%d] iRc = [%d] record = [%d]", iErrorStop, iRc, ulRecNo );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;
}
/************************************************************************/
//! @brief Get pointer to record buffer 
/*!
  \param iOpt 0 for RecBuf (current) or 1 for RecBuf2 (original contents)

  \returns Pointer to record buffer.
*/
char * xbDbf::GetRecordBuf( xbInt16 iOpt ) const {
  if( iOpt )
    return RecBuf2;
  else
    return RecBuf;
}




/************************************************************************/
//! @brief Get the current number of records in the dbf data file.
/*!
  \returns Record count or <a href="xbretcod_8h.html">Return Codes</a>
*/
xbUInt32 xbDbf::GetRecordCount(){

  xbUInt32 ulCnt;
  xbInt16 iRc = GetRecordCnt( ulCnt );
  if( iRc < 0 )
    return (xbUInt32) iRc;
  else
    return ulCnt;
}

/************************************************************************/
//! @brief Get the current number of records in the dbf data file.
/*!
  \param ulRecCnt Output number of records in file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::GetRecordCnt( xbUInt32 & ulRecCnt )
{
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  #ifdef XB_LOCKING_SUPPORT
  xbBool  bLocked = xbFalse;
  #endif  // XB_LOCKING_SUPPORT

  try{
    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockHeader( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      } else 
        bLocked = xbTrue;
    }
    #endif // XB_LOCKING_SUPPORT

    if((iRc = ReadHeader(1,1)) != XB_NO_ERROR){
      iErrorStop = 110;
      throw iRc;
    }
  }
  catch( xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbDbf::GetRecordCnt()  Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }

  #ifdef XB_LOCKING_SUPPORT
  if( bLocked ){
    LockHeader( XB_UNLOCK );
  }
  #endif

  ulRecCnt = ulNoOfRecs;
  return iRc;
}
/************************************************************************/
//! @brief Get the dbf record length. 
/*!
  \returns Record length.
*/
xbUInt16 xbDbf::GetRecordLen() const {
  return uiRecordLen;
}
/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Get table locked status 
/*!
  \returns Table lock status.
*/
xbBool xbDbf::GetTableLocked() const {
  return this->bTableLocked;
}
#endif  // XB_LOCKING_SUPPORT
/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Get tag list for dbf file. 
/*!
   This routine returns a list of tags for the file.<br>

   The library is structured to support one or more files of the same or differing 
   index types (NDX/MDX), with  each file supporting one or more index tags.<br>
 
  \returns Tag list for the file/table.
*/
xbLinkListNode<xbTag *> *xbDbf::GetTagList() const {
  return llTags.GetHeadNode();
}
#endif  // XB_INDEX_SUPPORT
/************************************************************************/
//! @brief Get the table alias. 
/*!
  This routine returns the table alias.
  \returns Table alias
*/
const xbString & xbDbf::GetTblAlias() const {
  return this->sAlias;
}

/************************************************************************/
//! @brief Get the pointer to the xbXbase structure,
/*! 
  \returns Pointer to xbXbase structure.
*/
xbXBase * xbDbf::GetXbasePtr() const {
  return xbase;
}
/************************************************************************/
#ifdef XB_INF_SUPPORT
//! @brief Load .INF data file,
/*! 
  Protected method.  This routine loads the ndx inf file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::LoadInfData(){

  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;

  try{
    // create file name
    xbString sInfFileName;
    if(( iRc = GetInfFileName( sInfFileName )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }
    // if file does not exist, return no error
    xbFile fMd( xbase );
    if( !fMd.FileExists( sInfFileName ))
      return XB_NO_ERROR;

    // open file file in read only mode
    if(( iRc = fMd.xbFopen( "r", sInfFileName, GetShareMode())) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }
    // clear the linked list
    llInfData.Clear();

    // for each entry in the file, add a linked list item
    xbString sRec;
    xbString sLeft3;
    xbString sFn;

    while( iRc == XB_NO_ERROR ){
      sRec = "";
      if(( iRc = fMd.xbFgets( 132, sRec )) == XB_NO_ERROR ){
        sLeft3 = sRec;
        sLeft3.Left( 3 );
        sLeft3.ToUpperCase();
        if( sLeft3 == "NDX"){
          sFn.ExtractElement(sRec.Str(), '=', 2 );
          sFn.ZapChar( 0x0d );
          sFn.ZapChar( 0x0a );
          llInfData.InsertAtEnd( sFn );  
        }
      }
    }
    // close the file
    if(( iRc = fMd.xbFclose()) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }

 } catch( xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::LoadInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
#endif  // XB_INF_SUPPORT

/************************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Lock append bytes.
/*!
  This routine locks the append bytes and is used by the AppendRecord function.

  \param iLockFunction XB_LOCK<br>XB_UNLOCK
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::LockAppend( xbInt16 iLockFunction )
{
  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;
  xbUInt32 ulAppendRec;

  try{
    if( iLockFunction == XB_LOCK ){
      iErrorStop = 100;
      if( ulAppendLocked > 0 )         /* already have an append lock */
        return XB_NO_ERROR;

      ulAppendRec = ulNoOfRecs + 1;    /* record number needing to be locked */
      if( GetLockFlavor() == LK_DBASE ){
        iRc = xbLock( XB_LOCK, LK4026531839, 1 );
        if( iRc != XB_NO_ERROR ){
          if( iRc == XB_LOCK_FAILED )
            return iRc;
          else{
            iErrorStop = 110;
            throw iRc;
          }
        }

        xbInt64 llAppendRecLockByte = (xbInt64) LK4026531838 - ulAppendRec;
        iRc = xbLock( XB_LOCK, llAppendRecLockByte, 1 );
        if( iRc != XB_NO_ERROR ){
          xbLock( XB_UNLOCK, LK4026531839, 1 );
          if( iRc == XB_LOCK_FAILED ){
            return iRc;
          } else {
            iErrorStop = 120;
            throw iRc;
          }
        }
        ulAppendLocked = ulAppendRec;    /* set the append lock switch */


   // } else { - other lock-table flavor options go here Clipper, Fox, etc - }

      } else {
        iErrorStop = 130;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }

    } else if( iLockFunction == XB_UNLOCK ){
      iErrorStop = 200;

      if( ulAppendLocked == 0 )        /* verify we have an active append lock */
        return XB_NO_ERROR;

      if( GetLockFlavor() == LK_DBASE ){
        xbInt64 llAppendRecLockByte =(xbInt64) LK4026531838 - ulAppendLocked;
        iRc = xbLock( XB_UNLOCK,  llAppendRecLockByte, 1 );
        if( iRc != XB_NO_ERROR ){
          xbLock( XB_UNLOCK, LK4026531839, 1 );
          iErrorStop = 220;
          throw iRc;
        }
        iRc = xbLock( XB_UNLOCK, LK4026531839, 1 );
        if( iRc != XB_NO_ERROR ){
          iErrorStop = 230;
          throw iRc;
        }

        ulAppendLocked = 0;             /* release the append lock switch */

   // } else { - other unlock-table flavor options go here Clipper, Fox, etc - }

      } else {
        iErrorStop = 290;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }
    } else {
      iErrorStop = 300;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }
  } catch( xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::LockAppendBytes() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }

  return iRc;
}

/************************************************************************/
//! @brief Lock Header 
/*!
  This routine locks the file header.
  \param iLockFunction XB_LOCK<br>XB_UNLOCK
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::LockHeader( xbInt16 iLockFunction )
{
  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;
  try{

    if( iLockFunction == XB_LOCK ){
      iErrorStop = 100;
      if( GetHeaderLocked())
        return XB_NO_ERROR; 

      iErrorStop = 110;
      if( GetLockFlavor() == LK_DBASE ){

        iRc = xbLock( XB_LOCK, LK4026531838, 1 );

        if( iRc != XB_NO_ERROR ){
          if( iRc == XB_LOCK_FAILED )
            return iRc;
          else{
            iErrorStop = 120;
            throw iRc;
          }
        }

      } else {
        iErrorStop = 130;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }

      SetHeaderLocked( xbTrue );

    } else if( iLockFunction == XB_UNLOCK ){

      iErrorStop = 200;
      if( !GetHeaderLocked())
        return XB_NO_ERROR;

      iErrorStop = 210;
      if( GetLockFlavor() == LK_DBASE ){
        iRc = xbLock( XB_UNLOCK, LK4026531838, 1 );
        if( iRc != XB_NO_ERROR ){
          iErrorStop = 220;
          throw iRc;
        }
      } else {
        iErrorStop = 230;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }
      SetHeaderLocked( xbFalse );
    } else {
      iErrorStop = 300;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }

  } catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::LockHeader() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;
}
/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Lock Index files.
/*!
  This routine locks all the index files.
  \param iLockFunction XB_LOCK<br>XB_UNLOCK
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::LockIndices( xbInt16 iLockFunction )
{
  // this function doesn't take into account any Lack Flavors other than DBASE,
  //  would need updated to supprot other lock flavors - Clipper, FoxPro etc

  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;

  try{
    xbIxList *ixLI = GetIxList();   // index list item

    while( ixLI ){
      if( iLockFunction == XB_LOCK ){

        #ifdef XB_NDX_SUPPORT
        if( *ixLI->sFmt == "NDX" ){
          if( !ixLI->ix->GetLocked()){
            if(( iRc = ixLI->ix->xbLock( XB_LOCK, LK4026531838, 1 )) != XB_NO_ERROR ){
              ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 );
              iErrorStop = 100;
              throw iRc;
            }
            ixLI->ix->SetLocked( xbTrue );
          }
        }
        #endif

        #ifdef XB_MDX_SUPPORT
        if( *ixLI->sFmt == "MDX" ){
          if( !ixLI->ix->GetLocked()){
            if(( iRc = ixLI->ix->xbLock( XB_LOCK, LK4026531838, 1 )) != XB_NO_ERROR ){
              ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 );
              iErrorStop = 100;
              throw iRc;
            }
            ixLI->ix->SetLocked( xbTrue );
          }
        }
        #endif

      } else if( iLockFunction == XB_UNLOCK ){

        #ifdef XB_NDX_SUPPORT
        if( *ixLI->sFmt == "NDX" ){
          if( ixLI->ix->GetLocked()){
            if(( iRc = ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 )) != XB_NO_ERROR ){
              iErrorStop = 100;
              throw iRc;
            }
            ixLI->ix->SetLocked( xbFalse );
          }
        }
        #endif

        #ifdef XB_MDX_SUPPORT
        if( *ixLI->sFmt == "MDX" ){
          if( ixLI->ix->GetLocked()){
            if(( iRc = ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 )) != XB_NO_ERROR ){
              iErrorStop = 100;
              throw iRc;
            }
            ixLI->ix->SetLocked( xbFalse );
          }
        }
        #endif

      }
      ixLI = ixLI->next;
    }

  } catch( xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::LockIndices() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;
}
#endif // XB_INDEX_SUPPORT
/************************************************************************/
#ifdef XB_MEMO_SUPPORT
//! @brief Lock Memo file.
/*!
  This routine locks the memo file for updates.
  \param iLockFunction XB_LOCK<br>XB_UNLOCK
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::LockMemo( xbInt16 iLockFunction ){
  if( MemoFieldsExist())
    return Memo->LockMemo( iLockFunction );
  else
    return XB_NO_ERROR;
}
#endif  // XB_MEMO_SUPPORT



/************************************************************************/
//! @brief Loc Record 
/*!
  This routine locks a record for update.
  \param iLockFunction XB_LOCK<br>XB_UNLOCK
  \param ulRecNo Record number to lock
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::LockRecord( xbInt16 iLockFunction, xbUInt32 ulRecNo )
{
  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;

  try{

    if( ulRecNo > ulNoOfRecs )
      return XB_INVALID_RECORD;

    if( iLockFunction == XB_LOCK ){
      iErrorStop = 100;

      if( lloRecLocks.KeyExists( ulRecNo ))
        return XB_NO_ERROR;

      if( GetLockFlavor() == LK_DBASE ){

        iRc = xbLock( XB_LOCK, LK4026531838 - ulRecNo, 1 );
        if( iRc != XB_NO_ERROR ){
          if( iRc == XB_LOCK_FAILED ){
            return iRc;
          } else {
            iErrorStop = 110;
            throw iRc;
          }
        } 
        // other lock-table flavor options go here Clipper, Fox, etc

      } else {
        iErrorStop = 120;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }

      // add the record lock info to the linked list chain of record locks
      iRc = lloRecLocks.InsertKey( ulRecNo );
      if( iRc != XB_NO_ERROR ){
        xbLock( XB_UNLOCK,  LK4026531838 - ulRecNo, 1 );
        iErrorStop = 130;
        throw iRc;
      }

    } else if( iLockFunction == XB_UNLOCK ){

      iErrorStop = 200;

      if( !lloRecLocks.KeyExists( ulRecNo ) )
        return XB_NO_ERROR;

      if( GetLockFlavor() == LK_DBASE ){
        iRc = xbLock( XB_UNLOCK,  LK4026531838 - ulRecNo, 1 );
        if( iRc != XB_NO_ERROR ){
          iErrorStop = 210;
          throw iRc;
        }
      } else {
        iErrorStop = 220;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }

      // remove the record lock info to the linked list chain of record locks
      // next line is crashing
      iRc = lloRecLocks.RemoveKey( ulRecNo );
      if( iRc != XB_NO_ERROR ){
        iErrorStop = 230;
        throw iRc;
      }

    } else {
      iErrorStop = 300;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }

  } catch( xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::LockRecord() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;
}

/************************************************************************/
//! @brief Lock table. 
/*!
  This routine locks the table for updates.

  \param iLockFunction XB_LOCK<br>XB_UNLOCK
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::LockTable( xbInt16 iLockFunction )
{
  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;

  try{
    if( iLockFunction == XB_LOCK ){
      iErrorStop = 100;
      if( GetTableLocked())
        return XB_NO_ERROR;  // table already locked 

      iErrorStop = 110;
      if( GetLockFlavor() == LK_DBASE ){

        // lOffset = LK4026531838;
        // iLen = 2;
        iRc = xbLock( XB_LOCK, LK4026531838, 2 );
        if( iRc != XB_NO_ERROR ){
          if( iRc == XB_LOCK_FAILED )
            return iRc;
          else{
            iErrorStop = 120;
            throw iRc;
          }
        }

        // lOffset = LK3026531838;
        // iLen = LK1000000000;
        iRc = xbLock( XB_LOCK, LK3026531838, LK1000000000);
        if( iRc != XB_NO_ERROR ){

          // lOffset = LK4026531838;
          // iLen = 2;
          xbLock( XB_UNLOCK, LK4026531838, 2 );
          if( iRc == XB_LOCK_FAILED ){
            return iRc;
          } else {
            iErrorStop = 130;
            throw iRc;
          }
        }

        // iRc = xbLock( XB_UNLOCK, lOffset, iLen );
        iRc = xbLock( XB_UNLOCK, LK3026531838, LK1000000000);
        if( iRc != XB_NO_ERROR ){
          // lOffset = LK4026531838;
          // iLen = 2;
          xbLock( XB_UNLOCK, LK4026531838, 2 );
          iErrorStop = 140;
          throw iRc;
        }

        // other lock-table flavor options go here Clipper, Fox, etc

      } else {
        iErrorStop = 190;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }
      SetTableLocked( xbTrue );

    } else if( iLockFunction == XB_UNLOCK ){

      iErrorStop = 200;
      if( !GetTableLocked())
        return XB_NO_ERROR;  // table already unlocked 

      iErrorStop = 210;
      if( GetLockFlavor() == LK_DBASE ){

        // lOffset = LK4026531838;
        // iLen = 2;
        iRc = xbLock( XB_UNLOCK, LK4026531838, 2 );
        if( iRc != XB_NO_ERROR ){
          iErrorStop = 220;
          throw iRc;
        }
      } else {
        iErrorStop = 290;
        iRc = XB_INVALID_OPTION;
        throw iRc;
      }
      SetTableLocked( xbFalse );

    } else {
      iErrorStop = 300;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }

  } catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::LockFile() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  return iRc;

}
#endif  // XB_LOCKING_SUPPORT


/************************************************************************/
//! @brief Check for existence of any memo fields. 
/*!
  \returns xbTrue - Memo fields exist.<br>xbFalse - Memo fields don't exist.
*/
xbBool xbDbf::MemoFieldsExist() const {


#ifdef XB_MEMO_SUPPORT
  if( iMemoFieldCnt > 0 )
    return xbTrue;
#endif
  return xbFalse;
}

/************************************************************************/
//! @brief Open a table/dbf file. 
/*!
  This routine sets the alias name to the same as the table name.

  \param sTableName Table name to open, Include the .dbf or .DBF extension. 
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::Open( const xbString & sTableName ) {
  return Open( sTableName, sTableName );
}

/************************************************************************/
//! @brief Open a table/dbf file. 
/*!
  \param sTableName Table name to open, Include the .dbf or .DBF extension. 
  \param sAlias Alias name to assign to this entry.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::Open( const xbString & sTableName, const xbString & sAlias ){

  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;

  try{
    if(( iRc = Open( sTableName, sAlias, XB_READ_WRITE, XB_MULTI_USER )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }

    // do any .INF data things on the file, like open indices
    #ifdef XB_INF_SUPPORT
    if(( iRc = LoadInfData()) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }
    xbUInt32 llNodeCnt = llInfData.GetNodeCnt();
    if( llNodeCnt > 0 ){
      xbString s2;
      xbLinkListNode<xbString> * llN = llInfData.GetHeadNode();
      for( xbUInt32 i = 0; i < llNodeCnt; i++ ){
        s2 = llN->GetKey();
		#ifdef XB_NDX_SUPPORT
        if(( iRc = OpenIndex( "NDX", s2 )) != XB_NO_ERROR ){
          iErrorStop = 120;
          throw iRc ;
        }
		#endif  // XB_NDX_SUPPORT
        llN = llN->GetNextNode();
      }
    }
    #endif // XB_INF_SUPPORT
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::Open() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Open an index.
/*!
  Open an index file for the dbf file.

  \param sIxType - "NDX" or "MDX"
  \param sFileName - File name of index,
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::OpenIndex( const xbString &sIxType, const xbString &sFileName ){

  // this routine is used to open indices and set up linkages

  xbInt16 iErrorStop = 0;
  xbInt16 iRc = XB_NO_ERROR;
  xbIx *pIx = NULL;

  try{
    xbString sType = sIxType;
    sType.ToUpperCase();

    if( sType == "" ){
      iErrorStop = 100;
      iRc = XB_INVALID_OPTION;
      throw iRc;

    #ifdef XB_NDX_SUPPORT
    } else if( sType == "NDX" ){
      pIx = new xbIxNdx( this );
      if(( iRc = pIx->Open( sFileName )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw iRc;
      }
    #endif

    #ifdef XB_MDX_SUPPORT
    } else if( sType == "MDX" ){

      pIx = new xbIxMdx( this );
      if(( iRc = pIx->Open( sFileName )) != XB_NO_ERROR ){
        iErrorStop = 120;
        throw iRc;
      }

    #endif

    } else {
      iErrorStop = 130;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }

    if(( iRc = AddIndex( pIx, sIxType )) != XB_NO_ERROR ){
      iErrorStop = 140;
      throw iRc;
    }
    if(( iRc = UpdateTagList()) != XB_NO_ERROR ){
      iErrorStop = 150;
      throw iRc;
    }
    pCurIx = pIx;
    sCurIxType = sIxType;
    vpCurIxTag = pIx->GetTag( 0 );
  }
  catch (xbInt16 iRc ){
    if( pIx ) delete pIx;
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::OpenIndex() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
#endif // XB_INDEX_SUPPORT



/************************************************************************/
//! @brief Pack dbf file. 
/*!
  This routine eliminates all deleted records from the file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::Pack()
{
  xbUInt32 ulDeletedRecCnt;
  return Pack(  ulDeletedRecCnt );
}


/************************************************************************/
//! @brief Pack dbf file. 
/*!
  This routine eliminates all deleted records from the file and clears
  out any unused blocks in the memo file if one exists.
  \param ulDeletedRecCnt - Output - number of recrods removed from the file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::Pack( xbUInt32 &ulDeletedRecCnt )
{
  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  xbUInt32 ulLastMovedRec = 0;
  xbUInt32 ulStartPos = 0;
  xbUInt32 ulLastPackedRec = 0;
  xbUInt32 ulMoveRec = 0;
  xbUInt32 ulRecCnt = 0;
  ulDeletedRecCnt = 0;

  #ifdef XB_LOCKING_SUPPORT
  xbBool bLocked = xbFalse;
  #endif // XB_LOCKING_SUPPORT

  try{
    if( !FileIsOpen() ){
      iErrorStop = 100;
      iRc = XB_DBF_FILE_NOT_OPEN;
      throw iRc;
    }

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw iRc;
      } else {
        bLocked = xbTrue;
      }
    }
    #endif

    if(( iRc = GetRecordCnt( ulRecCnt )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }

    xbBool bDone = xbFalse;
    for( xbUInt32 ulI = 1; ulI <= ulRecCnt && !bDone; ulI++ ){

      if(( iRc = GetRecord( ulI )) != XB_NO_ERROR ){
        iErrorStop = 130;
        throw iRc;
      }
      if( RecordDeleted()){
        ulDeletedRecCnt++;
        if( ulI > ulLastMovedRec )
          ulStartPos = ulI;
        else
          ulStartPos = ulLastMovedRec;

        iRc = GetNextRecord( 1, ulStartPos );

        if( iRc == XB_NO_ERROR ){
          ulMoveRec = ulCurRec;
        }
        else if( iRc == XB_EOF ){
          ulMoveRec = 0;
          bDone = xbTrue;
        }
        else{
          iErrorStop = 140;
          throw iRc;
        }
        if( ulMoveRec > 0 ){
          if(( iRc = DeleteRecord()) != XB_NO_ERROR ){
            iErrorStop = 150;
            throw iRc;
          }
          if(( iRc = PutRecord( ulMoveRec )) != XB_NO_ERROR ){
            iErrorStop = 160;
            throw iRc;
          }
          if(( iRc = UndeleteRecord()) != XB_NO_ERROR ){
            iErrorStop = 170;
            throw iRc;
          }
          if(( iRc = PutRecord( ulI )) != XB_NO_ERROR ){
            iErrorStop = 180;
            throw iRc;
          }
          ulLastPackedRec = ulI;
        }

      } else {
        ulLastPackedRec = ulI;
      }
    }

    if( ulLastPackedRec < ulRecCnt ){
      // update header record count

      xbDate d;
      cUpdateYY = (char) d.YearOf() - 1900;
      cUpdateMM = (char) d.MonthOf();
      cUpdateDD = (char) d.DayOf( XB_FMT_MONTH );
      ulNoOfRecs = ulLastPackedRec;

      // rewrite the header record 
      if(( iRc = WriteHeader( 1, 1 )) != XB_NO_ERROR ){
        iErrorStop = 190;
        throw iRc;
      }

      // truncate the file to the new size 
      if(( iRc = xbTruncate( uiHeaderLen + uiRecordLen * ulLastPackedRec )) != XB_NO_ERROR ){
        iErrorStop = 200;
        throw iRc;
      }
    }

    if( ulNoOfRecs > 0 ){
      if(( iRc = GetRecord( 1 )) != XB_NO_ERROR ){
        iErrorStop = 210;
        throw iRc;
      }
    }  else {
      BlankRecord();
      ulCurRec = 0;
    }

    #ifdef XB_MEMO_SUPPORT
    if( iMemoFieldCnt > 0 ){
      if(( iRc = Memo->PackMemo( 0 )) != XB_NO_ERROR ){
        iErrorStop = 220;
        throw iRc;
      }
    }
    #endif // XB_MEMO_SUPPORT

  }
  catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::Pack() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  #ifdef XB_LOCKING_SUPPORT
  if( bLocked ){
    LockTable( XB_UNLOCK );
  }
  #endif
  return iRc;
}

/************************************************************************/
//! @brief Write the current record to disk.
/*!
  This routine is used to write any updates to the current record buffer to disk.

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::PutRecord() {
   return PutRecord(ulCurRec);
}

/************************************************************************/
//! @brief Write record to disk.
/*!
  This routine is used to write a copy of the current record buffer to disk
  for a given record number.

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

xbInt16 xbDbf::PutRecord(xbUInt32 ulRecNo) 
{
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  try{

    if( ulRecNo < 1 ){
      iErrorStop = 100;
      throw XB_INVALID_RECORD;
    }

    xbUInt32 ulRecCnt;
    if(( iRc = GetRecordCnt( ulRecCnt )) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }

    if( ulRecNo > ulRecCnt ){
      iErrorStop = 120;
      throw XB_INVALID_RECORD;
    }

    if( iDbfStatus == XB_CLOSED ){
      iErrorStop = 130;
      iRc = XB_NOT_OPEN;
      throw iRc;
    }
    /* lock the database */
    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockHeader( XB_LOCK )) != XB_NO_ERROR ){
         throw iRc;
      }
    }
    #endif     // XB_LOCKING_SUPPORT

    if(( iRc = ReadHeader( 1, 1 )) != XB_NO_ERROR ){
      iErrorStop = 150;
      throw iRc;
    }

    // verify valid record number request
    if( ulRecNo > ulNoOfRecs || ulRecNo == 0L ){
      iErrorStop = 160;
      iRc = XB_INVALID_RECORD;
      throw iRc;
    }

    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock && !bTableLocked ){
      if(( iRc = LockRecord( XB_LOCK, ulRecNo )) != XB_NO_ERROR ){
        iErrorStop = 170;
        throw iRc;
        }

      #ifdef XB_INDEX_SUPPORT
      if(( iRc = LockIndices( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 180;
        throw iRc;
      }
      #endif  // XB_INDEX_SUPPORT
    }
    #endif  // XB_LOCKING_SUPPORT

    // build keys, check for duplicate keys, add keys
    #ifdef XB_INDEX_SUPPORT
    xbIxList *ixList = GetIxList();

    while( ixList ){
      if(( iRc = ixList->ix->CreateKeys( 2 )) != XB_NO_ERROR ){
        iErrorStop = 190;
        throw iRc;
      }
      iRc = ixList->ix->CheckForDupKeys();
      if( iRc != 0 ){
        if( iRc < 0 ){
          iErrorStop = 200;
          throw iRc;
        }
        throw XB_KEY_NOT_UNIQUE;
      }
      ixList = ixList->next;
    }

    ixList = GetIxList();
    while( ixList ){

      if(( iRc = ixList->ix->AddKeys( ulCurRec )) != XB_NO_ERROR ){
        iErrorStop = 210;
        throw iRc;
      }
      ixList = ixList->next;
    }

    ixList = GetIxList();
    while( ixList ){
      if(( iRc = ixList->ix->DeleteKeys()) != XB_NO_ERROR ){
        iErrorStop = 220;
        throw iRc;
      }
      ixList = ixList->next;
    }
    #endif     // XB_INDEX_SUPPORT

    // update latest header date if changed
    xbDate d;
    if( (cUpdateYY != (char)(d.YearOf() - 1900)) || (cUpdateMM != (char) d.MonthOf()) || (cUpdateDD != (char)d.DayOf( XB_FMT_MONTH))){
      cUpdateYY = (char) d.YearOf() - 1900;
      cUpdateMM = (char) d.MonthOf();
      cUpdateDD = (char) d.DayOf( XB_FMT_MONTH );
      // rewrite the header record - first 8 bytes
      if(( iRc = WriteHeader( 1, 1 )) != XB_NO_ERROR ){
        iErrorStop = 70;
        throw iRc;
      }
    }

    // update record
    iRc = xbFseek( (uiHeaderLen+(( (xbInt64) ulRecNo-1L ) * uiRecordLen )),0 );
    if( iRc != XB_NO_ERROR ){
      iErrorStop = 240;
      throw iRc;
    }

    if( xbFwrite( RecBuf, uiRecordLen, 1 ) != XB_NO_ERROR ){
      iErrorStop = 250;
      iRc = XB_WRITE_ERROR;
      throw iRc;
    }

    #ifdef XB_MEMO_SUPPORT
    if( MemoFieldsExist() ){
      if(( iRc = Memo->Commit()) != XB_NO_ERROR ){
        iErrorStop = 260;
        throw iRc;
      }
    }
    #endif

    ulCurRec = ulRecNo;
    iDbfStatus = XB_OPEN;
  }

  catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED && iRc != XB_KEY_NOT_UNIQUE ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::PutRecord() Exception Caught. Error Stop = [%d] iRc = [%d] record = [%d]", iErrorStop, iRc, ulRecNo );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }

  #ifdef XB_LOCKING_SUPPORT
  if( iAutoLock ){
    LockHeader( XB_UNLOCK );
    LockAppend( XB_UNLOCK );
    LockRecord( XB_UNLOCK, ulRecNo );
	#ifdef XB_INDEX_SUPPORT
    LockIndices( XB_UNLOCK );
	#endif   //  XB_INDEX_SUPPORT
  }
  #endif   // XB_LOCKING_SUPPORT

  return iRc;
}

/************************************************************************/
//! @brief Read dbf file header information.
/*!
   This method assumes the header has been locked appropriately
   in a multi user environment


  \param iPositionOption 0 - Don't fseek to beginning of file before read.<br>
      1 - Start from beginning of file.

  \param iReadOption 0 - Read entire 32 byte header<br>
                     1 - Read first eight bytes which includes the last update date and number of records.

  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::ReadHeader( xbInt16 iPositionOption, xbInt16 iReadOption ){

  char buf[32];
  size_t iReadSize;

  if(iPositionOption)
    xbRewind();
  if( iReadOption == 1 )
    iReadSize = 8;
  else
    iReadSize = 32;

  if(xbFread(buf, iReadSize, 1) != XB_NO_ERROR)
    return XB_READ_ERROR;
  memcpy(&cVersion, buf, 4);
  ulNoOfRecs  = eGetUInt32(&buf[4]);
  if( iReadOption == 1 )
    return XB_NO_ERROR;

  uiHeaderLen      = eGetUInt16(&buf[8]);
  uiRecordLen      = eGetUInt16(&buf[10]);
  cTransactionFlag = buf[14];
  cEncryptionFlag  = buf[15];
  cIndexFlag       = buf[28];
  cLangDriver      = buf[29];
  return XB_NO_ERROR;
}

/************************************************************************/
//! @brief Return record deletion status.
/*!
  This routine returns the record deletion status.
  \param iOpt 0 = Current record buffer, 1 = Original record buffer
  \returns xbTrue - Record deleted.<br>xbFalse - Record not deleted.
*/
xbInt16 xbDbf::RecordDeleted( xbInt16 iOpt ) const {
  if( !iOpt && RecBuf && RecBuf[0] == 0x2a )
    return xbTrue;
  else if( iOpt && RecBuf2 && RecBuf2[0] == 0x2a )
    return xbTrue;
  else
    return xbFalse;
}

/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Remove an index from the internal list of indices for this table
/*
    The index list is used during any table update process to update any open
    index file.  Index files can contain one or more tags.

  \param ixIn Pointer to index object for a given index file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::RemoveIndex( xbIx * ixIn ){

  xbIxList *p = ixList;
  // if index is the first entry in the list
  if( ixList->ix == ixIn ){
    ixList = ixList->next;
    delete p->sFmt;
    delete p->ix;
    free( p );
    return XB_NO_ERROR;
  }

  // spin down to the correct ix
  xbIxList *p2 = NULL;
  while( p && p->ix != ixIn ){
    p2 = p;
    p = p->next;
  }
  if( p ){
    p2->next = p->next;
    delete p->sFmt;
    delete p->ix;
    free( p );
  }
  return XB_NO_ERROR;
}
#endif // XB_INDEX_SUPPORT

/************************************************************************/
// @brief Reset number of records.
/*
  Protected method.  Resets number of records to 0.
  \returns void
*/
void xbDbf::ResetNoOfRecords() {
  ulNoOfRecs = 0UL;
}

/************************************************************************/
#ifdef XB_INF_SUPPORT
// @brief Update .INF data file.
/*
  Protected method.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbDbf::SaveInfData(){

  xbInt16  iRc = 0;
  xbInt16  iErrorStop = 0;
  xbFile fMd( xbase );

  try{

    xbUInt32 llNodeCnt = llInfData.GetNodeCnt();

    xbString sInfFileName;
    if(( iRc = GetInfFileName( sInfFileName )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }

    // open the file
    if(( iRc = fMd.xbFopen( "w", sInfFileName, GetShareMode())) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }

    xbString s1;
    xbString s2;
    s2.Sprintf( "[dbase]%c%c", 0x0d, 0x0a );
    if(( iRc = fMd.xbFputs( s2 )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }

    // for each entry in the linked list, write a line
    xbLinkListNode<xbString> * llN = llInfData.GetHeadNode();
    for( xbUInt32 i = 0; i < llNodeCnt; i++ ){
      s2 = llN->GetKey();
      if( i > 0 )
        s1.Sprintf( "NDX%d=%s%c%c", i, s2.Str(), 0x0d, 0x0a );
          else
        s1.Sprintf( "NDX=%s%c%c", s2.Str(), 0x0d, 0x0a );

      if(( iRc = fMd.xbFputs( s1 )) != XB_NO_ERROR ){
        iErrorStop = 130;
        throw iRc;
      }
      llN = llN->GetNextNode();
    }

    // close the file
    if(( iRc = fMd.xbFclose()) != XB_NO_ERROR ){
      iErrorStop = 140;
      throw iRc;
    }

  } catch( xbInt16 iRc ){
    if( fMd.FileIsOpen())
      fMd.xbFclose();
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::SaveInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
#endif // XB_INF_SUPPORT
/************************************************************************/
//! @brief Set auto commit.
/*!
  This routine sets the auto commit setting for this table.
  \returns XB_NO_ERROR;
*/
xbInt16 xbDbf::SetAutoCommit( xbBool iAutoCommit ) {
  this->iAutoCommit = iAutoCommit;
  return XB_NO_ERROR;
}


/************************************************************************/
//! @brief Set auto lock.
/*!
  This routine sets the auto lock setting for this table.
  There is an overall system level auto lock default setting and each table
  can have it's own autolock setting.  This method controls the table level
  auto lock setting.

  \param iAutoLock 1 - Use auto lock for this table.<br>
                   0 - Don't use auto lock for this table.<br>
                   -1 - (minus one) Use system default.<br>
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
#ifdef XB_LOCKING_SUPPORT
void xbDbf::SetAutoLock( xbInt16 iAutoLock ){
  if( iAutoLock == -1 )
    this->iAutoLock = xbase->GetDefaultAutoLock();
  else
    this->iAutoLock = iAutoLock;
}
#endif


/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Set the current tag for the dbf file.
/*!
  \param sTagName - Tag Name
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::SetCurTag( const xbString &sTagName ){

  if( sTagName == "" ){
    SetCurTag( "", 0, 0 );
    return XB_NO_ERROR;

  }  else {
    xbLinkListNode<xbTag *> *llN = GetTagList();
    xbTag *pTag;
    while( llN ){
      pTag = llN->GetKey();
      if( pTag->GetTagName() == sTagName ){
        SetCurTag( pTag->GetType(), pTag->GetIx(), pTag->GetVpTag());
        return XB_NO_ERROR;
      }
      llN = llN->GetNextNode();
    }
  }

  return XB_INVALID_TAG;
}

/************************************************************************/
//! @brief Set the current tag for the dbf file.
/*!

  \param sIxType - One of "NDX" or MDX",
  \param pIx - Pointer to index object.
  \param vpTag - Pointer to tag object.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

void xbDbf::SetCurTag( const xbString &sIxType, xbIx *pIx, void *vpTag ){
    pCurIx     = pIx;
    vpCurIxTag = vpTag;
    sCurIxType.Set( sIxType );
}
#endif // XB_INDEX_SUPPORT

/************************************************************************/
//! @brief Set the header locked status.
/*!
  \param bHeaderLocked xbTrue - Locked<br>xbFalse - Not locked.
  \returns void
*/
#ifdef XB_LOCKING_SUPPORT
void xbDbf::SetHeaderLocked( xbBool bHeaderLocked ){
  this->bHeaderLocked = bHeaderLocked;
}
#endif

/************************************************************************/
//! @brief Set lock flavor.
/*!
  This routine is for future expansion.
  \param iLockFlavor 1 - Use Dbase (tm) style locking.
  \returns void
*/
#ifdef XB_LOCKING_SUPPORT
void xbDbf::SetLockFlavor( xbInt16 iLockFlavor ){
  this->iLockFlavor = iLockFlavor;
}
#endif

/************************************************************************/
//! @brief Set table locked status.
/*!
  \param bTableLocked - xbTrue Table locked.<br>xbFalse Table unlocked.
  \returns void
*/
#ifdef XB_LOCKING_SUPPORT
void xbDbf::SetTableLocked( xbBool bTableLocked ){
  this->bTableLocked = bTableLocked;
}
#endif
/************************************************************************/
//! @brief Undelete all records. 
/*!
  This routine will remove the deletion flag on any deleted records in the table.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::UndeleteAllRecords(){
  return DeleteAll( 1 );
}

/************************************************************************/
//! @brief Undelete one record. 
/*!
  This routine will undelete the current record, if it is deleted.
  \returns XB_NO_ERROR<br>XB_INVALID_RECORD
*/
xbInt16 xbDbf::UndeleteRecord()
{
  if( RecBuf && ulCurRec > 0 ){
    if( RecBuf[0] != 0x20 ){
      if( iDbfStatus != XB_UPDATED ){
        iDbfStatus = XB_UPDATED;
        memcpy( RecBuf2, RecBuf, uiRecordLen );  // save off original before making updates
      }
      RecBuf[0] = 0x20;
    }
    return XB_NO_ERROR;
  }
  else
    return XB_INVALID_RECORD;
}

/************************************************************************/
#ifdef XB_MEMO_SUPPORT
//! @brief Update memo field
/*!
  This routine updates a memo field.
  \param iFieldNo - Memo field number to update.
  \param sMemoData - Memo data for update.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::UpdateMemoField( xbInt16 iFieldNo, const xbString &sMemoData ){
  return Memo->UpdateMemoField( iFieldNo, sMemoData );
}
/************************************************************************/
//! @brief Update memo field
/*!
  This routine updates a memo field.
  \param sFieldName - Memo field name to update.
  \param sMemoData - Memo data for update.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::UpdateMemoField( const xbString & sFieldName, const xbString & sMemoData ){
  return Memo->UpdateMemoField( GetFieldNo( sFieldName ), sMemoData );
}
#endif


/************************************************************************/
#ifdef XB_INDEX_SUPPORT
//! @brief Update SchemaIxFlag
/*!
  This routine can be called from the DeleteTag routine if a tag has been deleted and the flag needs reset
  \param iFldNo - Which field the index flag needs changed on
  \param cVal   - Value to change it to
*/

void xbDbf::UpdateSchemaIxFlag( xbInt16 iFldNo, unsigned char cVal ){
  if( cVal != 0x00 || cVal != 0x01 )
    SchemaPtr[iFldNo].cIxFlag = cVal;
}

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

//! @brief Update tag list. 
/*!
  This routine updates the internal tag list of open index tags.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::UpdateTagList(){

  xbInt16 iErrorStop = 0;
  xbInt16 iRc = XB_NO_ERROR;
  xbInt32 lTagCnt;

  try{
   ClearTagList();

   // For each active index
   xbIxList *p = GetIxList();
   xbIx *ixp;
   while( p ){
     ixp = p->ix;
     // for each tag within the file
     lTagCnt = ixp->GetTagCount();
     for( xbInt32 l = 0; l < lTagCnt; l++ ){
       xbTag *pTag = new xbTag( ixp, ixp->GetTag( l ), *p->sFmt, ixp->GetTagName( ixp->GetTag( l )),
            ixp->GetKeyExpression( ixp->GetTag( l )), ixp->GetKeyFilter( ixp->GetTag( l )),
            ixp->GetUnique( ixp->GetTag( l )), ixp->GetSortOrder( ixp->GetTag( l )));

       // append it to the llTags list
       llTags.InsertAtEnd( pTag );
      }
      p = p->next;
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbdbf::UpdateTagList() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
#endif   // XB_INDEX_SUPPORT

/************************************************************************/
// @brief Write Header 
/*
  Protected method.

  \param iPositionOption  0 - Don't fseek to beginning of file before read.<br>
                          1 - Go to beginning of file before read.
  \param iWriteOption     0 - Write entire 32 byte header.<br>
                          1 - Write first eight bytes needed for table updates - last update date and number of records.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::WriteHeader( xbInt16 iPositionOption, xbInt16 iWriteOption )
{
   char buf[32];
   xbInt32 lWriteLen;

   if(iPositionOption)
     xbRewind();

   memset(buf, 0, 32);
   if( iWriteOption == 1 )
     lWriteLen = 8;
   else{
     lWriteLen = 32;
     ePutUInt16( &buf[8],  uiHeaderLen );
     ePutUInt16( &buf[10], uiRecordLen );
     buf[14] = cTransactionFlag;
     buf[15] = cEncryptionFlag;
     buf[28] = cIndexFlag;
     buf[29] = cLangDriver;
   }
   memcpy(&buf[0],  &cVersion, 4);
   ePutUInt32( &buf[4],  ulNoOfRecs);
   if(xbFwrite(buf, (size_t) lWriteLen, 1) != XB_NO_ERROR)
     return XB_WRITE_ERROR;

   return XB_NO_ERROR;
}
/************************************************************************/
//! @brief Zap (remove) everything from the file,
/*!
  This routine eliminates everything from the dbf file and dbt memo file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbDbf::Zap(){

  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;

  #ifdef XB_LOCKING_SUPPORT
  xbBool bLocked = xbFalse;
  #endif // XB_LOCKING_SUPPORT

  try{
    if( iDbfStatus != XB_OPEN ){
      iErrorStop = 100;
      iRc = XB_DBF_FILE_NOT_OPEN;
      throw iRc;
    }
    #ifdef XB_LOCKING_SUPPORT
    if( iAutoLock ){
      if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw iRc;
      }
    }
    #endif

    xbDate d;
    cUpdateYY = (char) d.YearOf() - 1900;
    cUpdateMM = (char) d.MonthOf();
    cUpdateDD = (char) d.DayOf( XB_FMT_MONTH );
    ulNoOfRecs = 0;

    // rewrite the header record 
    if(( iRc = WriteHeader( 1, 1 )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }

    // truncate the file to the new size 
    if(( iRc = xbTruncate( uiHeaderLen  )) != XB_NO_ERROR ){
      iErrorStop = 130;
      throw iRc;
    }
    BlankRecord();
    ulCurRec = 0;

    #ifdef XB_MEMO_SUPPORT
    if( iMemoFieldCnt ){
      if(( iRc = Memo->Zap()) != XB_NO_ERROR ){
        iErrorStop = 140;
        throw iRc;
      }
    }
    #endif
    #ifdef XB_INDEX_SUPPORT
    xbLinkListNode<xbTag *> *llN = GetTagList();
    xbTag *pTag;
    xbIx  *pIx;
    void  *vpTag;
    while( llN ){
      pTag  = llN->GetKey();
      pIx   = pTag->GetIx();
      vpTag = pTag->GetVpTag();
      if(( iRc = pIx->Reindex( &vpTag )) != XB_NO_ERROR ){
        iErrorStop = 60;
        throw iRc;
      }
      llN = llN->GetNextNode();
    }

    #endif // XB_INDEX_SUPPORT
  }
  catch (xbInt16 iRc ){
    if( iRc != XB_LOCK_FAILED ){
      xbString sMsg;
      sMsg.Sprintf( "xbdbf::Zap() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
      xbase->WriteLogMessage( sMsg.Str() );
      xbase->WriteLogMessage( GetErrorMessage( iRc ));
    }
  }
  #ifdef XB_LOCKING_SUPPORT
  if( bLocked ){
    LockTable( XB_UNLOCK );
  }
  #endif // XB_LOCKING_SUPPORT
  return iRc;
}
/************************************************************************/
}   /* namespace */