summaryrefslogtreecommitdiff
path: root/src/core/xbmemo4.cpp
blob: d02df997bf7b4050f9f0f4f3ec689e0a985148bd (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
/* xbmemo4.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

  This class is used for support dBASE V4 memo files

*/

#include "xbase.h"

#ifdef XB_MEMO_SUPPORT
#ifdef XB_DBF4_SUPPORT

namespace xb{

/***********************************************************************/
//! @brief Class Constructor.
/*!
  \param dbf Pointer to dbf instance.
  \param sFileName Memo file name.
*/
xbMemoDbt4::xbMemoDbt4( xbDbf * dbf, xbString const & sFileName ) : xbMemo( dbf, sFileName ){
 iMemoFileType = 4;
 SetBlockSize( dbf->GetCreateMemoBlockSize() );
}

/***********************************************************************/
//! @brief Class Deconstructor.
xbMemoDbt4::~xbMemoDbt4(){}

/***********************************************************************/
//! @brief Abort.
/*!
  Abort any pending updates to the memo file.
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt4::Abort(){

  xbInt16  rc = XB_NO_ERROR;
  xbInt16  iErrorStop = 0;
  xbUInt32 ulBlockNo;

  try{
    xbUInt32 ulNodeCnt = llNewBlocks.GetNodeCnt();
    for( xbUInt32 l = 0; l < ulNodeCnt; l++ ){
      if(( rc = llNewBlocks.RemoveFromFront( ulBlockNo )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw rc;
      }
      if(( rc = FreeMemoBlockChain( ulBlockNo )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw rc;
      }
    }
    llOldBlocks.Clear();
  }

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

/***********************************************************************/
//! @brief Commit changes to memo file.
/*!
  Commit any pending updates to the memo file.
  \returns XB_NO_ERROR.
*/
xbInt16 xbMemoDbt4::Commit(){

  xbInt16  rc = XB_NO_ERROR;
  xbInt16  iErrorStop = 0;
  xbUInt32 ulBlockNo;

  try{
    xbUInt32 ulNodeCnt = llOldBlocks.GetNodeCnt();
    for( xbUInt32 l = 0; l < ulNodeCnt; l++ ){
      if(( rc = llOldBlocks.RemoveFromFront( ulBlockNo )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw rc;
      }
      if(( rc = FreeMemoBlockChain( ulBlockNo )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw rc;
      }
    }
    llNewBlocks.Clear();
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::Commit() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Create memo file.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::CreateMemoFile(){

  xbInt16 iErrorStop = 0;
  xbInt16 rc = XB_NO_ERROR;
  char cBuf[4];

  try{
    if(( rc = xbFopen( "w+b", dbf->GetShareMode() )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }

    ulHdrNextBlock = 1L;
    ePutUInt32( cBuf, ulHdrNextBlock );
    if(( rc = xbFwrite( cBuf, 4, 1 ))!= XB_NO_ERROR ){
      iErrorStop = 110;
      xbFclose();
      throw rc;
    }
    for(int i = 0; i < 4; i++ )
      xbFputc( 0x00 );
    GetFileNamePart( sDbfFileNameWoExt );
    sDbfFileNameWoExt.PadRight( ' ', 8 );                  // need at least eight bytes of name
    sDbfFileNameWoExt = sDbfFileNameWoExt.Mid( 1, 8 );     // need no more than eight bytes of name
    for( int i = 1; i < 9; i++ )
      xbFputc( sDbfFileNameWoExt[i] );

    for(int i = 0; i < 4; i++ )
      xbFputc( 0x00 );

    ePutInt16( cBuf, GetBlockSize());
    if(( rc = xbFwrite( cBuf, 2, 1 ))!= XB_NO_ERROR ){
      iErrorStop = 120;
      xbFclose();
      throw rc;
    }
    for( xbUInt32 i = 0; i < (GetBlockSize() - 22); i++ )
      xbFputc( 0x00 );
    if(( mbb = (void *) malloc( GetBlockSize())) == NULL ){
      rc = XB_NO_MEMORY;
      iErrorStop = 130;
      return XB_NO_MEMORY;
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::CreateMemoFile() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
    xbFclose();
  }
  return rc;
}
/***********************************************************************/
#ifdef XB_DEBUG_SUPPORT
//! @brief Dump memo file header.
/*!
  \returns XB_NO_ERROR
*/

xbInt16 xbMemoDbt4::DumpMemoFreeChain() 
{
  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbUInt32  ulCurBlock, ulLastDataBlock;

  try{
    if(( rc = ReadDbtHeader(1)) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }
    if(( rc = CalcLastDataBlock( ulLastDataBlock )) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw rc;
    }

    ulCurBlock = ulHdrNextBlock;
    std::cout << "**********************************" << std::endl;
    std::cout << "Head Node Next Block = " << ulCurBlock      << std::endl;;
    std::cout << "Total blocks in file = " << ulLastDataBlock << std::endl;

    while( ulCurBlock < ulLastDataBlock ){
      if(( rc = ReadBlockHeader( ulCurBlock, 2 )) != XB_NO_ERROR ){
        iErrorStop = 120;
        throw rc;
      }
      std::cout << "**********************************" << std::endl;
      std::cout << "This Free Block = [" << ulCurBlock      << "] contains [" << ulFreeBlockCnt << "] block(s)" << std::endl;
      std::cout << "Next Free Block = [" << ulNextFreeBlock << "]" << std::endl;
      ulCurBlock = ulNextFreeBlock;
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::UpdateMemoField() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return XB_NO_ERROR;
}

//! @brief Dump memo internals.
/*!
  \returns XB_NO_ERROR
*/

xbInt16 xbMemoDbt4::DumpMemoInternals() {

  xbLinkListNode<xbUInt32> *llPtr;
  xbInt16 iNodeCnt;

  llPtr    = llOldBlocks.GetHeadNode();
  iNodeCnt = llOldBlocks.GetNodeCnt();

  std::cout << "Link List Old Blocks - " << iNodeCnt << " nodes" << std::endl;
  for( xbInt16 i = 0; i < iNodeCnt; i++ ){
    std::cout << llPtr->GetKey() << ",";
    llPtr = llPtr->GetNextNode();
  }
  std::cout << std::endl;

  llPtr    = llNewBlocks.GetHeadNode();
  iNodeCnt = llNewBlocks.GetNodeCnt();
  std::cout << "Link List New Blocks - " << iNodeCnt << " nodes" << std::endl;
  for( xbInt16 i = 0; i < iNodeCnt; i++ ){
    std::cout << llPtr->GetKey() << ",";
    llPtr = llPtr->GetNextNode();
  }
  std::cout << std::endl;

  return XB_NO_ERROR;
}
#endif  // XB_DEBUG_SUPPORT

/***********************************************************************/
//! @brief Dump memo file header.
/*!
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt4::DumpMemoHeader(){

  xbInt16  rc = XB_NO_ERROR;
  xbUInt32 ulLastDataBlock;
  CalcLastDataBlock( ulLastDataBlock );

  if(( rc = ReadDbtHeader( 1 )) != XB_NO_ERROR )
    return rc;
  std::cout << "Version 4 Memo Header Info" << std::endl;
  std::cout << "Memo File Name       = "    << GetFqFileName()         << std::endl;
  std::cout << "Hdr Next Avail Block = "    << ulHdrNextBlock          << std::endl;
  std::cout << "Block Size           = "    << GetBlockSize()          << std::endl;
  std::cout << "Dbf File Name wo Ext = "    << sDbfFileNameWoExt.Str() << std::endl;
  std::cout << "Last Data Block      = "    << ulLastDataBlock         << std::endl;
  return rc;
}

/************************************************************************/
//! @brief Find an empty set of blocks in the free block chain
/*!
  This routine searches thruugh the free node chain in a dbase IV type   
  memo file searching for a place to grab some free blocks for reuse 

  \param ulBlocksNeeded The size to look in the chain for.
  \param ulLastDataBlock is the last data block in the file, enter 0
                    for the routine to calculate it.
  \param ulLocation The location it finds.
  \param ulPreviousNode Block number of the node immediately previous to this node in the chain.<br>
                        0 if header node
  \param bFound Output xbFalse - Spot not found in chain.<br>
                       xbTrue - Spot found in chain.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemoDbt4::FindBlockSetInChain( xbUInt32 ulBlocksNeeded,
    xbUInt32 &ulLastDataBlock, xbUInt32 &ulLocation, xbUInt32 &ulPrevNode, xbBool &bFound ){
  xbInt16  rc = XB_NO_ERROR;
  xbInt16  iErrorStop = 0;
  xbUInt32 ulCurNode;

  try{
    ulPrevNode = 0L;
    if( ulLastDataBlock == 0 ){
      /* Determine last good data block */
      if(( rc = CalcLastDataBlock( ulLastDataBlock )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw rc;
      }
    }
    if( ulHdrNextBlock < ulLastDataBlock ){
      ulCurNode = ulHdrNextBlock;

      if(( rc = ReadBlockHeader( ulHdrNextBlock, 2 )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw rc;
      }
      while( ulBlocksNeeded > ulFreeBlockCnt && ulNextFreeBlock < ulLastDataBlock ){
        ulPrevNode = ulCurNode;
        ulCurNode  = ulNextFreeBlock;
        if(( rc = ReadBlockHeader( ulNextFreeBlock, 2 )) != XB_NO_ERROR ){
          iErrorStop = 120;
          throw rc;
        }
      }
      if( ulBlocksNeeded <= ulFreeBlockCnt ){
        ulLocation = ulCurNode;
        //    PreviousNode = lPrevNode;
        bFound = xbTrue;
      } else {   // no data found and at end of chain 
        ulPrevNode = ulCurNode;
        bFound = xbFalse;
      }
    } else {
      bFound = xbFalse;
    }
  }
   catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::FindBlockSetInChain() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Free a block.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::FreeMemoBlockChain( xbUInt32 ulBlockNo ){
  xbUInt32 ulLastDataBlock;
  return FreeMemoBlockChain( ulBlockNo, ulLastDataBlock );
}

/***********************************************************************/
//! @brief Free a block.
/*!
  \param ulBlockNo The block number being deleted.
  \param ulLastDataBlock Output - Last free block number,prior to this block.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemoDbt4::FreeMemoBlockChain( xbUInt32 ulBlockNo, xbUInt32 &ulLastDataBlock )
{
  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbUInt32 ulNoOfFreedBlocks;
  xbUInt32 ulLastFreeBlock = 0;
  xbUInt32 ulLastFreeBlockCnt = 0;
  xbUInt32 ulSaveNextFreeBlock;

  // iFieldNo - The field no m\bing deleted
  // iBlockNo - The block number being deleted
  // iNoOfFreeBlocks - The number of blocks being freed with this delete
  // iLastDataBlock - The next block number to allocate if more blocks needed
  // iHdrNextBlock - The head pointer in the main header block
  // iNextFreeBlock - The block that is immediately following the current free block to be added
  // iLastFreeBlock - Last free block number,prior to this block
  // iLastFreeBlockCnt - Last free block number of blocks

  try{

    if( ulBlockNo <= 0 ){
      iErrorStop = 100;
      rc =XB_INVALID_BLOCK_NO;
      throw rc;
    }

    /* Load the first block */
    if(( rc = ReadBlockHeader( ulBlockNo, 1 )) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw rc;
    }

    if( (ulFieldLen) % GetBlockSize() )
      ulNoOfFreedBlocks = ((ulFieldLen) / GetBlockSize()) + 1L;
    else
      ulNoOfFreedBlocks = (ulFieldLen) / GetBlockSize();

    /* Determine last good data block */
    if(( rc = CalcLastDataBlock( ulLastDataBlock )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw rc;
    }

    if(( rc = ReadDbtHeader( 0 )) != XB_NO_ERROR ){
      iErrorStop = 130;
      throw rc;
    }

    // Not an empty node chain, position to correct location in chain
    ulNextFreeBlock = ulHdrNextBlock;
    while( ulBlockNo > ulNextFreeBlock && ulBlockNo < ulLastDataBlock ){
      ulLastFreeBlock = ulNextFreeBlock;

      if(( rc = ReadBlockHeader( ulNextFreeBlock, 2 )) != XB_NO_ERROR ){
        iErrorStop = 140;
        return rc;
      }
      ulLastFreeBlockCnt = ulFreeBlockCnt;
    }

    // One of two outcomes at this point
    //  A)  This block is combined with the next free block chain, and points to the free chain after the next free block
    //  B)  This block is not combined with the next free block chain, and points to the next block 
    //      (which could be the last block

    // should next block should be concatonated onto the end of this set?
    ulSaveNextFreeBlock = ulNextFreeBlock;
    if(( ulBlockNo + ulNoOfFreedBlocks ) == ulNextFreeBlock && ulNextFreeBlock < ulLastDataBlock ){
      if(( rc = ReadBlockHeader( ulNextFreeBlock, 2 )) != XB_NO_ERROR ){
        iErrorStop = 150;
        throw rc;
      }
      ulNoOfFreedBlocks += ulFreeBlockCnt;
      ulSaveNextFreeBlock = ulNextFreeBlock;
    }

    // if this is the first set of free blocks
    if( ulLastFreeBlock == 0 ){
      // 1 - write out the current block
      // 2 - update header block
      // 3 - write header block
      // 4 - update data field

      ePutUInt32( (char *) mbb,   ulSaveNextFreeBlock );
      ePutUInt32( (char *) mbb+4, ulNoOfFreedBlocks );
      if(( rc = WriteBlock( ulBlockNo, 8, mbb )) != XB_NO_ERROR ){
        iErrorStop = 160;
        throw rc;
      }

      ulHdrNextBlock = ulBlockNo;
      if(( rc = UpdateHeadNextNode()) != XB_NO_ERROR ){
        iErrorStop = 170;
        throw rc;
      }
      return XB_NO_ERROR; 
    }

    /* determine if this block set should be added to the previous set */
    if(( ulLastFreeBlockCnt + ulLastFreeBlock ) == ulBlockNo ){
      if(( rc = ReadBlockHeader( ulLastFreeBlock, 2 )) != XB_NO_ERROR ){
        iErrorStop = 180;
        throw rc;
      }
      ulFreeBlockCnt += ulNoOfFreedBlocks;

      ePutUInt32( (char *) mbb,   ulSaveNextFreeBlock );
      ePutUInt32( (char *) mbb+4, ulFreeBlockCnt );
      if(( rc = WriteBlock( ulLastFreeBlock, 8, mbb )) != XB_NO_ERROR ){
        iErrorStop = 190;
        throw rc;
      }
      return XB_NO_ERROR;
    }

    /* insert into the chain */
    /* 1 - set the next bucket on the current node         */
    /* 2 - write this node                                 */
    /* 3 - go to the previous node                         */
    /* 4 - insert this nodes id into the previous node set */
    /* 5 - write previous node                             */

    ePutUInt32( (char *) mbb,   ulSaveNextFreeBlock );
    ePutUInt32( (char *) mbb+4, ulNoOfFreedBlocks );
    if(( rc = WriteBlock( ulBlockNo, 8, mbb )) != XB_NO_ERROR ){
      iErrorStop = 200;
      throw rc;
    }

    if(( rc = ReadBlockHeader( ulLastFreeBlock, 2 )) != XB_NO_ERROR ){
      iErrorStop = 210;
      throw rc;
    }

    ePutUInt32( (char *) mbb,   ulBlockNo );
    if(( rc = WriteBlock( ulLastFreeBlock, 8, mbb )) != XB_NO_ERROR ){
      iErrorStop = 220;
      throw rc;
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::DeleteMemoField() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/************************************************************************/
//! @brief Get a set of blocks from the free block chain.
/*!
   This routine grabs a set of blocks out of the free block chain.

  \param ulBlocksNeeded The number of blocks requested.
  \param ulLocation
  \param ulPrevNode
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/


xbInt16 xbMemoDbt4::GetBlockSetFromChain( xbUInt32 ulBlocksNeeded,
   xbUInt32 ulLocation, xbUInt32 ulPrevNode )
{
  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbUInt32  ulNextFreeBlock2;
  xbUInt32  ulNewFreeBlocks;
  xbUInt32  ulSaveNextFreeBlock;

  try{
    if(( rc = ReadBlockHeader( ulLocation, 2 )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }

    if( ulBlocksNeeded == ulFreeBlockCnt ){  // grab this whole set of blocks
      if( ulPrevNode == 0 ){                // first in the chain
        ulHdrNextBlock = ulNextFreeBlock;
        if(( rc = UpdateHeadNextNode()) != XB_NO_ERROR ){
          iErrorStop = 110;
          throw rc;
        }
      }
      else                                // remove out of the middle or end 
      {
        ulNextFreeBlock2 = ulNextFreeBlock;
        if(( rc = ReadBlockHeader( ulPrevNode, 2 )) != XB_NO_ERROR ){
          iErrorStop = 120;
          throw rc;
        }
        ulNextFreeBlock = ulNextFreeBlock2;
        if(( rc = WriteBlockHeader( ulPrevNode, 2 )) != XB_NO_ERROR ){
          iErrorStop = 130;
          throw rc;
        }
      }
    } else {                               // only take a portion of this set 
      if( ulPrevNode == 0 ){                // first in the set 
        ulHdrNextBlock = ulLocation + ulBlocksNeeded;
        if(( rc = UpdateHeadNextNode()) != XB_NO_ERROR ){
          iErrorStop = 140;
          throw rc;
        }
        ulFreeBlockCnt -= ulBlocksNeeded;
        if(( rc = WriteBlockHeader( ulHdrNextBlock, 2 )) != XB_NO_ERROR ){
          iErrorStop = 150;
          throw rc;
        }
      }
       else {                             // remove out of the middle or end
        ulNewFreeBlocks     = ulFreeBlockCnt - ulBlocksNeeded;
        ulSaveNextFreeBlock = ulNextFreeBlock; 
        ulNextFreeBlock2    = ulLocation + ulBlocksNeeded;

        if(( rc = ReadBlockHeader( ulPrevNode, 2 )) != XB_NO_ERROR ){
          iErrorStop = 160;
          throw rc;
        }
        ulNextFreeBlock = ulNextFreeBlock2;
        if(( rc = WriteBlockHeader( ulPrevNode, 2 )) != XB_NO_ERROR ){
          iErrorStop = 170;
          throw rc;
        }
        ulFreeBlockCnt  = ulNewFreeBlocks;
        ulNextFreeBlock = ulSaveNextFreeBlock;
        if(( rc = WriteBlockHeader( ulNextFreeBlock2, 2 )) != XB_NO_ERROR ){
          iErrorStop = 180;
          throw rc;
        }
      }
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::GetBlockSetFromChain() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Get a memo field for a given field number.
/*!
  \param iFieldNo Field number to retrieve data for.
  \param sMemoData Output - string containing memo field data.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::GetMemoField( xbInt16 iFieldNo, xbString & sMemoData ){

  xbUInt32 ulBlockNo;
  xbUInt32 ulMemoFieldLen;
  xbUInt32 ulMemoFieldDataLen;
  xbInt16  rc = XB_NO_ERROR;
  xbInt16  iErrorStop = 0;
  char     *p = NULL;

  try{
    if(( rc = GetMemoFieldLen( iFieldNo, ulMemoFieldLen, ulBlockNo )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }

    if( ulBlockNo == 0L || ulMemoFieldLen == 0L )
      sMemoData = "";
    else{
      ulMemoFieldDataLen = ulMemoFieldLen - 8;

      if(( p = (char *)calloc(1, ulMemoFieldDataLen+1)) == NULL ){
        iErrorStop = 110;
        rc = XB_NO_MEMORY;
        xbString sMsg;
        sMsg.Sprintf( "xbMemoDbt4::GetMemoField() lBlockNo = %ld Data Len = [%ld]", ulBlockNo, ulMemoFieldDataLen + 1 );
        xbase->WriteLogMessage( sMsg.Str() );
        throw rc;
      }

      // go to the first block of the memo field, skip past the first 8 bytes
      if(( xbFseek( ( ulBlockNo * GetBlockSize() + 8 ), SEEK_SET )) != XB_NO_ERROR ){
        iErrorStop = 120;
        rc = XB_SEEK_ERROR;
        throw rc;
      }

      // read the memo file data into buffer pointed to by "p"
      if(( rc = xbFread( p, ulMemoFieldDataLen, 1 )) != XB_NO_ERROR ){
        iErrorStop = 130;
        rc = XB_READ_ERROR;
        throw rc;
      }
      // null terminate the string
      char *p2;
      p2 = p + ulMemoFieldDataLen;
      *p2 = 0x00;

      // save it to the string
      sMemoData.Set( p, ulMemoFieldDataLen + 1 );
      free( p );
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::GetMemoField() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
    if( p )
      free( p );
  }
  return rc;
}

/***********************************************************************/
//! @brief Get a memo field length for a given field number.
/*!
  \param iFieldNo Field number to retrieve data for.
  \param ulMemoFieldLen Output - length of memo field data.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemoDbt4::GetMemoFieldLen( xbInt16 iFieldNo, xbUInt32 &ulMemoFieldLen ){
  xbUInt32 ulBlockNo;
  return GetMemoFieldLen( iFieldNo, ulMemoFieldLen, ulBlockNo );
}

/***********************************************************************/
//! @brief Get a memo field length for a given field number.
/*!
  \param iFieldNo Field number to retrieve data for.
  \param ulMemoFieldLen Output - length of memo field data.
  \param ulBlockNo Output - Starting block number.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::GetMemoFieldLen( xbInt16 iFieldNo, xbUInt32 &ulMemoFieldLen, xbUInt32 &ulBlockNo ){

  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  char    cFieldType;

  try{

    if(( rc = dbf->GetFieldType( iFieldNo, cFieldType )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }
    if( cFieldType != 'M' ){
      iErrorStop = 110;
      rc = XB_INVALID_MEMO_FIELD;
      throw rc;
    }
    if(( rc = dbf->GetULongField( iFieldNo, ulBlockNo )) < XB_NO_ERROR ){
      iErrorStop = 120;
      throw rc;
    }
    if( ulBlockNo < 1 ){
      ulMemoFieldLen = 0;
      return XB_NO_ERROR;
    }
    if(( rc = ReadBlockHeader( ulBlockNo, 1 )) != XB_NO_ERROR ){
      iErrorStop = 130;
      throw rc;
    }
    ulMemoFieldLen = ulFieldLen;
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::GetMemoFieldLen() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Open memo file.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::OpenMemoFile() {

  xbInt16 iErrorStop = 0;
  xbInt16 rc = XB_NO_ERROR;

  try{
    if(( rc = xbFopen( dbf->GetOpenMode(), dbf->GetShareMode())) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }
    if(( rc = ReadDbtHeader( 1 )) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw rc;
    }
    if(( mbb = (void *) malloc( GetBlockSize())) == NULL ){
      xbFclose();
      iErrorStop = 120;
      rc = XB_NO_MEMORY;
      throw rc;
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::OpenMemoFile() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Pack memo file.
/*!
  This routine frees up any unused blocks in the file resulting from field updates.
  Version 3 memo files do not reclaim unused space (Version 4 files do).
  This routine cleans up the unused space.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::PackMemo( void (*memoStatusFunc ) ( xbUInt32 ulItemNum, xbUInt32 ulNumItems )){
  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  char *  cBlock = NULL;

  #ifdef XB_LOCKING_SUPPORT
  xbBool  bTableLocked = xbFalse;
  xbBool  bMemoLocked = xbFalse;
  #endif

  try{
    #ifdef XB_LOCKING_SUPPORT
    if( dbf->GetAutoLock() && !dbf->GetTableLocked() ){
      if(( iRc = dbf->LockTable( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw iRc;
      } else {
        bTableLocked = xbTrue;
      }
      if(( iRc = LockMemo( XB_LOCK )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw iRc;
      } else {
        bMemoLocked = xbTrue;
      }
    }
    #endif

    // create temp file
    xbString sTempMemoName;
    if(( iRc = CreateUniqueFileName( GetDirectory(), "dbt", sTempMemoName )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }

    xbMemoDbt4 *pMemo = new xbMemoDbt4( dbf, sTempMemoName );
    if(( iRc = pMemo->CreateMemoFile()) != XB_NO_ERROR ){
      iErrorStop = 130;
      throw iRc;
    }
    // for dbase III, block size is always 512, don't need to reset it
    // for each record in dbf
    xbUInt32 ulRecCnt;
    if(( iRc = dbf->GetRecordCnt( ulRecCnt)) != XB_NO_ERROR ){
      iErrorStop = 140;
      throw iRc;
    }
    xbInt32  lFldCnt  = dbf->GetFieldCnt();
    char     cFldType;
    xbString sMemoFldData;

    for( xbUInt32 ulI = 1; ulI <= ulRecCnt; ulI++ ){
      if(( iRc = dbf->GetRecord( ulI  )) != XB_NO_ERROR ){
        iErrorStop = 150;
        throw iRc;
      }

      if( (void *) memoStatusFunc)
        (*memoStatusFunc)(ulI, ulRecCnt );

      // for each memo field 
      for( xbInt32 lFc = 0; lFc < lFldCnt; lFc++ ){
        if(( iRc = dbf->GetFieldType( lFc, cFldType )) != XB_NO_ERROR ){
          iErrorStop = 160;
          throw iRc;
        }
          if( cFldType == 'M' ){
          // copy it to work field
          if(( iRc = dbf->GetMemoField( lFc, sMemoFldData )) != XB_NO_ERROR ){
            iErrorStop = 170;
            throw iRc;
          }
          // write it to new field
          if(( iRc = pMemo->UpdateMemoField( lFc, sMemoFldData )) != XB_NO_ERROR ){
            iErrorStop = 180;
            throw iRc;
          }
        }
      }
    }

    //copy target back to source
    xbUInt32 ulBlkSize = GetBlockSize();
    xbUInt64 ullFileSize;
    if(( iRc = pMemo->GetFileSize( ullFileSize )) != XB_NO_ERROR ){
      iErrorStop = 190;
      throw iRc;
    }
    // file size should be evenly divisible by block size
    xbUInt32 ulBlkCnt;

    if( ullFileSize % ulBlkSize ){
      iErrorStop = 200;
      throw iRc;
    } else {
      ulBlkCnt = (xbUInt32) (ullFileSize / ulBlkSize);
    }
    if(( iRc = xbTruncate( 0 )) != XB_NO_ERROR ){
      iErrorStop = 210;
      throw iRc;
    }

    if(( cBlock = (char *) malloc( ulBlkSize )) == NULL ){
      iErrorStop = 220;
      throw iRc;
    }

    // can't rename files in a multiuser, cross platform environment, causes issues
    // copy work table back to source table

    for( xbUInt32 ulBc = 0; ulBc < ulBlkCnt; ulBc++ ){
      if(( iRc = pMemo->ReadBlock( ulBc, ulBlkSize, cBlock )) != XB_NO_ERROR ){
        iErrorStop = 230;
        throw iRc;
      }
      if(( iRc = WriteBlock( ulBc, ulBlkSize, cBlock )) != XB_NO_ERROR ){
        iErrorStop = 240;
        throw iRc;
      }
    }

    if(( xbFseek( 8, SEEK_SET )) != XB_NO_ERROR ){
        iErrorStop = 250;
        iRc = XB_SEEK_ERROR;
        throw iRc;
    }

    for( int i = 1; i < 9; i++ )
      xbFputc( sDbfFileNameWoExt[i] );

    //close and delete target
    if(( iRc = pMemo->xbFclose()) != XB_NO_ERROR ){
      iErrorStop = 260;
      throw iRc;
    }
    if(( iRc = pMemo->xbRemove()) != XB_NO_ERROR ){
      iErrorStop = 270;
      throw iRc;
    }
    free( cBlock );
    delete pMemo;

  }
  catch (xbInt16 iRc ){
    free( cBlock );

    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::PackMemo() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  #ifdef XB_LOCKING_SUPPORT
   if( bTableLocked )
     dbf->LockTable( XB_UNLOCK );
   if( bMemoLocked )
      LockMemo( XB_UNLOCK );
  #endif
  return iRc;
}
/***********************************************************************/
//! @brief Read block header.
/*!
  \param ulBlockNo Block to read
  \param iOption 1 - Read fields option 1
                 2 - Read fields option 2
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::ReadBlockHeader( xbUInt32 ulBlockNo, xbInt16 iOption ) {

  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{

    if(( rc = ReadBlock( ulBlockNo, 8, mbb )) != XB_NO_ERROR ){
      iErrorStop = 100;
      rc = XB_READ_ERROR;
    }
    if( iOption == 1 ){
      iField1    = eGetInt16((char *) mbb );
      iStartPos  = eGetInt16((char *) mbb+2);
      ulFieldLen = eGetUInt32((char *) mbb+4);
    }
    else if( iOption == 2 ){
      ulNextFreeBlock = eGetUInt32((char *) mbb );
      ulFreeBlockCnt  = eGetUInt32((char *) mbb+4 );
    }
    else{
      iErrorStop = 110;
      rc = XB_INVALID_OPTION;
      throw rc;
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::ReadBlockHeader() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Read dbt header file.
/*!
  \param iOption 0  -->  read only first four bytes<br>
                 1  -->  read the entire thing
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::ReadDbtHeader( xbInt16 iOption ) {

  xbInt16 iErrorStop = 0;
  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iReadLen = 0;
  char    *p;
  char    MemoBlock[22];

  try{
    if( !FileIsOpen() ){
      iErrorStop = 100;
      rc = XB_NOT_OPEN;
      throw rc;
    }
    if( xbFseek( 0, SEEK_SET )){
      iErrorStop = 110;
      rc = XB_SEEK_ERROR;
      throw rc;
    }
    if( iOption )
      iReadLen = 22;
    else
      iReadLen = 4;

    if(( xbFread( &MemoBlock, (size_t) iReadLen, 1 )) != XB_NO_ERROR ){
      iErrorStop = 120;
      rc = XB_READ_ERROR;
      throw rc;
    }

    p = MemoBlock;
    ulHdrNextBlock = eGetUInt32( p );
    if( iOption == 0)
      return XB_NO_ERROR;

    p += 8;
    sDbfFileNameWoExt = "";
    for( int i = 0; i < 8; i++ )
      sDbfFileNameWoExt += *p++;

    p += 4;
    SetBlockSize( (xbUInt32) eGetInt16( p ));

    cVersion = MemoBlock[16];

  }
  catch (xbInt16 rc ){

    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::ReadDbtHeader() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}

/***********************************************************************/
#ifdef XB_DEBUG_SUPPORT
//! @brief Read free block information from header.
/*!
  This routing pulls any reusable block information for file header.
  Not used with version 3 memo files - stub.

  \param ulBlockNo
  \param ulNextBlock
  \param ulFreeBlockCnt
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt4::ReadFreeBlockHeader( xbUInt32 ulBlockNo, xbUInt32 &ulNextBlock, xbUInt32 &ulFreeBlockCount ){

  xbInt16 rc = XB_NO_ERROR;
  rc = ReadBlockHeader( ulBlockNo, 2 );
  ulNextBlock = ulNextFreeBlock;
  ulFreeBlockCount = ulFreeBlockCnt;
  return rc;
}
#endif
/***********************************************************************/
//! @brief Update header name.
/*!
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt4::UpdateHeaderName() {

  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  GetFileNamePart( sDbfFileNameWoExt );
  sDbfFileNameWoExt.PadRight( ' ', 8 );                  // need at least eight bytes of name
  sDbfFileNameWoExt = sDbfFileNameWoExt.Mid( 1, 8 );     // need no more than eight bytes of name

  try{
    if(( rc = xbFseek( 8, SEEK_SET )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }

    for( int i = 1; i < 9; i++ ){
      if(( rc = xbFputc( sDbfFileNameWoExt[i] )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw rc;
      }
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::UpdateHeaderName() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Update a memo field length for a given field number.
/*!
  \param iFieldNo Field number to update data for.
  \param sMemoData Data to update memo field data with.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::UpdateMemoField( xbInt16 iFieldNo, const xbString & sMemoData ) {

  xbInt16  iErrorStop = 0;
  xbInt16  rc = XB_NO_ERROR;
  xbUInt32 ulBlockNo;

  try{

    if(( rc = dbf->GetULongField( iFieldNo, ulBlockNo )) < XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }

    if( sMemoData == "" ){
      if( ulBlockNo == 0 ){
        /* if nothing to do, return */
        return XB_NO_ERROR;
      } else {

        // if this is in the new blocks link list already, then this is not the first update for this memo field
        // this would be second or third update on the field since the original change and not commited
        // Since it won't be needed in either a Commmit() or Abort(), can be freed immediately
        if( llNewBlocks.SearchFor( ulBlockNo ) > 0 ){
          if(( rc = FreeMemoBlockChain( ulBlockNo )) != XB_NO_ERROR ){
            iErrorStop = 110;
            throw rc;
          }
          if(( llNewBlocks.RemoveByVal( ulBlockNo )) < XB_NO_ERROR ){
            iErrorStop = 120;
            throw rc;
          }
        } else {
          // first revision, save what it was in case of Abort() command
          if(( llOldBlocks.InsertAtFront( ulBlockNo )) != XB_NO_ERROR ){
            iErrorStop = 130;
            throw rc;
          }
        }
        if(( rc = dbf->PutField( iFieldNo, "" )) != XB_NO_ERROR ){
          iErrorStop = 140;
          throw rc;
        }
      }
    } else {
      // free up the old space
      xbUInt32 ulLastDataBlock = 0L;

      if( ulBlockNo > 0 ){
        if( llNewBlocks.SearchFor( ulBlockNo ) > 0 ){

          if(( rc = FreeMemoBlockChain( ulBlockNo, ulLastDataBlock )) != XB_NO_ERROR ){
            iErrorStop = 150;
            throw rc;
          }
        } else {
          // first revision, save what it was in case of Abort() command
          if(( rc = llOldBlocks.InsertAtFront( ulBlockNo )) != XB_NO_ERROR ){
            iErrorStop = 160;
            throw rc;
          }
        }
      }
      // should next line be unsigned 32 bit int?
      xbUInt32 ulTotalLen = 8 + sMemoData.Len();
      xbUInt32 ulBlocksNeeded;
      if( ulTotalLen % GetBlockSize())
        ulBlocksNeeded = ulTotalLen / GetBlockSize() + 1;
      else
        ulBlocksNeeded = ulTotalLen / GetBlockSize();

      xbBool bUsedBlockFound;
      xbUInt32 ulHeadBlock;
      xbUInt32 ulPrevNode;
      if(( rc = FindBlockSetInChain( ulBlocksNeeded, ulLastDataBlock, ulHeadBlock, ulPrevNode, bUsedBlockFound )) != XB_NO_ERROR ){
        iErrorStop = 170;
        throw rc;
      }
      iField1   = -1;
      iStartPos = 8;
      ulFieldLen = sMemoData.Len() + 8;

      if( bUsedBlockFound ){

        if(( rc = GetBlockSetFromChain( ulBlocksNeeded, ulHeadBlock, ulPrevNode )) != XB_NO_ERROR ){
          iErrorStop = 180;
          throw rc;
        }

        if(( rc = WriteBlockHeader( ulHeadBlock, 1 )) != XB_NO_ERROR ){
          iErrorStop = 190;
          throw rc;
        }

        if(( rc = xbFwrite( sMemoData.Str(), sMemoData.Len(), 1 )) != XB_NO_ERROR ){
          iErrorStop = 200;
          throw rc;
        }
      } else { // append to the end 

        if(( rc = WriteBlockHeader( ulLastDataBlock, 1 )) != XB_NO_ERROR ){
          iErrorStop = 220;
          throw rc;
        }

        if(( rc = xbFwrite( sMemoData.Str(), sMemoData.Len(), 1 )) != XB_NO_ERROR ){
          iErrorStop = 230;
          throw rc;
        }

        if(( rc = xbFputc( 0x00, (xbInt32)((ulBlocksNeeded * GetBlockSize()) - (sMemoData.Len() + 8)))) != XB_NO_ERROR ){
          iErrorStop = 240;
          throw rc;
        }

        if( ulLastDataBlock == ulHdrNextBlock ){                   // this is first node to be added to the node chain
          ulHdrNextBlock += ulBlocksNeeded;
          ulHeadBlock = ulLastDataBlock;
          if(( rc = UpdateHeadNextNode()) != XB_NO_ERROR ){
            iErrorStop = 250;
            throw rc;
          }

        } else {                                                // adding memo data to the end of the file, but chain exists

          ulNextFreeBlock = ulLastDataBlock + ulBlocksNeeded;
          ulHeadBlock = ulLastDataBlock;
          if(( rc =  WriteBlockHeader( ulPrevNode, 2 )) != XB_NO_ERROR ){
            iErrorStop = 260;
            throw rc;
          }
        }
      }

      if(( rc = llNewBlocks.InsertAtFront( ulHeadBlock )) != XB_NO_ERROR ){  // In case of Abort(), this block needs to be freed
        iErrorStop = 270;
        throw rc;
      }
      if(( rc = dbf->PutLongField( iFieldNo, (xbInt32) ulHeadBlock )) != XB_NO_ERROR ){
        iErrorStop = 280;
        throw rc;
      }
    }
  }

  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::UpdateMemoField() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Write block header.
/*!  
  \param ulBlockNo Block to read
  \param iOption 1 - Read fields option 1
                 2 - Read fields option 2
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt4::WriteBlockHeader( xbUInt32 ulBlockNo, xbInt16 iOption ) {

  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{
    if( iOption == 1 ){
      ePutInt16 ((char *) mbb,   iField1 );
      ePutInt16 ((char *) mbb+2, iStartPos );
      ePutUInt32((char *) mbb+4, ulFieldLen );
    }
    else if( iOption == 2 ){
      ePutUInt32((char *) mbb,   ulNextFreeBlock );
      ePutUInt32((char *) mbb+4, ulFreeBlockCnt );
    }
    else{
      iErrorStop = 100;
      rc = XB_INVALID_OPTION;
      throw rc;
    }

    if(( rc = WriteBlock( ulBlockNo, 8, mbb )) != XB_NO_ERROR ){
      iErrorStop = 110;
      rc = XB_READ_ERROR;
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::WriteHeader() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Empty the memo file.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemoDbt4::Zap(){

  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  char    cBuf[4];
  try{
    ulHdrNextBlock = 1L;
    ePutUInt32( cBuf, ulHdrNextBlock );

    if(( iRc != xbFseek( 0, SEEK_SET )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }
    if(( iRc != xbFwrite( cBuf, 4, 1 ))!= XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }
    if(( iRc != xbTruncate( GetBlockSize())) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }
  }

  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt4::Zap() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}/***********************************************************************/
}              /* namespace        */
#endif         /*  XB_DBF4_SUPPORT */
#endif         /*  XB_MEMO_SUPPORT  */