1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
/**
* Writes the content of a temporary file into the final result file.
* The result file is assumed to be already open in write mode, after writing, content isn't flushed nor closed.
*
* @param tempFile A reference to a temporary file structure (or buffer), device will be opened in RO mode.
* @param finalFile A reference to the final file, device needs to be already OPEN in Write mode.
*/
void SyncResultWriter::mergeFile(QIODevice& tempFile, QFile& finalFile )
{
if( !tempFile.open( QIODevice::ReadOnly ) )
{
setError("Could not open temp file for merging!");
return;
}
const int B_SIZE = 4096;
char inBuffer[B_SIZE];
//reads temp file block per block
int bytesRead = tempFile.read(inBuffer, B_SIZE) ;
while( bytesRead > 0 )
{
//write the current block to the definitive file
if( finalFile.write( inBuffer, bytesRead ) == -1 )
{
setError( QString( "Error writing chunk : ") + finalFile.errorString() );
return;
}
//next chunk
bytesRead = tempFile.read(inBuffer, B_SIZE);
}
//if the loop was broken because of a reading error :
if( bytesRead == -1 )
{
setError( QString( "Error reading chunk : ") + tempFile.errorString() );
return;
}
//finalize line
finalFile.write( "\n");
tempFile.close();
}
/**
* Informs if an error has occured somewhere during the writing process.
* The result of this method can be tested after task completion.
* @return True if an error occured, false otherwise.
*/
bool SyncResultWriter::hasError()
{
return this->foundError;
}
/**
* Returns the lastest error message. Use with hasError().
* @return An error message String, or an empty String.
*/
QString SyncResultWriter::errorString()
{
return this->errorMessage;
}
/**
* Method used internally to set this object in error state.
* @param message The error message to set.
*/
void SyncResultWriter::setError(QString message)
{
this->foundError = true;
this->errorMessage = message;
} |
Partager