summaryrefslogtreecommitdiff
path: root/src/core/xbtblmgr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/xbtblmgr.cpp')
-rwxr-xr-xsrc/core/xbtblmgr.cpp257
1 files changed, 257 insertions, 0 deletions
diff --git a/src/core/xbtblmgr.cpp b/src/core/xbtblmgr.cpp
new file mode 100755
index 0000000..e1bf496
--- /dev/null
+++ b/src/core/xbtblmgr.cpp
@@ -0,0 +1,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 */