Overwrite file with new information @input: - filePath - string with absolute path to csv-file - content - not empty handler of content for csv-file - codec - pointer to codec object that would be used for file writing @output: - bool - True if file was overwritten with new data, otherwise False
| 96 | // @output: |
| 97 | // - bool - True if file was overwritten with new data, otherwise False |
| 98 | bool WriterPrivate::overwriteFile( |
| 99 | const QString& filePath, |
| 100 | ContentIterator& content, |
| 101 | QTextCodec* codec) |
| 102 | { |
| 103 | // Create path to the unique temporary file |
| 104 | QString tempFileName = getTempFileName(); |
| 105 | if (tempFileName.isEmpty()) |
| 106 | { |
| 107 | qDebug() << __FUNCTION__ << |
| 108 | "Error - failed to create unique name for temp file"; |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | TempFileHandler handler(tempFileName); |
| 113 | |
| 114 | // Write information to the temporary file |
| 115 | if (false == appendToFile(tempFileName, content, codec)) { return false; } |
| 116 | |
| 117 | // Remove "old" file if it exists |
| 118 | if (QFile::exists(filePath) && false == QFile::remove(filePath)) |
| 119 | { |
| 120 | qDebug() << __FUNCTION__ << "Error - failed to remove file" << filePath; |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // Copy "new" file (temporary file) to the destination path (replace |
| 125 | // "old" file) |
| 126 | if (false == QFile::copy(tempFileName, filePath)) |
| 127 | { |
| 128 | qDebug() << __FUNCTION__ << |
| 129 | "Error - failed to copy temp file to" << filePath; |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | // Write csv data to IO Device |
| 137 | // @input: |