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
| 88 | // @output: |
| 89 | // - bool - True if file was overwritten with new data, otherwise False |
| 90 | bool WriterPrivate::overwriteFile( |
| 91 | const QString& filePath, |
| 92 | ContentIterator& content, |
| 93 | const QStringConverter::Encoding codec) |
| 94 | { |
| 95 | // Create path to the unique temporary file |
| 96 | const auto tempFileName = getTempFileName(); |
| 97 | if (tempFileName.isEmpty()) { |
| 98 | qDebug() << __FUNCTION__ << |
| 99 | "Error - failed to create unique name for temp file"; |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | TempFileHandler handler(tempFileName); |
| 104 | |
| 105 | // Write information to the temporary file |
| 106 | if (!appendToFile(tempFileName, content, codec)) { return false; } |
| 107 | |
| 108 | // Remove "old" file if it exists |
| 109 | if (QFile::exists(filePath) && !QFile::remove(filePath)) { |
| 110 | qDebug() << __FUNCTION__ << "Error - failed to remove file" << filePath; |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // Copy "new" file (temporary file) to the destination path (replace |
| 115 | // "old" file) |
| 116 | if (!QFile::copy(tempFileName, filePath)) { |
| 117 | qDebug() << __FUNCTION__ << |
| 118 | "Error - failed to copy temp file to" << filePath; |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // Write csv data to IO Device |
| 126 | // @input: |