/**************************************************************************** Desc: This routine obtains exclusive access to a database by creating a .lck file. FLAIM holds the .lck file open as long as the database is open. When the database is finally closed, it deletes the .lck file. This is only used for 3.x databases. ****************************************************************************/ RCODE flmCreateLckFile( const char * pszFilePath, IF_FileHdl ** ppLockFileHdlRV) { RCODE rc = NE_XFLM_OK; char szLockPath [F_PATH_MAX_SIZE]; char szDbBaseName [F_FILENAME_SIZE]; char * pszFileExt; IF_FileHdl * pLockFileHdl = NULL; char szFilePathStr[ F_PATH_MAX_SIZE]; if( RC_BAD( rc = gv_XFlmSysData.pFileSystem->pathToStorageString( pszFilePath, szFilePathStr))) { goto Exit; } // Extract the 8.3 name and put a .lck extension on it to create // the full path for the .lck file. if (RC_BAD( rc = gv_XFlmSysData.pFileSystem->pathReduce( szFilePathStr, szLockPath, szDbBaseName))) { goto Exit; } pszFileExt = &szDbBaseName [0]; while ((*pszFileExt) && (*pszFileExt != '.')) pszFileExt++; f_strcpy( pszFileExt, ".lck"); if( RC_BAD( rc = gv_XFlmSysData.pFileSystem->pathAppend( szLockPath, szDbBaseName))) { goto Exit; } if( RC_BAD( rc = gv_XFlmSysData.pFileSystem->createLockFile( szLockPath, &pLockFileHdl))) { goto Exit; } *ppLockFileHdlRV = (IF_FileHdl *)pLockFileHdl; pLockFileHdl = NULL; Exit: if (pLockFileHdl) { (void)pLockFileHdl->closeFile(); pLockFileHdl->Release(); pLockFileHdl = NULL; } return( rc); }
/**************************************************************************** Desc: *****************************************************************************/ RCODE F_DynamicList::dumpToFile() { RCODE rc = NE_FLM_OK; DLIST_NODE * pTmp; FLMUINT uiLoop; IF_FileHdl * pFileHdl = NULL; #define DLST_RESP_SIZE 256 char szResponse[ DLST_RESP_SIZE]; FLMUINT uiTermChar; FTX_SCREEN * pScreen; IF_FileSystem * pFileSystem = NULL; if( RC_BAD( rc = FlmGetFileSystem( &pFileSystem))) { goto Exit; } f_strcpy( szResponse, (const char *)DLIST_DUMPFILE_PATH); FTXWinGetScreen( m_pListWin, &pScreen); FTXGetInput( pScreen, "enter filename to dump to", szResponse, DLST_RESP_SIZE-1, &uiTermChar); if ( uiTermChar != FKB_ENTER) { goto Exit; } if (RC_BAD( rc = pFileSystem->doesFileExist( szResponse))) { //create file if it doesn't already exist if ( rc == NE_FLM_IO_PATH_NOT_FOUND) { rc = pFileSystem->createFile( szResponse, FLM_IO_RDWR, &pFileHdl); } else { goto Exit_local; } } else { rc = pFileSystem->openFile( szResponse, FLM_IO_RDWR, &pFileHdl); } TEST_RC_LOCAL( rc); { FLMUINT64 ui64FileSize = 0; FLMUINT uiBytesWritten = 0; //figure out size of file currently, so you can append to it pFileHdl->size( &ui64FileSize); pTmp = m_pFirst; uiLoop = 0; while( pTmp) { FLMBYTE * pszNextLine = (FLMBYTE*)(pTmp->pvData); TEST_RC_LOCAL( rc = pFileHdl->write( ui64FileSize, //offset to current file size f_strlen( (const char *)pszNextLine), pszNextLine, &uiBytesWritten)); ui64FileSize += uiBytesWritten; TEST_RC_LOCAL( rc = pFileHdl->write( ui64FileSize, //add in newline 1, (FLMBYTE*)"\n", &uiBytesWritten)); ui64FileSize += uiBytesWritten; pTmp = pTmp->pNext; } (void)pFileHdl->closeFile(); } Exit_local: {//give success/fail message char szMessage[ 256]; FLMUINT uiChar; FTXWinGetScreen( m_pListWin, &pScreen); if ( RC_OK( rc)) { f_sprintf( szMessage, "contents of focused list appended to %s", DLIST_DUMPFILE_PATH); } else { f_sprintf( szMessage, "error rc=%u dumping to file %s", (unsigned)rc, DLIST_DUMPFILE_PATH); } FTXDisplayMessage( pScreen, FLM_RED, FLM_WHITE, szMessage, "press ESC or ENTER to close dialog", &uiChar); } Exit: if (pFileHdl) { pFileHdl->Release(); pFileHdl = NULL; } if( pFileSystem) { pFileSystem->Release(); } return rc; }
/**************************************************************************** Desc: Read the ini file and parse its contents ****************************************************************************/ RCODE FTKAPI F_IniFile::read( const char * pszFileName) { RCODE rc = NE_FLM_OK; FLMBOOL bMore = FALSE; FLMBOOL bEOF = FALSE; #define INITIAL_READ_BUF_SIZE 100 FLMUINT uiReadBufSize = 0; FLMUINT uiBytesAvail = 0; FLMUINT uiBytesInLine = 0; char * pszReadBuf = NULL; FLMUINT uiLineNum = 0; IF_FileSystem * pFileSystem = f_getFileSysPtr(); f_assert( m_bReady); f_assert( !m_pFileHdl); // Open the file if (RC_BAD( rc = f_alloc( f_strlen( pszFileName) + 1, &m_pszFileName))) { goto Exit; } f_strcpy( m_pszFileName, pszFileName); // It's not an error if the file doesn't exist. If it does exist, // we'll read in its data. if( RC_BAD( pFileSystem->openFile( pszFileName, FLM_IO_RDONLY, &m_pFileHdl))) { goto Exit; } m_uiFileOffset = 0; // Read in and parse the file uiReadBufSize = INITIAL_READ_BUF_SIZE; if (RC_BAD( rc = f_alloc( uiReadBufSize, &pszReadBuf))) { goto Exit; } // Read in and parse each line in the file... while (!bEOF) { uiLineNum++; uiBytesAvail = uiReadBufSize; if( RC_BAD( rc = readLine( pszReadBuf, &uiBytesAvail, &bMore)) && rc != NE_FLM_IO_END_OF_FILE) { goto Exit; } if (rc == NE_FLM_IO_END_OF_FILE) { bEOF = TRUE; } // While there are more bytes in the line, re-alloc the buffer, and do // another read. uiBytesInLine = uiBytesAvail; while( bMore) { uiBytesAvail = uiReadBufSize; uiReadBufSize *= 2; if (RC_BAD( rc = f_realloc( uiReadBufSize, &pszReadBuf))) { goto Exit; } if (RC_BAD( rc = readLine( pszReadBuf+uiBytesAvail, &uiBytesAvail, &bMore)) && (rc != NE_FLM_IO_END_OF_FILE) ) { goto Exit; } if( rc == NE_FLM_IO_END_OF_FILE) { bEOF = TRUE; } uiBytesInLine += uiBytesAvail; } if ( (RC_OK( rc) || (rc == NE_FLM_IO_END_OF_FILE)) && (uiBytesInLine > 0) ) { // NumBytes will be 0 if the line was blank. No need // to call parseBuffer in this case if (RC_BAD( rc = parseBuffer( pszReadBuf, uiBytesInLine))) { if (rc == NE_FLM_SYNTAX) { rc = NE_FLM_OK; } else { goto Exit; } } } } Exit: // Close the file if( m_pFileHdl) { m_pFileHdl->closeFile(); m_pFileHdl->Release(); m_pFileHdl = NULL; } // Free the buffer if (pszReadBuf) { f_free( &pszReadBuf); } if (rc == NE_FLM_IO_END_OF_FILE) { rc = NE_FLM_OK; } return rc; }
/**************************************************************************** Desc: Copies the data stored in the INI_LINE structs to the ini file ****************************************************************************/ RCODE FTKAPI F_IniFile::write( void) { RCODE rc = NE_FLM_OK; FLMUINT uiBytesWritten; INI_LINE * pCurLine = NULL; FLMUINT uiFileOffset = 0; IF_FileSystem * pFileSystem = f_getFileSysPtr(); f_assert( m_bReady); if (!m_bModified) { // Nothing needs to be written goto Exit; } // Open the file f_assert( !m_pFileHdl); if (RC_BAD( rc = pFileSystem->createFile( m_pszFileName, FLM_IO_RDWR, &m_pFileHdl))) { goto Exit; } pCurLine = m_pFirstLine; while (pCurLine) { if (pCurLine->pszParamName) { // Output the param name if (RC_BAD (rc = m_pFileHdl->write( uiFileOffset, f_strlen( pCurLine->pszParamName), pCurLine->pszParamName, &uiBytesWritten))) { goto Exit; } uiFileOffset += uiBytesWritten; if (pCurLine->pszParamValue) { // Output the "=" and the value if (RC_BAD (rc = m_pFileHdl->write( uiFileOffset, 1, (void *)"=", &uiBytesWritten))) { goto Exit; } uiFileOffset += uiBytesWritten; if (RC_BAD (rc = m_pFileHdl->write( uiFileOffset, f_strlen( pCurLine->pszParamValue), pCurLine->pszParamValue, &uiBytesWritten))) { goto Exit; } uiFileOffset += uiBytesWritten; } } if (pCurLine->pszComment) { // Output the comment if (pCurLine->pszParamName) { if (RC_BAD (rc = m_pFileHdl->write( uiFileOffset, 2, (void *)" #", &uiBytesWritten))) { goto Exit; } } else { if (RC_BAD (rc = m_pFileHdl->write( uiFileOffset, 1, (void *)"#", &uiBytesWritten))) { goto Exit; } } uiFileOffset += uiBytesWritten; if (RC_BAD (rc = m_pFileHdl->write( uiFileOffset, f_strlen( pCurLine->pszComment), pCurLine->pszComment, &uiBytesWritten))) { goto Exit; } uiFileOffset += uiBytesWritten; } // Write out a newline... if (RC_BAD (rc = m_pFileHdl->write( uiFileOffset, f_strlen( "\n"), (void *)"\n", &uiBytesWritten))) { goto Exit; } uiFileOffset += uiBytesWritten; pCurLine = pCurLine->pNext; } m_bModified = FALSE; Exit: if (m_pFileHdl) { m_pFileHdl->closeFile(); m_pFileHdl->Release(); m_pFileHdl = NULL; } return( rc); }
/**************************************************************************** Desc: Returns the requested file handle ****************************************************************************/ RCODE F_MultiFileHdl::getFileHdl( FLMUINT uiFileNum, FLMBOOL bGetForWrite, IF_FileHdl ** ppFileHdl) { RCODE rc = NE_FLM_OK; IF_FileSystem * pFileSystem = f_getFileSysPtr(); FLMUINT uiSlot; IF_FileHdl * pTmpHdl; char szPath[ F_PATH_MAX_SIZE]; f_assert( m_bOpen); *ppFileHdl = NULL; uiSlot = uiFileNum % F_MULTI_FHDL_LIST_SIZE; pTmpHdl = m_pFileHdlList[ uiSlot].pFileHdl; if( pTmpHdl && m_pFileHdlList[ uiSlot].uiFileNum != uiFileNum) { if( RC_BAD( rc = pTmpHdl->flush())) { goto Exit; } pTmpHdl->closeFile(); pTmpHdl->Release(); pTmpHdl = NULL; f_memset( &m_pFileHdlList[ uiSlot], 0, sizeof( FH_INFO)); } if( !pTmpHdl) { dataFilePath( uiFileNum, szPath); if( RC_BAD( rc = pFileSystem->openFile( szPath, FLM_IO_RDWR, &pTmpHdl))) { if( rc == NE_FLM_IO_PATH_NOT_FOUND && bGetForWrite) { if( RC_BAD( rc = pFileSystem->createFile( szPath, FLM_IO_RDWR, &pTmpHdl))) { goto Exit; } } else { goto Exit; } } m_pFileHdlList[ uiSlot].pFileHdl = pTmpHdl; m_pFileHdlList[ uiSlot].uiFileNum = uiFileNum; f_assert( !m_pFileHdlList[ uiSlot].bDirty); } *ppFileHdl = m_pFileHdlList[ uiSlot].pFileHdl; if( bGetForWrite) { m_pFileHdlList[ uiSlot].bDirty = TRUE; } Exit: return( rc); }