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; }
/* Output an item to the screen and/or printer and/or alternate */ void hb_conOutAlt( const char * szStr, HB_SIZE nLen ) { PHB_FILE pFile; HB_TRACE( HB_TR_DEBUG, ( "hb_conOutAlt(%s, %" HB_PFS "u)", szStr, nLen ) ); if( hb_setGetConsole() ) hb_gtWriteCon( szStr, nLen ); if( hb_setGetAlternate() && ( pFile = hb_setGetAltHan() ) != NULL ) { /* Print to alternate file if SET ALTERNATE ON and valid alternate file */ hb_fileWrite( pFile, szStr, nLen, -1 ); } if( ( pFile = hb_setGetExtraHan() ) != NULL ) { /* Print to extra file if valid alternate file */ hb_fileWrite( pFile, szStr, nLen, -1 ); } if( ( pFile = hb_setGetPrinterHandle( HB_SET_PRN_CON ) ) != NULL ) { /* Print to printer if SET PRINTER ON and valid printer file */ hb_fileWrite( pFile, szStr, nLen, -1 ); hb_prnPos()->col += ( int ) nLen; } }
static HB_BOOL _writeeol( PHB_FILE fhnd ) { const char * eol = hb_conNewLine(); HB_SIZE len = strlen( eol ); return hb_fileWrite( fhnd, eol, len, -1 ) == len; }
/* * Perform a write of WorkArea memory to the data store. */ static HB_ERRCODE hb_sdfGoCold( SDFAREAP pArea ) { HB_TRACE( HB_TR_DEBUG, ( "hb_sdfGoCold(%p)", pArea ) ); if( pArea->fRecordChanged ) { HB_SIZE nSize = pArea->uiRecordLen + pArea->uiEolLen; if( hb_fileWrite( pArea->pFile, pArea->pRecord, nSize, -1 ) != nSize ) { PHB_ITEM pError = hb_errNew(); hb_errPutGenCode( pError, EG_WRITE ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_WRITE ) ); hb_errPutSubCode( pError, EDBF_WRITE ); hb_errPutOsCode( pError, hb_fsError() ); hb_errPutFileName( pError, pArea->szFileName ); SELF_ERROR( &pArea->area, pError ); hb_itemRelease( pError ); return HB_FAILURE; } pArea->fRecordChanged = HB_FALSE; pArea->fFlush = HB_TRUE; } return HB_SUCCESS; }
static void hb_flushFBuffer( PHB_FILEBUF pFileBuf ) { if( pFileBuf->nPos > 0 ) { hb_fileWrite( pFileBuf->pFile, pFileBuf->pBuf, pFileBuf->nPos, -1 ); pFileBuf->nPos = 0; } }
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_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; }
/* deletes xxx bytes from the current file, beginning at the current record */ static int _del_buff( PFT_TEXT ft_text, HB_ISIZ iLen ) { char * WriteBuff = ( char * ) hb_xgrab( BUFFSIZE ); HB_FOFFSET fpRead, fpWrite; HB_ISIZ WriteLen; /* initialize file pointers */ fpWrite = ft_text->offset[ ft_text->area ]; fpRead = ft_text->offset[ ft_text->area ] + iLen; /* do initial load of buffer */ WriteLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE, fpRead ) ); fpRead += WriteLen; ft_text->error[ ft_text->area ] = 0; while( WriteLen > 0 ) { HB_ISIZ SaveLen; /* position to beginning of write area */ SaveLen = hb_fileResult( hb_fileWriteAt( ft_text->handles[ ft_text->area ], WriteBuff, WriteLen, fpWrite ) ); /* move write pointer */ fpWrite += SaveLen; if( SaveLen != WriteLen ) { /* error, fetch errcode and quit */ ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* return to read area and read another buffer */ WriteLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE, fpRead ) ); fpRead += WriteLen; } /* store length in bytes, set legacy EOF marker */ ft_text->lastbyte[ ft_text->area ] = hb_fileSeek( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ); hb_fileWrite( ft_text->handles[ ft_text->area ], WriteBuff, 0, -1 ); /* clear last_rec so next gobot will recount the records */ ft_text->last_rec[ ft_text->area ] = 0; hb_fileSeek( ft_text->handles[ ft_text->area ], ft_text->offset[ ft_text->area ], FS_SET ); hb_xfree( WriteBuff ); return ft_text->error[ ft_text->area ]; }
/* * 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; }
/* writes a line of data to the file, including the terminating EOL */ static int _writeLine( PFT_TEXT ft_text, const char * theData, HB_SIZE iDataLen ) { int err = 0; if( hb_fileWrite( ft_text->handles[ ft_text->area ], theData, iDataLen, -1 ) != iDataLen ) { err = 1; ft_text->error[ ft_text->area ] = hb_fsError(); } else if( ! _writeeol( ft_text->handles[ ft_text->area ] ) ) { err = 1; ft_text->error[ ft_text->area ] = hb_fsError(); } return err; }
/* Output an item to the screen and/or printer */ static void hb_conOutDev( const char * szStr, HB_SIZE nLen ) { PHB_FILE pFile; HB_TRACE( HB_TR_DEBUG, ( "hb_conOutDev(%s, %" HB_PFS "u)", szStr, nLen ) ); if( ( pFile = hb_setGetPrinterHandle( HB_SET_PRN_DEV ) ) != NULL ) { /* Display to printer if SET DEVICE TO PRINTER and valid printer file */ hb_fileWrite( pFile, szStr, nLen, -1 ); hb_prnPos()->col += ( int ) nLen; } else /* Otherwise, display to console */ hb_gtWrite( szStr, nLen ); }
static HB_BOOL hb_LZSSxFlush( PHB_LZSSX_COMPR pCompr ) { if( pCompr->fResult && pCompr->pOutput != NULL ) { if( hb_fileWrite( pCompr->pOutput, pCompr->outBuffer, pCompr->outBuffPos, -1 ) != pCompr->outBuffPos ) { pCompr->fResult = HB_FALSE; } else { pCompr->nOutSize += pCompr->outBuffPos; pCompr->outBuffPos = 0; } } return pCompr->fResult; }
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; }
/* the contents of the inserted bytes are indeterminate, i.e. you'll have to write to them before they mean anything */ static int _ins_buff( PFT_TEXT ft_text, HB_ISIZ iLen ) { char * ReadBuff = ( char * ) hb_xgrab( BUFFSIZE ); char * WriteBuff = ( char * ) hb_xgrab( BUFFSIZE ); char * SaveBuff; HB_FOFFSET fpRead, fpWrite; HB_ISIZ WriteLen, ReadLen; HB_ISIZ SaveLen; HB_ISIZ iLenRemaining = iLen; /* set target move distance, this allows iLen to be greater than BUFFSIZE */ iLen = HB_MIN( iLenRemaining, BUFFSIZE ); iLenRemaining -= iLen; /* initialize file pointers */ fpRead = ft_text->offset[ ft_text->area ]; fpWrite = ft_text->offset[ ft_text->area ] + iLen; /* do initial load of both buffers */ WriteLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE, fpRead ) ); fpRead += WriteLen; ReadLen = hb_fileResult( hb_fileRead( ft_text->handles[ ft_text->area ], ReadBuff, BUFFSIZE, -1 ) ); fpRead += ReadLen; ft_text->error[ ft_text->area ] = 0; while( ! ft_text->error[ ft_text->area ] && iLen > 0 ) { while( WriteLen > 0 ) { /* position to beginning of write area */ if( hb_fileSeek( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ) != fpWrite ) { ft_text->error[ ft_text->area ] = hb_fsError(); break; } if( ( SaveLen = hb_fileResult( hb_fileWrite( ft_text->handles[ ft_text->area ], WriteBuff, WriteLen, -1 ) ) ) == 0 ) { ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* move write pointer */ fpWrite += SaveLen; if( SaveLen != WriteLen ) { /* error, fetch errcode and quit */ ft_text->error[ ft_text->area ] = hb_fsError(); break; } #if 0 WriteLen = SaveLen; #endif /* swap buffers */ SaveBuff = WriteBuff; WriteBuff = ReadBuff; ReadBuff = SaveBuff; WriteLen = ReadLen; /* return to read area and read another buffer */ ReadLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], ReadBuff, BUFFSIZE, fpRead ) ); fpRead += ReadLen; } iLen = HB_MIN( iLenRemaining, BUFFSIZE ); iLenRemaining -= iLen; } /* store length in bytes, set legacy EOF marker */ ft_text->lastbyte[ ft_text->area ] = hb_fileSeek( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ); hb_fileWrite( ft_text->handles[ ft_text->area ], WriteBuff, 0, -1 ); /* clear last_rec so next gobot will recount the records */ ft_text->last_rec[ ft_text->area ] = 0; hb_fileSeek( ft_text->handles[ ft_text->area ], ft_text->offset[ ft_text->area ], FS_SET ); hb_xfree( ReadBuff ); hb_xfree( WriteBuff ); return ft_text->error[ ft_text->area ]; }
/* Export DBF content to a SQL script file */ static HB_ULONG hb_db2Sql( AREAP pArea, PHB_ITEM pFields, HB_MAXINT llNext, PHB_ITEM pWhile, PHB_ITEM pFor, const char * szDelim, const char * szSep, const char * szEsc, const char * szTable, PHB_FILE pFile, HB_BOOL fInsert, HB_BOOL fRecno ) { PHB_FILEBUF pFileBuf; HB_ULONG ulRecords = 0; HB_USHORT uiFields = 0, ui; PHB_ITEM pTmp; HB_BOOL fWriteSep = HB_FALSE; const char * szNewLine = hb_conNewLine(); char * szInsert = NULL; HB_BOOL fEof = HB_TRUE; HB_BOOL fNoFieldPassed = ( pFields == NULL || hb_arrayLen( pFields ) == 0 ); if( SELF_FIELDCOUNT( pArea, &uiFields ) != HB_SUCCESS ) return 0; if( fInsert && szTable ) szInsert = hb_xstrcpy( NULL, "INSERT INTO ", szTable, " VALUES ( ", NULL ); pFileBuf = hb_createFBuffer( pFile, HB_FILE_BUF_SIZE ); pTmp = hb_itemNew( NULL ); while( llNext-- > 0 ) { if( pWhile ) { if( SELF_EVALBLOCK( pArea, pWhile ) != HB_SUCCESS || ! hb_itemGetL( pArea->valResult ) ) break; } if( SELF_EOF( pArea, &fEof ) != HB_SUCCESS || fEof ) break; if( pFor ) { if( SELF_EVALBLOCK( pArea, pFor ) != HB_SUCCESS ) break; } if( ! pFor || hb_itemGetL( pArea->valResult ) ) { ++ulRecords; if( szInsert ) hb_addStrToFBuffer( pFileBuf, szInsert ); if( fRecno ) { HB_ULONG ulRec = ulRecords; char szRecno[ 13 ], * szVal; szVal = szRecno + sizeof( szRecno ); *--szVal = 0; do { *--szVal = ( char ) ( ulRec % 10 ) + '0'; ulRec /= 10; } while( ulRec ); hb_addStrToFBuffer( pFileBuf, szVal ); hb_addStrToFBuffer( pFileBuf, szSep ); } if( fNoFieldPassed ) { for( ui = 1; ui <= uiFields; ui++ ) { if( SELF_GETVALUE( pArea, ui, pTmp ) != HB_SUCCESS ) break; if( fWriteSep ) hb_addStrToFBuffer( pFileBuf, szSep ); fWriteSep = hb_exportBufSqlVar( pFileBuf, pTmp, szDelim, szEsc ); } if( ui <= uiFields ) break; } else { /* TODO: exporting only some fields */ } if( szInsert ) hb_addStrToFBuffer( pFileBuf, " );" ); hb_addStrToFBuffer( pFileBuf, szNewLine ); fWriteSep = HB_FALSE; } if( SELF_SKIP( pArea, 1 ) != HB_SUCCESS ) break; if( ( llNext % 10000 ) == 0 ) hb_inkeyPoll(); } if( szInsert ) hb_xfree( szInsert ); hb_destroyFBuffer( pFileBuf ); hb_itemRelease( pTmp ); #if 0 /* Writing EOF */ hb_fileWrite( pFile, "\x1A", 1, -1 ); #endif return ulRecords; }
static void hb_conDevPos( int iRow, int iCol ) { PHB_FILE pFile; HB_TRACE( HB_TR_DEBUG, ( "hb_conDevPos(%d, %d)", iRow, iCol ) ); /* Position printer if SET DEVICE TO PRINTER and valid printer file otherwise position console */ if( ( pFile = hb_setGetPrinterHandle( HB_SET_PRN_DEV ) ) != NULL ) { int iPRow = iRow; int iPCol = iCol + hb_setGetMargin(); PHB_PRNPOS pPrnPos = hb_prnPos(); if( pPrnPos->row != iPRow || pPrnPos->col != iPCol ) { char buf[ 256 ]; int iPtr = 0; if( pPrnPos->row != iPRow ) { if( ++pPrnPos->row > iPRow ) { memcpy( &buf[ iPtr ], "\x0C\x0D\x00\x00", 2 ); /* Source buffer is 4 bytes to make CodeGuard happy */ iPtr += 2; pPrnPos->row = 0; } else { memcpy( &buf[ iPtr ], s_szCrLf, s_iCrLfLen ); iPtr += s_iCrLfLen; } while( pPrnPos->row < iPRow ) { if( iPtr + s_iCrLfLen > ( int ) sizeof( buf ) ) { hb_fileWrite( pFile, buf, ( HB_USHORT ) iPtr, -1 ); iPtr = 0; } memcpy( &buf[ iPtr ], s_szCrLf, s_iCrLfLen ); iPtr += s_iCrLfLen; ++pPrnPos->row; } pPrnPos->col = 0; } else if( pPrnPos->col > iPCol ) { buf[ iPtr++ ] = '\x0D'; pPrnPos->col = 0; } while( pPrnPos->col < iPCol ) { if( iPtr == ( int ) sizeof( buf ) ) { hb_fileWrite( pFile, buf, ( HB_USHORT ) iPtr, -1 ); iPtr = 0; } buf[ iPtr++ ] = ' '; ++pPrnPos->col; } if( iPtr ) hb_fileWrite( pFile, buf, ( HB_USHORT ) iPtr, -1 ); } } else hb_gtSetPos( iRow, iCol ); }