HB_BOOL hb_fsCopy( const char * pszSource, const char * pszDest ) { HB_BOOL fResult = HB_FALSE; PHB_FILE pSrcFile; if( ( pSrcFile = hb_fileExtOpen( pszSource, NULL, FO_READ | FO_SHARED | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { PHB_FILE pDstFile; HB_ERRCODE errCode; if( ( pDstFile = hb_fileExtOpen( pszDest, NULL, FXO_TRUNCATE | FO_READWRITE | FO_EXCLUSIVE | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { void * pbyBuffer = hb_xgrab( HB_FSCOPY_BUFFERSIZE ); for( ;; ) { HB_SIZE nBytesRead; if( ( nBytesRead = hb_fileRead( pSrcFile, pbyBuffer, HB_FSCOPY_BUFFERSIZE, -1 ) ) > 0 && nBytesRead != ( HB_SIZE ) FS_ERROR ) { if( nBytesRead != hb_fileWrite( pDstFile, pbyBuffer, nBytesRead, -1 ) ) { errCode = hb_fsError(); break; } } else { errCode = hb_fsError(); fResult = errCode == 0; break; } } hb_xfree( pbyBuffer ); hb_fileClose( pDstFile ); } else errCode = hb_fsError(); hb_fileClose( pSrcFile ); if( fResult ) { HB_FATTR ulAttr; if( hb_fileAttrGet( pszSource, &ulAttr ) ) hb_fileAttrSet( pszDest, ulAttr ); } hb_fsSetError( errCode ); } return fResult; }
static HB_SIZE ct_StrFile( const char * pFileName, const char * pcStr, HB_SIZE nLen, HB_BOOL bAppend, HB_FOFFSET nOffset, HB_BOOL bTrunc ) { HB_SIZE nWrite = 0; HB_BOOL bFile = hb_fileExists( pFileName, NULL ); PHB_FILE hFile = hb_fileExtOpen( pFileName, NULL, FO_WRITE | FO_PRIVATE | FXO_SHARELOCK | ( bAppend ? FXO_APPEND : FXO_TRUNCATE ) | ( ct_getsafety() ? FXO_UNIQUE : 0 ), NULL, NULL ); if( hFile ) { if( ! bFile ) hb_fileAttrSet( pFileName, ct_getfcreate() ); if( nOffset ) hb_fileSeek( hFile, nOffset, FS_SET ); else hb_fileSeek( hFile, 0, FS_END ); nWrite = hb_fileResult( hb_fileWrite( hFile, pcStr, nLen, -1 ) ); if( nWrite == nLen && bTrunc ) hb_fileWrite( hFile, NULL, 0, -1 ); hb_fileClose( hFile ); } return nWrite; }
static HB_BOOL hb_memowrit( HB_BOOL bHandleEOF ) { const char * pszFileName = hb_parc( 1 ); PHB_ITEM pString = hb_param( 2, HB_IT_STRING ); HB_BOOL bRetVal = HB_FALSE; if( pszFileName && pString ) { PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE | FXO_TRUNCATE | FXO_SHARELOCK | FXO_NOSEEKPOS, NULL, NULL ); if( pFile != NULL ) { HB_SIZE nSize = hb_itemGetCLen( pString ); bRetVal = hb_fileWriteAt( pFile, hb_itemGetCPtr( pString ), nSize, 0 ) == nSize; /* NOTE: CA-Cl*pper will add the EOF even if the write failed. [vszakats] */ /* NOTE: CA-Cl*pper will not return .F. when the EOF could not be written. [vszakats] */ if( bHandleEOF && bRetVal ) /* if true, then write EOF */ { char cEOF = HB_CHAR_EOF; hb_fileWriteAt( pFile, &cEOF, sizeof( char ), nSize ); } hb_fileClose( pFile ); } } return bRetVal; }
/* extended FILE IO handle destructor */ static HB_GARBAGE_FUNC( hb_file_Destructor ) { PHB_FILE * fileHolder = ( PHB_FILE * ) Cargo; if( * fileHolder ) { PHB_FILE pFile = * fileHolder; * fileHolder = NULL; hb_fileClose( pFile ); } }
/* * Close the table in the WorkArea. */ static HB_ERRCODE hb_sdfClose( SDFAREAP pArea ) { HB_TRACE( HB_TR_DEBUG, ( "hb_sdfClose(%p)", pArea ) ); /* Update record and unlock records */ if( pArea->pFile ) { SELF_GOCOLD( &pArea->area ); if( ! pArea->fReadonly && hb_setGetEOF() ) { hb_fileWrite( pArea->pFile, "\032", 1, -1 ); pArea->fFlush = HB_TRUE; } SELF_FLUSH( &pArea->area ); hb_fileClose( pArea->pFile ); pArea->pFile = NULL; } SUPER_CLOSE( &pArea->area ); if( pArea->pFieldOffset ) { hb_xfree( pArea->pFieldOffset ); pArea->pFieldOffset = NULL; } if( pArea->pRecord ) { hb_xfree( pArea->pRecord - 1 ); pArea->pRecord = NULL; } if( pArea->pBuffer ) { hb_xfree( pArea->pBuffer ); pArea->pBuffer = NULL; } if( pArea->szEol ) { hb_xfree( pArea->szEol ); pArea->szEol = NULL; } if( pArea->szFileName ) { hb_xfree( pArea->szFileName ); pArea->szFileName = NULL; } return HB_SUCCESS; }
static PHRB_BODY hb_hrbLoadFromFile( const char * szHrb, HB_USHORT usMode ) { PHRB_BODY pHrbBody = NULL; PHB_ITEM pError = NULL; PHB_FILE pFile; /* Open as binary */ do { pFile = hb_fileExtOpen( szHrb, hb_stackSetStruct()->HB_SET_DEFEXTENSIONS ? ".hrb" : NULL, FO_READ | FXO_SHARELOCK, NULL, pError ); if( pFile == NULL ) { pError = hb_errRT_FileError( pError, NULL, EG_OPEN, 6102, szHrb ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } while( pFile == NULL ); if( pError ) hb_itemRelease( pError ); if( pFile != NULL ) { HB_SIZE nBodySize = ( HB_SIZE ) hb_fileSize( pFile ); if( nBodySize ) { char * pbyBuffer; pbyBuffer = ( char * ) hb_xgrab( nBodySize + sizeof( char ) + 1 ); hb_fileReadAt( pFile, pbyBuffer, nBodySize, 0 ); pbyBuffer[ nBodySize ] = '\0'; pHrbBody = hb_hrbLoad( ( const char * ) pbyBuffer, nBodySize, usMode, szHrb ); hb_xfree( pbyBuffer ); } hb_fileClose( pFile ); } return pHrbBody; }
static HB_BOOL hb_memowrit( HB_BOOL bHandleEOF ) { const char * pszFileName = hb_parc( 1 ); PHB_ITEM pString = hb_param( 2, HB_IT_STRING ); HB_BOOL bRetVal = HB_FALSE; if( pszFileName && pString ) { PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE | FXO_TRUNCATE | FXO_SHARELOCK, NULL, NULL ); if( pFile != NULL ) { HB_SIZE nSize = hb_itemGetCLen( pString ); const char * pData = hb_itemGetCPtr( pString ); while( nSize > 0 ) { HB_SIZE nWritten = hb_fileWrite( pFile, pData, nSize, 0 ); if( nWritten == 0 || nWritten == ( HB_SIZE ) FS_ERROR ) break; nSize -= nWritten; pData += nWritten; } bRetVal = nSize == 0; /* NOTE: CA-Cl*pper will add the EOF even if the write failed. [vszakats] */ /* NOTE: CA-Cl*pper will not return .F. when the EOF could not be written. [vszakats] */ if( bHandleEOF && bRetVal ) /* if true, then write EOF */ { char cEOF = HB_CHAR_EOF; hb_fileWrite( pFile, &cEOF, sizeof( char ), -1 ); } hb_fileClose( pFile ); } } return bRetVal; }
HB_EXPORT HB_FOFFSET hb_fileSizeGet( const char * pszFileName, HB_BOOL bUseDirEntry ) { int i = s_fileFindDrv( pszFileName ); if( i >= 0 ) { HB_ERRCODE uiError; HB_FOFFSET nSize = 0; if( bUseDirEntry ) { PHB_ITEM pDir = hb_fileDirectory( pszFileName, "HS" ); uiError = hb_fsError(); if( pDir ) { PHB_ITEM pEntry = hb_arrayGetItemPtr( pDir, 1 ); if( pEntry ) nSize = hb_arrayGetNInt( pEntry, F_SIZE ); hb_itemRelease( pDir ); } } else { PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, FO_READ | FO_COMPAT, NULL, NULL ); if( pFile ) { nSize = hb_fileSize( pFile ); uiError = hb_fsError(); hb_fileClose( pFile ); } else uiError = hb_fsError(); } hb_fsSetFError( uiError ); return nSize; } return hb_fsFSize( pszFileName, bUseDirEntry ); }
static void hb_memoread( HB_BOOL bHandleEOF ) { const char * pszFileName = hb_parc( 1 ); if( pszFileName ) { PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, FO_READ | FO_SHARED | FO_PRIVATE | FXO_SHARELOCK | FXO_NOSEEKPOS, NULL, NULL ); if( pFile != NULL ) { HB_SIZE nSize = ( HB_SIZE ) hb_fileSize( pFile ); if( nSize != 0 ) { char * pbyBuffer = ( char * ) hb_xgrab( nSize + 1 ); nSize = hb_fileReadAt( pFile, pbyBuffer, nSize, 0 ); /* Don't read the file terminating EOF character */ if( bHandleEOF && nSize > 0 ) { if( pbyBuffer[ nSize - 1 ] == HB_CHAR_EOF ) --nSize; } hb_retclen_buffer( pbyBuffer, nSize ); } else hb_retc_null(); hb_fileClose( pFile ); } else hb_retc_null(); } else hb_retc_null(); }
static void s_consrv_close( PHB_CONSRV conn ) { int i = 0; if( conn->rpcFilter ) hb_itemRelease( conn->rpcFilter ); while( conn->streams ) { PHB_CONSTREAM stream = conn->streams; conn->streams = stream->next; hb_xfree( stream ); } if( conn->mutex ) hb_itemRelease( conn->mutex ); if( conn->sd != HB_NO_SOCKET ) hb_socketClose( conn->sd ); if( conn->zstream ) hb_znetClose( conn->zstream ); while( conn->filesCount > 0 ) { if( i >= NETIO_FILES_MAX ) break; /* internal error, it should not happen */ if( conn->fileTable[ i ] ) { hb_fileClose( conn->fileTable[ i ] ); conn->filesCount--; } ++i; } hb_xfree( conn ); }
/* * Close the table in the WorkArea. */ static HB_ERRCODE hb_sdfClose( SDFAREAP pArea ) { HB_TRACE( HB_TR_DEBUG, ( "hb_sdfClose(%p)", pArea ) ); SUPER_CLOSE( &pArea->area ); /* Update record and unlock records */ if( pArea->pFile ) { SELF_FLUSH( &pArea->area ); hb_fileClose( pArea->pFile ); pArea->pFile = NULL; } if( pArea->pFieldOffset ) { hb_xfree( pArea->pFieldOffset ); pArea->pFieldOffset = NULL; } if( pArea->pRecord ) { hb_xfree( pArea->pRecord - 1 ); pArea->pRecord = NULL; } if( pArea->szEol ) { hb_xfree( pArea->szEol ); pArea->szEol = NULL; } if( pArea->szFileName ) { hb_xfree( pArea->szFileName ); pArea->szFileName = NULL; } return HB_SUCCESS; }
static HB_BOOL hb_copyfile( const char * pszSource, const char * pszDest ) { HB_BOOL bRetVal = HB_FALSE; PHB_FILE pSource; PHB_ITEM pError = NULL; HB_TRACE( HB_TR_DEBUG, ( "hb_copyfile(%s, %s)", pszSource, pszDest ) ); do { pSource = hb_fileExtOpen( pszSource, NULL, FO_READ | FO_SHARED | FO_PRIVATE | FXO_DEFAULTS | FXO_SHARELOCK, NULL, pError ); if( pSource == NULL ) { pError = hb_errRT_FileError( pError, NULL, EG_OPEN, 2012, pszSource ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } while( pSource == NULL ); if( pError ) { hb_itemRelease( pError ); pError = NULL; } if( pSource != NULL ) { PHB_FILE pDest; do { pDest = hb_fileExtOpen( pszDest, NULL, FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE | FXO_TRUNCATE | FXO_DEFAULTS | FXO_SHARELOCK, NULL, pError ); if( pDest == NULL ) { pError = hb_errRT_FileError( pError, NULL, EG_CREATE, 2012, pszDest ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } while( pDest == NULL ); if( pError ) { hb_itemRelease( pError ); pError = NULL; } if( pDest != NULL ) { HB_UCHAR * buffer; HB_SIZE nRead; buffer = ( HB_UCHAR * ) hb_xgrab( BUFFER_SIZE ); bRetVal = HB_TRUE; while( ( nRead = hb_fileRead( pSource, buffer, BUFFER_SIZE, -1 ) ) != 0 ) { HB_SIZE nWritten = 0; while( nWritten < nRead ) { nWritten += hb_fileWrite( pDest, buffer + nWritten, nRead - nWritten, -1 ); if( nWritten < nRead ) { pError = hb_errRT_FileError( pError, NULL, EG_WRITE, 2016, pszDest ); if( hb_errLaunch( pError ) != E_RETRY ) { bRetVal = HB_FALSE; break; } } } } if( pError ) hb_itemRelease( pError ); hb_xfree( buffer ); hb_fileClose( pDest ); } hb_fileClose( pSource ); #if defined( HB_OS_UNIX ) if( bRetVal ) { HB_FATTR ulAttr; if( hb_fileAttrGet( pszSource, &ulAttr ) ) hb_fileAttrSet( pszDest, ulAttr ); } #endif } return bRetVal; }