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

XBase64 Software Library

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

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

Email Contact:

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

   Base memo class
*/

#include "xbase.h"

#ifdef XB_MEMO_SUPPORT

namespace xb{

/***********************************************************************/
//! @brief Class Constructor.
/*!
  \param dbf Pointer to dbf construct.
  \param sFileName Memo file name.
*/

xbMemo::xbMemo( xbDbf * dbf, xbString const &sFileName ) : xbFile( dbf->GetXbasePtr() ) {
  this->dbf = dbf;              /* pointer to the associated dbf class instance */
  // xbase = dbf->GetXbasePtr();   /* pointer to the engine */
  SetDirectory( dbf->GetDirectory());
  SetFileName( sFileName );
  mbb = NULL;
  #ifdef XB_LOCKING_SUPPORT
  bFileLocked = xbFalse;
  #endif
}
/***********************************************************************/
//! @brief Class Destructor.
xbMemo::~xbMemo(){
 if( mbb )
    free( mbb );
}
/***********************************************************************/
//! @brief Calculate the last data block number.
/*!
  \param ulLastDataBlock Output - Last used block number in the file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemo::CalcLastDataBlock( xbUInt32 & ulLastDataBlock ){

  xbInt16 iRc = XB_NO_ERROR;
  if(( iRc = xbFseek( 0, SEEK_END )) != XB_NO_ERROR )
    return iRc;

  ulLastDataBlock = (xbUInt32) xbFtell() / (xbUInt32) GetBlockSize();
  return XB_NO_ERROR;
}
/***********************************************************************/
//! @brief Close the memo file.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemo::CloseMemoFile(){

  if( mbb ){
    free( mbb );
    mbb = NULL;
  }
  return xbFclose();
}


/***********************************************************************/
//! @brief Get memo file type.
/*!
  \returns 3 - Version 3 memo file.<br>
           4 - Version 4 memo file.
*/
xbInt16 xbMemo::GetMemoFileType(){
  return iMemoFileType;
}

/***********************************************************************/
//! @brief Get next block available from file header.
/*!
  \param ulBlockNo Output - Next block number for appending data to memo file.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemo::GetHdrNextBlock( xbUInt32 & ulBlockNo ){
  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  try{
    if(( iRc = ReadDbtHeader( 0 )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }
    ulBlockNo = ulHdrNextBlock;
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbmemo::GetNextAvailableBlock() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
/***********************************************************************/
#ifdef XB_LOCKING_SUPPORT
//! @brief Lock memo file
/*!

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

xbInt16 xbMemo::LockMemo( xbInt16 iLockFunction ){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;

  try{
    if( iLockFunction == XB_LOCK ){

      if( bFileLocked )       // already locked
        return XB_NO_ERROR;

      if( dbf->GetLockFlavor() == LK_DBASE ){
        iRc = xbLock( XB_LOCK, LK4026531838, 1 );
        if( iRc != XB_NO_ERROR ){
          if( iRc == XB_LOCK_FAILED )
            return iRc;
          else {
            iErrorStop = 100;
            throw iRc;
          }
        } else {
          bFileLocked = xbTrue;
        }
      }
    } else if( iLockFunction == XB_UNLOCK ){

      if( !bFileLocked )  // already unlocked
        return XB_NO_ERROR;

      if( dbf->GetLockFlavor() == LK_DBASE ){
        iRc = xbLock( XB_UNLOCK, LK4026531838, 1 );
        if( iRc != XB_NO_ERROR ){
          if( iRc == XB_LOCK_FAILED )
            return iRc;
          else {
            iErrorStop = 110;
            throw iRc;
          }
        } else {
          bFileLocked = xbFalse;
        }
      }
    } else {
      iErrorStop = 120;
      iRc = XB_INVALID_OPTION;
      throw iRc;
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbmemo::LockMemoFile() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}

/***********************************************************************/
//! @brief Get memo file lock status.
/*!
  \returns xbTrue - Memo file is locked.<br>
           xbFalse - Memo file is not locked.
*/
xbBool xbMemo::GetMemoLocked() const {
  return bFileLocked;
}
#endif

/***********************************************************************/
//! @brief Update Next Node number in file header
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbMemo::UpdateHeadNextNode(){

  xbInt16 iRc = XB_NO_ERROR;
  xbInt16 iErrorStop = 0;
  try{
    char buf[4];
    ePutUInt32( buf, ulHdrNextBlock );
    if(( iRc = xbFseek( 0, SEEK_SET )) != XB_NO_ERROR ){
      iErrorStop = 100;
      throw iRc;
    }
    if(( iRc = xbFwrite( &buf, 4, 1 )) != XB_NO_ERROR ){
      iErrorStop = 110;
      throw iRc;
    }
  }
  catch (xbInt16 iRc ){
    xbString sMsg;
    sMsg.Sprintf( "xbmemo::UpdateHeadeNextNode() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc );
    xbase->WriteLogMessage( sMsg.Str() );
    xbase->WriteLogMessage( GetErrorMessage( iRc ));
  }
  return iRc;
}
/***********************************************************************/
}              /* namespace       */
#endif         /*  XB_MEMO_SUPPORT */