summaryrefslogtreecommitdiff
path: root/src/core/xbmemo3.cpp
blob: 60c1d5381637cd37e28e75b786b1baffb96fbe7a (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
/* xbmemo3.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 V3 memo files

*/

#include "xbase.h"

#ifdef XB_MEMO_SUPPORT
#ifdef XB_DBF3_SUPPORT

namespace xb{

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

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

/***********************************************************************/
//! @brief Abort.
/*!
  Abort any pending updates to the memo file.
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt3::Abort(){
  return XB_NO_ERROR;
}/***********************************************************************/
//! @brief Commit changes to memo file.
/*!
  \returns XB_NO_ERROR.
*/
xbInt16 xbMemoDbt3::Commit(){
  return XB_NO_ERROR;
}
/***********************************************************************/
//! @brief Create memo file.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbMemoDbt3::CreateMemoFile(){

  xbInt16 rc = XB_NO_ERROR;
  char cBuf[4];
  if(( rc = xbFopen( "w+b", dbf->GetShareMode())) != XB_NO_ERROR )
    return rc;
  ulHdrNextBlock = 1L;
  ePutUInt32( cBuf, ulHdrNextBlock );
  if(( rc = xbFwrite( cBuf, 4, 1 ))!= XB_NO_ERROR ){
    xbFclose();
    return rc;
  }
  for(int i = 0; i < 12; i++ )
    xbFputc( 0x00 );
  xbFputc( 0x03 );
  for(int i = 0; i < 495; i++ )
    xbFputc( 0x00 );
  if(( mbb = (void *) malloc( 512 )) == NULL ){
    xbFclose();
    return XB_NO_MEMORY;
  }
  return XB_NO_ERROR;
}
/***********************************************************************/
#ifdef XB_DEBUG_SUPPORT
//! @brief Dump memo file header.
/*!
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt3::DumpMemoFreeChain() {
  std::cout << "Xbase version 3 file - no free block chain" << std::endl;
  return XB_NO_ERROR;
}
#endif   // XB_DEBUG_SUPPORT

//! @brief Dump memo file header.
/*!
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt3::DumpMemoHeader(){
  xbInt16  rc = XB_NO_ERROR;
  xbUInt64 stFileSize;
  if(( rc = ReadDbtHeader( 1 )) != XB_NO_ERROR )
    return rc;
  GetFileSize( stFileSize );
  std::cout << "Version 3 Memo Header Info" << std::endl;
  std::cout << "Memo File Name       = " << GetFqFileName()    << std::endl;
  std::cout << "Next Available Block = " << ulHdrNextBlock     << std::endl;
  std::cout << "Memo File Version    = " << (xbInt16) cVersion << " (";
  BitDump( cVersion );
  std::cout << ")" << std::endl;
  std::cout << "Block Size           = " << GetBlockSize()     << std::endl;
  std::cout << "File Size            = " << stFileSize         << std::endl;
  std::cout << "Block Count          = " << stFileSize / GetBlockSize() << std::endl;
  return XB_NO_ERROR;
}
/***********************************************************************/
//! @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 xbMemoDbt3::GetMemoField( xbInt16 iFieldNo, xbString & sMemoData ){

  xbInt16 iErrorStop = 0;
  xbInt16 rc = XB_NO_ERROR;
  xbUInt32 ulScnt;
  char *sp, *spp;
  xbUInt32  ulBlockNo;
  xbBool  bDone = xbFalse;
  sMemoData = "";
  try{
    if(( rc = dbf->GetULongField( iFieldNo, ulBlockNo )) < XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }
    if( ulBlockNo == 0L ){
      sMemoData = "";
      return XB_NO_ERROR;
    }
    spp = NULL;
    while( !bDone ){
      if(( rc = ReadBlock( ulBlockNo++, GetBlockSize(), mbb )) != XB_NO_ERROR ){
        iErrorStop = 120;
        throw rc;
      }
      ulScnt = 0;
      sp = (char *) mbb;
      while( ulScnt < 512 && !bDone ){
        if( *sp == 0x1a && *spp == 0x1a )
          bDone = xbTrue;
        else{
          ulScnt++; spp = sp; sp++;
        }
      }
      sMemoData.Append( (char *) mbb, ulScnt );
    }
    sMemoData.ZapTrailingChar( 0x1a );
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt3::GetMemoField() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Get a memo field length for a given field number.
/*!
  \param iFieldNo Field number to retrieve data for.
  \param ulFieldLen Output - length of memo field data.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemoDbt3::GetMemoFieldLen( xbInt16 iFieldNo, xbUInt32 & ulFieldLen ){

  xbInt16   iErrorStop = 0;
  xbInt16   rc = XB_NO_ERROR;
  xbInt16   iScnt;
  char *sp, *spp;
  xbUInt32  ulBlockNo;
  xbInt16   iNotDone;
  try{
    if(( rc = dbf->GetULongField( iFieldNo, ulBlockNo )) < XB_NO_ERROR ){
      iErrorStop = 100;
      throw rc;
    }
    if( ulBlockNo == 0 ){
      ulFieldLen = 0;
      return XB_NO_ERROR;
    }
    ulFieldLen = 0L;
    spp = NULL;
    iNotDone = 1;
    while( iNotDone ){
      if(( rc = ReadBlock( ulBlockNo++, GetBlockSize(), mbb )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw rc;
      }
      iScnt = 0;
      sp = (char *) mbb;
      while( iScnt < 512 && iNotDone ){
        if( *sp == 0x1a && *spp == 0x1a )
          iNotDone = 0;
        else{
          ulFieldLen++; iScnt++; spp = sp; sp++;
        }
      }
    }
    if( ulFieldLen > 0 ) ulFieldLen--;
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt3::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 xbMemoDbt3::OpenMemoFile() {
  xbInt16 rc = XB_NO_ERROR;
  if(( rc = xbFopen( dbf->GetOpenMode(), dbf->GetShareMode())) != XB_NO_ERROR )
    return rc;
  if(( mbb = (void *) malloc( 512 )) == NULL ){
    xbFclose();
    return XB_NO_MEMORY;
  }
  return XB_NO_ERROR;
}
/***********************************************************************/
//! @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 xbMemoDbt3::PackMemo( void (*memoStatusFunc ) ( xbUInt32 ulItemNum, xbUInt32 ulNumItems ))
{
  xbInt16 iRc = XB_NO_ERROR;
  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;
    }

    xbMemoDbt3 *pMemo = new xbMemoDbt3( 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( (size_t) 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;
      }
    }
    //close and delete target
    if(( iRc = pMemo->xbFclose()) != XB_NO_ERROR ){
      iErrorStop = 250;
      throw iRc;
    }
    if(( iRc = pMemo->xbRemove()) != XB_NO_ERROR ){
      iErrorStop = 260;
      throw iRc;
    }
    free( cBlock );
    delete pMemo;
  }
  catch (xbInt16 iRc ){
    free( cBlock );
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt3::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 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 xbMemoDbt3::ReadDbtHeader( xbInt16 iOption ){
  char *p;
  char MemoBlock[20];
  xbInt16 rc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  xbUInt32 ulReadSize;

  try{
    if( !FileIsOpen() ){
      iErrorStop = 100;
      rc = XB_NOT_OPEN;
      throw rc;
    }
    if( iOption == 0 )
      ulReadSize = 4;
    else{
      xbUInt64 stFileSize = 0;
      if(( rc = GetFileSize( stFileSize )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw rc;
      }
      if( stFileSize < 4 ){
        iErrorStop = 120;
        rc = XB_INVALID_BLOCK_NO;
        throw rc;
      }
      else if( stFileSize > 20 )
        ulReadSize = 130;
      else
        ulReadSize = 4;
    }
    if( xbFseek( 0, SEEK_SET )){
      iErrorStop = 140;
      rc = XB_SEEK_ERROR;
      throw rc;
    }
    if(( xbFread( &MemoBlock, ulReadSize, 1 )) != XB_NO_ERROR ){
      iErrorStop = 150;
      rc = XB_READ_ERROR;
      throw rc;
    }
    p = MemoBlock;
    ulHdrNextBlock = eGetUInt32( p );

    if( iOption == 0)
      return XB_NO_ERROR;

    if( ulReadSize >= 20 ){
      p+=16;
      cVersion = *p;
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt3::ReadDbtHeader() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}
/***********************************************************************/
//! @brief Update header name.
/*!
  \returns XB_NO_ERROR
*/
xbInt16 xbMemoDbt3::UpdateHeaderName(){
  return XB_NO_ERROR;
}
/***********************************************************************/
//! @brief Update a memo field 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 xbMemoDbt3::UpdateMemoField( xbInt16 iFieldNo, const xbString & sMemoData ) {

  xbInt16 iErrorStop = 0;
  xbInt16 rc = XB_NO_ERROR;
  try{
    if( sMemoData == "" ){
      if(( rc = dbf->PutField( iFieldNo, "" )) != XB_NO_ERROR ){
        iErrorStop = 100;
        throw rc;
      }
    } else {
      xbUInt32 ulDataLen      = sMemoData.Len() + 2;
      xbUInt32 ulBlocksNeeded = (ulDataLen / 512) + 1;
      xbUInt32 ulLastDataBlock;
      if(( rc = CalcLastDataBlock( ulLastDataBlock )) != XB_NO_ERROR ){
        iErrorStop = 110;
        throw rc;
      }
      if(( rc = xbFseek( ((xbInt64) ulLastDataBlock * 512), SEEK_SET )) != XB_NO_ERROR ){
        iErrorStop = 120;
        throw rc;
      }
      if(( rc = xbFwrite( sMemoData.Str(), sMemoData.Len(), 1 )) != XB_NO_ERROR ){
        iErrorStop = 130;
        throw rc;
      }
      if(( rc = xbFputc( 0x1a, 2 )) != XB_NO_ERROR ){
        iErrorStop = 140;
        throw rc;
      }
      if(( rc = xbFputc( 0x00, (xbInt32) ( ulBlocksNeeded * 512 ) - (xbInt32) ulDataLen )) != XB_NO_ERROR ){
        iErrorStop = 150;
        throw rc;
      }
      if(( rc = dbf->PutULongField( iFieldNo, ulLastDataBlock )) != XB_NO_ERROR ){
        iErrorStop = 160;
        throw rc;
      }
      ulHdrNextBlock = ulLastDataBlock + ulBlocksNeeded;
      if(( rc = UpdateHeadNextNode()) != XB_NO_ERROR ){
        iErrorStop = 170;
        throw rc;
      }
    }
  }
  catch (xbInt16 rc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt3::UpdateMemoField() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, rc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( rc ));
  }
  return rc;
}

/***********************************************************************/
//! @brief Empty the memo file.
/*!
  This routine clears everything out of the file. It does not address the
  block pointers on the dbf file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemoDbt3::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( 512 )) != XB_NO_ERROR ){
      iErrorStop = 120;
      throw iRc;
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbMemoDbt3::Zap() Exception Caught. Error Stop = [%d] rc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/***********************************************************************/
}              /* namespace        */
#endif         /*  XB_DBF3_SUPPORT */
#endif         /*  XB_MEMO_SUPPORT  */