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

//#ifdef HAVE_STRING_H
//#include <string.h>
//#endif

namespace xb{

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

xbTblMgr::xbTblMgr(){
  TblList = NULL;
  iOpenTableCount = 0;
}

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

xbTblMgr::~xbTblMgr(){
  xbTblList *l;
  if( TblList ){
    while( TblList ){
      l = TblList;
      TblList = TblList->pNext;
      delete l->psTblName;
      delete l->psTblAlias;
      free( l );
    }
  }
}

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

xbInt16 xbTblMgr::AddTblToTblList( xbDbf *d, const xbString & sTblName ){
  return AddTblToTblList( d, sTblName, "" );
}

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

xbInt16 xbTblMgr::AddTblToTblList( xbDbf *d, const xbString & sTblName, const xbString & sTblAlias ) {

  xbTblList *i, *s, *t;
  xbInt16 iRc = 0;
  xbInt16 iErrorStop = 0;
  xbString sAlias;

  try{

    if( sTblName.Len() == 0 ){
      iErrorStop = 10;
      iRc = XB_INVALID_TABLE_NAME;
      throw iRc;
    }

    if( sTblAlias.Len() == 0 ){
      sAlias = sTblName;
      sAlias.SwapChars( '\\', '/' );
      xbUInt32 iPos = sAlias.GetLastPos( '/' );
      if( iPos > 0 )            /* get rid of the directory part of the name */
        sAlias.Ltrunc( iPos );
    } else {
      sAlias = sTblAlias;
    }

    if((i = (xbTblList *) calloc(1, sizeof(xbTblList))) == NULL){
      iErrorStop = 20;
      iRc = XB_NO_MEMORY;
      throw iRc;
    }
    i->psTblName  = new xbString( sTblName );
    i->psTblAlias = new xbString( sAlias );
    i->pDbf       = d;
    i->pNext      = NULL;

    // insert new table into the list of open tables, sorted by table name 
    s = NULL;
    t = TblList;

    while(t && (strcmp( t->psTblAlias->Str(), sAlias.Str()) < 0 )){
      s = t;
      t = t->pNext;
    }

    if( t && (strcmp( t->psTblAlias->Str(), sAlias.Str()) == 0 )){
      iErrorStop = 30;
      delete i->psTblName;
      delete i->psTblAlias;
      free( i );
      iRc = XB_DUP_TABLE_OR_ALIAS;
      throw iRc;
    }
    i->pNext = t;
    if (s == NULL)
      TblList = i;
    else
      s->pNext = i;
  }
  catch (xbInt16 iRc ){
    if( iErrorStop != 30 ){
      xbString sMsg;
      sMsg.Sprintf( "xbTblMgr::AddTblToTblList() Exception Caught. Error Stop = [%d] iRc = [%d] Tbl Name = [%s] Alias = [%s]", iErrorStop, iRc, sTblName.Str(), sTblAlias.Str() );
      std::cout << sMsg << std::endl;
    }
  }
  if( iRc == XB_NO_ERROR )
    iOpenTableCount++;
  return iRc;
}

/*************************************************************************/
xbInt16 xbTblMgr::DisplayTableList() const {
  xbInt16 iTblCnt = 0;
  xbTblList * l = TblList;
  std::cout << "-- Open Table List --" << std::endl;
  if( l == NULL )
    std::cout << "Table list is empty" << std::endl;
  else{
    while( l ){
      iTblCnt++;
      std::cout << iTblCnt << " Table=[" << l->psTblName->Str() << "]  Alias=[" << l->psTblAlias->Str() << "]" << std::endl;
      l = l->pNext;
    }
  }
  return iTblCnt;
}

/*************************************************************************/
/* Get pointer to named dbf.
  Looks up an open DBF file by Name.

  returns A pointer to the xbDbf class instance if found or NULL if not found.

  // looks for a match as an alias first, if not found as an alias, looks at the name

*/

xbDbf *xbTblMgr::GetDbfPtr(const xbString& sTblAlias) const {

  xbTblList *t;
  t = TblList;
  while( t ){
    if( sTblAlias == t->psTblAlias->Str())
      return t->pDbf;
    t = t->pNext;
  }

  t = TblList;
  while( t ){
    if( sTblAlias == t->psTblName->Str())
      return t->pDbf;
    t = t->pNext;
  }
  return NULL;
}

/*************************************************************************/
/* Get pointer to named dbf.
  Looks up an open DBF file by Name.

  returns pointer to the xbDbf class instance if found or NULL if not found.
*/

xbDbf *xbTblMgr::GetDbfPtr(xbInt16 iItemNo ) const {

  xbTblList *t;
  t = TblList;
  xbInt16 iCnt = 1;

  if( iItemNo < 1 || iItemNo > iOpenTableCount )
    return NULL;

  while( t && iCnt < iItemNo ){
    t = t->pNext;
    iCnt++;
  }
  if( t )
    return t->pDbf;
  else
    return NULL;
}

/*************************************************************************/
xbInt16 xbTblMgr::GetOpenTableCount() const {
  return iOpenTableCount;
}

/*************************************************************************/
xbInt16 xbTblMgr::RemoveTblFromTblList( const xbString & sTblAlias ) {
  xbTblList *i, *s;

  i = TblList;
  s = NULL;

  while( i ){

    if( strcmp( i->psTblAlias->Str(), sTblAlias.Str()) == 0 ) {
      if(s)
        s->pNext = i->pNext;
      else
        TblList = i->pNext;

      delete i->psTblName;
      delete i->psTblAlias;
      free( i );
      iOpenTableCount--;
      return XB_NO_ERROR;
    } else {
      s = i;
      i = i->pNext;
    }
  }
  return XB_NOT_FOUND;
}
/*************************************************************************/
xbInt16 xbTblMgr::RemoveTblFromTblList( xbDbf *pTbl ) {
  xbTblList *i, *s;

  i = TblList;
  s = NULL;

  while( i ){

    if( i->pDbf == pTbl ) {
      if(s)
        s->pNext = i->pNext;
      else
        TblList = i->pNext;

      delete i->psTblName;
      delete i->psTblAlias;
      free( i );
      iOpenTableCount--;
      return XB_NO_ERROR;
    } else {
      s = i;
      i = i->pNext;
    }
  }
  return XB_NOT_FOUND;
}

/*************************************************************************/
}   /* namespace */