diff options
Diffstat (limited to 'src/core/xbdbf4.cpp')
-rwxr-xr-x | src/core/xbdbf4.cpp | 357 |
1 files changed, 324 insertions, 33 deletions
diff --git a/src/core/xbdbf4.cpp b/src/core/xbdbf4.cpp index c421add..2b49be9 100755 --- a/src/core/xbdbf4.cpp +++ b/src/core/xbdbf4.cpp @@ -71,33 +71,46 @@ xbInt16 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, /* 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 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, 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 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, Memo = new xbMemoDbt4( this, fTemp.GetFqFileName()); if(( rc = Memo->CreateMemoFile()) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw rc; } } @@ -145,18 +158,18 @@ xbInt16 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, #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; } @@ -176,12 +189,12 @@ xbInt16 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, /* 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; } @@ -207,13 +220,13 @@ xbInt16 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, k += SchemaPtr[i].cFieldLen; if(( xbFwrite( &SchemaPtr[i].cFieldName, 1, 11 )) != XB_NO_ERROR ) { - iErrorStop = 120; + iErrorStop = 200; rc = XB_WRITE_ERROR; throw rc; } if(( xbFwrite( &SchemaPtr[i].cType, 1, 1 )) != XB_NO_ERROR ) { - iErrorStop = 130; + iErrorStop = 210; rc = XB_WRITE_ERROR; throw rc; } @@ -222,13 +235,13 @@ xbInt16 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, xbFputc( 0x00 ); if(( xbFwrite( &SchemaPtr[i].cFieldLen, 1, 1 )) != XB_NO_ERROR ) { - iErrorStop = 140; + iErrorStop = 220; rc = XB_WRITE_ERROR; throw rc; } if(( xbFwrite( &SchemaPtr[i].cNoOfDecs, 1, 1 )) != XB_NO_ERROR ) { - iErrorStop = 150; + iErrorStop = 230; rc = XB_WRITE_ERROR; throw rc; } @@ -243,7 +256,7 @@ xbInt16 xbDbf4::CreateTable( const xbString &sTableName, const xbString &sAlias, /* write the header terminator */ if(( xbFputc( XB_CHARHDR )) != XB_NO_ERROR ){ - iErrorStop = 160; + iErrorStop = 240; rc = XB_WRITE_ERROR; throw rc; } @@ -321,7 +334,7 @@ xbInt16 xbDbf4::Open( const xbString & sTableName, const xbString & sAlias, try{ /* 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 xbDbf4::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(( xFileVersion = DetermineXbaseTableVersion( cVersion )) != 4 ){ if(( cVersion & 0x07 ) != 3 ){ - iErrorStop = 60; + iErrorStop = 150; iRc = XB_FILE_TYPE_NOT_SUPPORTED; throw iRc; } @@ -369,19 +382,19 @@ xbInt16 xbDbf4::Open( const xbString & sTableName, const xbString & sAlias, } if(( RecBuf = (char *) malloc( 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; } @@ -411,7 +424,7 @@ xbInt16 xbDbf4::Open( const xbString & sTableName, const xbString & sAlias, ulCurRec = 0L; iDbfStatus = XB_OPEN; if(( iRc = BlankRecord()) != XB_NO_ERROR ){ - iErrorStop = 100; + iErrorStop = 190; throw iRc; } @@ -432,7 +445,7 @@ xbInt16 xbDbf4::Open( const xbString & sTableName, const xbString & sAlias, Memo = new xbMemoDbt4( this, fTemp.GetFqFileName()); if(( iRc = Memo->OpenMemoFile()) != XB_NO_ERROR ){ - iErrorStop = 110; + iErrorStop = 200; throw iRc; } } @@ -448,7 +461,7 @@ xbInt16 xbDbf4::Open( const xbString & sTableName, const xbString & sAlias, sIxFileName.PutAt( lLen-1, 'D' ); sIxFileName.PutAt( lLen, 'X' ); if(( iRc = OpenIndex( "MDX", sIxFileName )) != XB_NO_ERROR ){ - iErrorStop = 120; + iErrorStop = 210; throw iRc; } } @@ -491,6 +504,279 @@ xbInt16 xbDbf4::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 <a href="xbretcod_8h.html">Return Codes</a> +*/ +xbInt16 xbDbf4::Rename( const xbString sNewName ){ + + xbInt16 iRc = XB_NO_ERROR; + xbInt16 iErrorStop = 0; + + xbString sNewDbf; + + #ifdef XB_MEMO_SUPPORT + xbString sNewDbt; + xbBool bDbtRenamed = xbFalse; + xbString sThisDbt; + #endif + + #ifdef XB_MDX_SUPPORT + xbString sNewMdx; + xbBool bMdxRenamed = xbFalse; + xbString sThisMdx; + #endif + + #ifdef XB_INF_SUPPORT + xbString sNewInf; + xbString sThisInf; + xbBool bInfRenamed = xbFalse; + #endif // XB_INF_SUPPORT + + xbBool bDbfRenamed = xbFalse; + + #ifdef XB_LOCKING_SUPPORT + xbBool bLocked = xbFalse; + #endif // XB_LOCKIN_SUPPORT + + + try{ + + xbString sDir; + xbString sFile; + xbString sExt; + xbString sNewNameWoExt; + sNewNameWoExt.Set( sNewName ); + if( sNewName.Pos( ".DBF" ) > 0 ) + sNewNameWoExt.Left( sNewName.Len() - 4 ); + +// std::cout << "NewName wo ext = [" << sNewNameWoExt.Str() << "]\n"; + + GetFileDirPart ( sDir ); + GetFileNamePart( sFile ); + GetFileExtPart ( sExt ); + + 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_MDX_SUPPORT + sNewMdx.Sprintf( "%s%s.MDX", sDir.Str(), sNewNameWoExt.Str()); + if( FileExists( sNewMdx )) return XB_FILE_EXISTS; + sThisMdx.Sprintf( "%s%s.MDX", 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 + + +/* + std::cout << "xbDbf3::Rename dir = [" << sDir.Str() << "] file = [" << sFile.Str() << "] ext = [" << sExt.Str() << "]\n"; + std::cout << "xbDbf3::Rename new dbf = [" << sNewDbf.Str() << "]\n"; + std::cout << "xbDbf3::Rename new dbt = [" << sNewDbt.Str() << "]\n"; + std::cout << "xbDbf3::Rename new inf = [" << sNewInf.Str() << "]\n"; + std::cout << "xbDbf3::Rename new mdx = [" << sNewMdx.Str() << "]\n"; +*/ + + if( FileExists( sNewDbf )) return XB_FILE_EXISTS; + + #ifdef XB_LOCKING_SUPPORT + if( GetAutoLock() ){ + 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(( iRc = Memo->xbFseek( 8, SEEK_SET )) != XB_NO_ERROR ){ + iErrorStop = 140; + throw iRc; + } + + sNewNameWoExt.PadRight( ' ', 8 ); + for( int i = 1; i < 9; i++ ) + Memo->xbFputc( sNewNameWoExt[i] ); + + if( bWasOpen ){ + if(( iRc = Memo->xbFclose()) != XB_NO_ERROR ){ + iErrorStop = 150; + throw iRc; + } + } + + Memo->SetFileName( sNewDbt ); + if(( xbRename( sThisDbt.Str(), sNewDbt.Str())) != XB_NO_ERROR ){ + iErrorStop = 160; + throw iRc; + } + + if( bWasOpen ){ + if(( iRc = Memo->xbFopen( iOpenMode, iShareMode )) != XB_NO_ERROR ){ + iErrorStop = 150; + throw iRc; + } + } + + bDbtRenamed = xbTrue; + } + #endif + + #ifdef XB_MDX_SUPPORT + if( FileExists( sThisMdx )){ + xbIxMdx *ixMdx; + xbString s; + xbBool bMdxFound = xbFalse; + xbIxList *ixList = GetIxList(); + while( ixList && !bMdxFound ){ + s = ixList->sFmt->Str(); + if( s == "MDX" ){ + ixMdx = (xbIxMdx *) ixList->ix; + bMdxFound = xbTrue; + } + } + + if( bMdxFound ){ + if(( iRc = ixMdx->xbFseek( 4, SEEK_SET )) != XB_NO_ERROR ){ + iErrorStop = 180; + throw iRc; + } + + sNewNameWoExt.PadRight( ' ', 8 ); + for( int i = 1; i < 9; i++ ) + ixMdx->xbFputc( sNewNameWoExt[i] ); + + if( bWasOpen ){ + if(( iRc = ixMdx->xbFclose()) != XB_NO_ERROR ){ + iErrorStop = 190; + throw iRc; + } + } + + ixMdx->SetFileName( sNewMdx ); + if(( xbRename( sThisMdx.Str(), sNewMdx.Str())) != XB_NO_ERROR ){ + iErrorStop = 200; + throw iRc; + } + + if( bWasOpen ){ + if(( iRc = ixMdx->xbFopen( iOpenMode, iShareMode )) != XB_NO_ERROR ){ + iErrorStop = 210; + throw iRc; + } + } + bMdxRenamed = xbTrue; + } + } + #endif // XB_MDX_SUPPORT + + #ifdef XB_INF_SUPPORT + if( FileExists( sThisInf )){ + if(( xbRename( sThisInf.Str(), sNewInf.Str())) != XB_NO_ERROR ){ + iErrorStop = 180; + throw iRc; + } else { + bInfRenamed = xbTrue; + } + } + #endif // XB_INF_SUPPORT + + // rename the table in the table list + xbTblList *tle = xbase->GetTblListEntry( this ); + if( tle ){ + tle->psTblName->Set( GetFqFileName().Str()); + tle->psTblAlias->Set( sNewNameWoExt.Str()); + } + + + } + catch ( xbInt16 iRc ) + { + xbString sMsg; + sMsg.Sprintf( "xbdbf4::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 + + #ifdef XB_MDX_SUPPORT + if( bMdxRenamed ){ + xbRename( sNewMdx.Str(), sThisMdx.Str()); + } + #endif + + #ifdef XB_INF_SUPPORT + if( bInfRenamed ){ + xbRename( sNewInf.Str(), sNewInf.Str()); + } + #endif // XB_INF_SUPPORT + } + + #ifdef XB_LOCKING_SUPPORT + if( GetAutoLock() ){ + iRc = LockTable( XB_UNLOCK ); + } + #endif + + } + + #ifdef XB_LOCKING_SUPPORT + if( bLocked ){ + LockTable( XB_UNLOCK ); + } + #endif + + return iRc; +} /************************************************************************/ #ifdef XB_MEMO_SUPPORT @@ -573,6 +859,11 @@ xbInt16 xbDbf4::ValidateSchema( xbSchema * s ){ return XB_INVALID_FIELD_LEN; } + // field len must be greater then number of decimals + else if((s[i].cType == 'N' || s[i].cType == 'F') && s[i].iFieldLen < s[i].iNoOfDecs ){ + return XB_INVALID_FIELD_LEN; + } + #ifdef XB_MEMO_SUPPORT else if(s[i].cType == 'M'){ s[i].iFieldLen = 10; |