From 517ad9d4b6eae320b708d03a9340a22893b0cab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 29 Jan 2023 15:45:51 +0100 Subject: New upstream version 4.0.3 --- src/core/xbdbf3.cpp | 272 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 231 insertions(+), 41 deletions(-) (limited to 'src/core/xbdbf3.cpp') diff --git a/src/core/xbdbf3.cpp b/src/core/xbdbf3.cpp index cbbff1f..504542f 100755 --- a/src/core/xbdbf3.cpp +++ b/src/core/xbdbf3.cpp @@ -47,7 +47,7 @@ xbDbf3::~xbDbf3() {}; \param iOverlay xbTrue - Overlay.
xbFalse - Don't overlay. \param iShareMode XB_SINGLE_USER
XB_MULTI_USER \returns Return Codes - + */ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlias, xbSchema *pSchema, xbInt16 iOverlay, xbInt16 iShareMode ){ @@ -71,33 +71,46 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia /* check if the file already exists */ if( FileExists( 0 )){ if( !iOverlay ){ - iErrorStop = 10; + iErrorStop = 100; rc = XB_FILE_EXISTS; throw rc; } - #ifdef XB_NDXINF_SUPPORT - // remove the metadata file if there is one + + // remove other files if they exist xbString sMname = sNfn; xbUInt32 iMnameLen = sMname.Len(); - sMname.PutAt( iMnameLen-2, 'X' ); - sMname.PutAt( iMnameLen-1, 'B' ); - sMname.PutAt( iMnameLen, 'M' ); + sMname.PutAt( iMnameLen-2, 'I' ); + sMname.PutAt( iMnameLen-1, 'N' ); + sMname.PutAt( iMnameLen, 'F' ); xbFile fTemp( xbase ); fTemp.SetFileName( sMname ); if( fTemp.FileExists() ) fTemp.xbRemove(); - #endif // XB_NDXINF_SUPPORT + + sMname.PutAt( iMnameLen-2, 'D' ); + sMname.PutAt( iMnameLen-1, 'B' ); + sMname.PutAt( iMnameLen, 'T' ); + fTemp.SetFileName( sMname ); + if( fTemp.FileExists() ) + fTemp.xbRemove(); + + sMname.PutAt( iMnameLen-2, 'M' ); + sMname.PutAt( iMnameLen-1, 'D' ); + sMname.PutAt( iMnameLen, 'X' ); + fTemp.SetFileName( sMname ); + if( fTemp.FileExists() ) + fTemp.xbRemove(); } /* check if we already have a file with this alias */ if(( rc = xbase->AddTblToTblList( this, GetFqFileName(), sAlias )) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw rc; } rc = ValidateSchema( pSchema ); if( rc < 0 ){ - iErrorStop = 30; + iErrorStop = 120; throw rc; } else iNoOfFields = rc; @@ -121,7 +134,7 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia xbFile fTemp( xbase ); fTemp.SetFileName( sMfn ); if( fTemp.FileExists() && !iOverlay ){ - iErrorStop = 40; + iErrorStop = 130; rc = XB_FILE_EXISTS; throw rc; } @@ -129,7 +142,7 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia Memo = new xbMemoDbt3( this, fTemp.GetFqFileName()); if(( rc = Memo->CreateMemoFile()) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw rc; } } @@ -145,18 +158,18 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia #endif if(( rc = xbFopen( "w+b", iShareMode )) != XB_NO_ERROR ){ - iErrorStop = 60; + iErrorStop = 150; throw rc; } uiRecordLen++; /* add one byte for 0x0D */ if(( RecBuf = (char *) malloc( uiRecordLen )) == NULL ){ - iErrorStop = 70; + iErrorStop = 160; throw rc; } if(( RecBuf2 = (char *) malloc( uiRecordLen )) == NULL ){ - iErrorStop = 80; + iErrorStop = 170; rc = XB_NO_MEMORY; throw rc; } @@ -173,12 +186,12 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia /* write the header prolog */ if(( rc = WriteHeader( 0, 0 )) != XB_NO_ERROR ){ - iErrorStop = 90; + iErrorStop = 180; rc = XB_WRITE_ERROR; throw rc; } if((SchemaPtr = (xbSchemaRec *) malloc( (size_t) iNoOfFields * sizeof( xbSchemaRec ))) == NULL){ - iErrorStop = 100; + iErrorStop = 190; rc = XB_NO_MEMORY; throw rc; } @@ -195,7 +208,7 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia SchemaPtr[i].cNoOfDecs = (unsigned char) pSchema[i].iNoOfDecs; if( SchemaPtr[i].cNoOfDecs > SchemaPtr[i].cFieldLen ){ - iErrorStop = 110; + iErrorStop = 200; rc = XB_WRITE_ERROR; throw rc; } @@ -204,13 +217,13 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia k += SchemaPtr[i].cFieldLen; if(( xbFwrite( &SchemaPtr[i].cFieldName, 1, 11 )) != XB_NO_ERROR ) { - iErrorStop = 110; + iErrorStop = 210; rc = XB_WRITE_ERROR; throw rc; } if(( xbFwrite( &SchemaPtr[i].cType, 1, 1 )) != XB_NO_ERROR ) { - iErrorStop = 120; + iErrorStop = 220; rc = XB_WRITE_ERROR; throw rc; } @@ -219,13 +232,13 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia xbFputc( 0x00 ); if(( xbFwrite( &SchemaPtr[i].cFieldLen, 1, 1 )) != XB_NO_ERROR ) { - iErrorStop = 130; + iErrorStop = 230; rc = XB_WRITE_ERROR; throw rc; } if(( xbFwrite( &SchemaPtr[i].cNoOfDecs, 1, 1 )) != XB_NO_ERROR ) { - iErrorStop = 140; + iErrorStop = 240; rc = XB_WRITE_ERROR; throw rc; } @@ -240,7 +253,7 @@ xbInt16 xbDbf3::CreateTable( const xbString & sTableName, const xbString & sAlia /* write the header terminator */ if(( xbFputc( XB_CHARHDR )) != XB_NO_ERROR ){ - iErrorStop = 150; + iErrorStop = 250; rc = XB_WRITE_ERROR; throw rc; } @@ -321,7 +334,7 @@ xbInt16 xbDbf3::Open( const xbString & sTableName, const xbString & sAlias, /* verify the file is not already open */ if( iDbfStatus != XB_CLOSED ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_ALREADY_OPEN; throw iRc; } @@ -330,32 +343,32 @@ xbInt16 xbDbf3::Open( const xbString & sTableName, const xbString & sAlias, this->sAlias = sAlias; if( !FileExists()){ - iErrorStop = 20; + iErrorStop = 110; iRc = XB_FILE_NOT_FOUND; throw iRc; } if(( iRc = xbase->AddTblToTblList( this, GetFqFileName(), sAlias )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } /* open the file */ if(( iRc = xbFopen( iOpenMode, iShareMode )) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } /* copy the header into memory */ if(( iRc = ReadHeader( 1, 0 )) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw iRc; } /* check the version */ if(( cVersion & 0x07 ) != 3 ){ // if( xFileVersion != 3 && xFileVersion != 4 ){ - iErrorStop = 60; + iErrorStop = 150; iRc = XB_FILE_TYPE_NOT_SUPPORTED; throw iRc; } @@ -369,19 +382,19 @@ xbInt16 xbDbf3::Open( const xbString & sTableName, const xbString & sAlias, } if(( RecBuf = (char *) malloc( (size_t) uiRecordLen )) == NULL ){ - iErrorStop = 70; + iErrorStop = 160; iRc = XB_NO_MEMORY; throw iRc; } if(( RecBuf2 = (char *) malloc( uiRecordLen )) == NULL ) { - iErrorStop = 80; + iErrorStop = 170; iRc = XB_NO_MEMORY; throw iRc; } if((SchemaPtr=(xbSchemaRec *)malloc( (size_t) iNoOfFields * sizeof( xbSchemaRec ))) == NULL){ - iErrorStop = 90; + iErrorStop = 180; iRc = XB_NO_MEMORY; throw iRc; } @@ -410,7 +423,7 @@ xbInt16 xbDbf3::Open( const xbString & sTableName, const xbString & sAlias, ulCurRec = 0L; iDbfStatus = XB_OPEN; if(( iRc = BlankRecord()) != XB_NO_ERROR ){ - iErrorStop = 100; + iErrorStop = 190; throw iRc; } @@ -430,11 +443,11 @@ xbInt16 xbDbf3::Open( const xbString & sTableName, const xbString & sAlias, Memo = new xbMemoDbt3( this, fTemp.GetFqFileName()); if(( iRc = Memo->OpenMemoFile()) != XB_NO_ERROR ){ - iErrorStop = 110; + iErrorStop = 200; throw iRc; } } - #endif + #endif // XB_MEMO_SUPPORT } catch ( xbInt16 iRc ) @@ -458,13 +471,13 @@ xbInt16 xbDbf3::Open( const xbString & sTableName, const xbString & sAlias, } InitVars(); -#ifdef XB_MEMO_SUPPORT + #ifdef XB_MEMO_SUPPORT if( Memo ){ Memo->CloseMemoFile(); delete Memo; Memo = NULL; } -#endif + #endif // XB_MEMO_SUPPORT } if( iRc == XB_NO_ERROR ) @@ -472,6 +485,179 @@ xbInt16 xbDbf3::Open( const xbString & sTableName, const xbString & sAlias, return iRc; } +/************************************************************************/ +//! @brief Rename table. +/*! + This routine renames a give table, associated memo and inf files + \param sNewName - New file name. + \returns Return Codes +*/ +xbInt16 xbDbf3::Rename( const xbString sNewName ){ + + xbInt16 iRc = XB_NO_ERROR; + xbInt16 iErrorStop = 0; + + xbString sNewDbf; + + xbBool bDbfRenamed = xbFalse; + + #ifdef XB_INF_SUPPORT + xbString sNewInf; + xbString sThisInf; + xbBool bInfRenamed = xbFalse; + #endif + + #ifdef XB_MEMO_SUPPORT + xbString sNewDbt; + xbString sThisDbt; + xbBool bDbtRenamed = xbFalse; + #endif // XB_MEMO_SUPPORT + + #ifdef XB_LOCKING_SUPPORT + xbBool bLocked = xbFalse; + #endif + + try{ + + xbString sDir; + xbString sFile; + xbString sExt; + + xbString sNewNameWoExt; + sNewNameWoExt.Set( sNewName ); + if( sNewName.Pos( ".DBF" ) > 0 ) + sNewNameWoExt.Left( sNewName.Len() - 4 ); + + GetFileDirPart ( sDir ); + GetFileNamePart( sFile ); + GetFileExtPart ( sExt ); + + if( FileExists( sNewDbf )) return XB_FILE_EXISTS; + sNewDbf.Sprintf( "%s%s.DBF", sDir.Str(), sNewNameWoExt.Str()); + + #ifdef XB_MEMO_SUPPORT + sNewDbt.Sprintf( "%s%s.DBT", sDir.Str(), sNewNameWoExt.Str()); + if( FileExists( sNewDbt )) return XB_FILE_EXISTS; + sThisDbt.Sprintf( "%s%s.DBT", sDir.Str(), sFile.Str()); + #endif + + #ifdef XB_INF_SUPPORT + sNewInf.Sprintf( "%s%s.INF", sDir.Str(), sNewNameWoExt.Str()); + if( FileExists( sNewInf )) return XB_FILE_EXISTS; + sThisInf.Sprintf( "%s%s.INF", sDir.Str(), sFile.Str()); + #endif // XB_INF_SUPPORT + + #ifdef XB_LOCKING_SUPPORT + if( GetAutoLock() && GetTableLocked() ){ + if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ + iErrorStop = 100; + throw iRc; + } + bLocked = xbTrue; + } + #endif + + xbInt16 iOpenMode = GetOpenMode(); + xbInt16 iShareMode = GetShareMode(); + xbBool bWasOpen = xbFalse; + if( FileIsOpen() ){ + bWasOpen = xbTrue; + if(( iRc = xbFclose()) != XB_NO_ERROR ){ + iErrorStop = 110; + throw iRc; + } + } + + if(( iRc = xbRename( GetFqFileName().Str(), sNewDbf.Str())) != XB_NO_ERROR ){ + iErrorStop = 120; + throw iRc; + } else { + bDbfRenamed = xbTrue; + } + xbString sNameWext; + sNameWext.Sprintf( "%s.DBF", sNewNameWoExt.Str()); + SetFileName( sNameWext ); + + if( bWasOpen ){ + if(( iRc = xbFopen( iOpenMode, iShareMode )) != XB_NO_ERROR ){ + iErrorStop = 130; + throw iRc; + } + } + + #ifdef XB_MEMO_SUPPORT + if( FileExists( sThisDbt )){ + if( bWasOpen ){ + if(( iRc = Memo->xbFclose()) != XB_NO_ERROR ){ + iErrorStop = 140; + throw iRc; + } + } + if(( xbRename( sThisDbt.Str(), sNewDbt.Str())) != XB_NO_ERROR ){ + iErrorStop = 150; + throw iRc; + } + Memo->SetFileName( sNewDbt ); + if( bWasOpen ){ + if(( iRc = Memo->xbFopen( iOpenMode, iShareMode )) != XB_NO_ERROR ){ + iErrorStop = 160; + throw iRc; + } + } + bDbtRenamed = xbTrue; + } + #endif // XB_MEMO_SUPPORT + + #ifdef XB_INF_SUPPORT + if( FileExists( sThisInf )){ + if(( iRc = xbRename( sThisInf.Str(), sNewInf.Str())) != XB_NO_ERROR ){ + iErrorStop = 170; + throw iRc; + } else { + bInfRenamed = xbTrue; + } + } + #endif // XB_INF_SUPPORT + + // rname the table in the table list + xbTblList *tle = xbase->GetTblListEntry( this ); + if( tle ){ + // std::cout << "setting [" << GetFqFileName().Str() << "][" << sNewNameWoExt.Str() << "]\n"; + tle->psTblName->Set( GetFqFileName().Str()); + tle->psTblAlias->Set( sNewNameWoExt.Str()); + } + } + catch ( xbInt16 iRc ) + { + xbString sMsg; + sMsg.Sprintf( "xbdbf3::Rename() Exception Caught Error Stop = %d iRc = %d", iErrorStop, iRc ); + xbase->WriteLogMessage( sMsg ); + xbase->WriteLogMessage( GetErrorMessage( iRc )); + + // attempt to reverse things out if unsuccessful + if( bDbfRenamed ){ + #ifdef XB_MEMO_SUPPORT + if( bDbtRenamed ){ + xbRename( sNewDbt.Str(), sThisDbt.Str()); + } + #endif // XB_MEMO_SUPPORT + #ifdef XB_INF_SUPPORT + if( bInfRenamed ){ + xbRename( sNewInf.Str(), sNewInf.Str()); + } + #endif // XB_INF_SUPPORT + } + + #ifdef XB_LOCKING_SUPPORT + if( bLocked ){ + LockTable( XB_UNLOCK ); + } + #endif + + } + return iRc; +} + /************************************************************************/ #ifdef XB_MEMO_SUPPORT @@ -540,9 +726,9 @@ xbInt16 xbDbf3::ValidateSchema( xbSchema * s ){ if( s[i].cType != 'C' && s[i].cType != 'N' && s[i].cType != 'D' && -#ifdef XB_MEMO_SUPPORT + #ifdef XB_MEMO_SUPPORT s[i].cType != 'M' && -#endif /* XB_MEMO_SUPPORT */ + #endif // XB_MEMO_SUPPORT s[i].cType != 'L' ){ return XB_INVALID_FIELD_TYPE; } @@ -559,13 +745,17 @@ xbInt16 xbDbf3::ValidateSchema( xbSchema * s ){ else if( s[i].cType == 'N' && s[i].iFieldLen > 19 ){ return XB_INVALID_FIELD_LEN; } + // field len must be >= no of decimals + else if( s[i].cType == 'N' && s[i].iFieldLen < s[i].iNoOfDecs ){ + return XB_INVALID_FIELD_LEN; + } -#ifdef XB_MEMO_SUPPORT + #ifdef XB_MEMO_SUPPORT else if(s[i].cType == 'M'){ s[i].iFieldLen = 10; s[i].iNoOfDecs = 0; } -#endif /* XB_MEMO_SUPPORT */ + #endif // XB_MEMO_SUPPORT uiRecordLen += s[i].iFieldLen; i++; -- cgit v1.2.3