XMLFileWriter class
| 295 | /// XMLFileWriter class |
| 296 | /// |
| 297 | XMLFileWriter::XMLFileWriter( |
| 298 | const FilePath &outputPath, const TranslatableString &caption, bool keepBackup ) |
| 299 | : mOutputPath{ outputPath } |
| 300 | , mCaption{ caption } |
| 301 | , mKeepBackup{ keepBackup } |
| 302 | // may throw |
| 303 | { |
| 304 | auto tempPath = wxFileName::CreateTempFileName( outputPath ); |
| 305 | if (!wxFFile::Open(tempPath, wxT("wb")) || !IsOpened()) |
| 306 | ThrowException( outputPath, mCaption ); |
| 307 | |
| 308 | if (mKeepBackup) { |
| 309 | int index = 0; |
| 310 | wxString backupName; |
| 311 | |
| 312 | do { |
| 313 | wxFileName outputFn{ mOutputPath }; |
| 314 | index++; |
| 315 | mBackupName = |
| 316 | outputFn.GetPath() + wxFILE_SEP_PATH + |
| 317 | outputFn.GetName() + wxT("_bak") + |
| 318 | wxString::Format(wxT("%d"), index) + wxT(".") + |
| 319 | outputFn.GetExt(); |
| 320 | } while( ::wxFileExists( mBackupName ) ); |
| 321 | |
| 322 | // Open the backup file to be sure we can write it and reserve it |
| 323 | // until committing |
| 324 | if (! mBackupFile.Open( mBackupName, "wb" ) || ! mBackupFile.IsOpened() ) |
| 325 | ThrowException( mBackupName, mCaption ); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | XMLFileWriter::~XMLFileWriter() |