Audacity 3.2.0
Public Member Functions | Private Member Functions | Private Attributes | List of all members
XMLFileWriter Class Referencefinal

Wrapper to output XML data to files. More...

#include <XMLWriter.h>

Inheritance diagram for XMLFileWriter:
[legend]
Collaboration diagram for XMLFileWriter:
[legend]

Public Member Functions

 XMLFileWriter (const FilePath &outputPath, const TranslatableString &caption, bool keepBackup=false)
 
virtual ~XMLFileWriter ()
 
void Commit ()
 
void PreCommit ()
 Does the part of Commit that might fail because of exhaustion of space. More...
 
void PostCommit ()
 
void Write (const wxString &data) override
 Write to file. Might throw. More...
 
FilePath GetBackupName () const
 
- Public Member Functions inherited from XMLWriter
 XMLWriter ()
 
virtual ~XMLWriter ()
 
virtual void StartTag (const wxString &name)
 
virtual void EndTag (const wxString &name)
 
void WriteAttr (const wxString &name, const Identifier &value)
 
virtual void WriteAttr (const wxString &name, const wxString &value)
 
virtual void WriteAttr (const wxString &name, const wxChar *value)
 
virtual void WriteAttr (const wxString &name, int value)
 
virtual void WriteAttr (const wxString &name, bool value)
 
virtual void WriteAttr (const wxString &name, long value)
 
virtual void WriteAttr (const wxString &name, long long value)
 
virtual void WriteAttr (const wxString &name, size_t value)
 
virtual void WriteAttr (const wxString &name, float value, int digits=-1)
 
virtual void WriteAttr (const wxString &name, double value, int digits=-1)
 
virtual void WriteData (const wxString &value)
 
virtual void WriteSubTree (const wxString &value)
 
virtual void Write (const wxString &data)=0
 

Private Member Functions

void ThrowException (const wxFileName &fileName, const TranslatableString &caption)
 
void CloseWithoutEndingTags ()
 

Private Attributes

const FilePath mOutputPath
 
const TranslatableString mCaption
 
FilePath mBackupName
 
const bool mKeepBackup
 
wxFFile mBackupFile
 
bool mCommitted { false }
 

Additional Inherited Members

- Protected Attributes inherited from XMLWriter
bool mInTag
 
int mDepth
 
wxArrayString mTagstack
 
std::vector< int > mHasKids
 

Detailed Description

Wrapper to output XML data to files.

XMLFileWriter This writes to a provisional file, and replaces the previously existing contents by a file rename in Commit() only after all writes succeed. The original contents may also be retained at a backup path name, as directed by the optional constructor argument. If it is destroyed before Commit(), then the provisional file is removed. If the construction and all operations are inside a GuardedCall or event handler, then the default delayed handler action in case of exceptions will notify the user of problems.

Definition at line 84 of file XMLWriter.h.

Constructor & Destructor Documentation

◆ XMLFileWriter()

XMLFileWriter::XMLFileWriter ( const FilePath outputPath,
const TranslatableString caption,
bool  keepBackup = false 
)

The caption is for message boxes to show in case of errors. Might throw.

XMLFileWriter class

Definition at line 297 of file XMLWriter.cpp.

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++;
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() )
326 }
327}
wxT("CloseDown"))
wxFFile mBackupFile
Definition: XMLWriter.h:131
FilePath mBackupName
Definition: XMLWriter.h:128
const TranslatableString mCaption
Definition: XMLWriter.h:127
const bool mKeepBackup
Definition: XMLWriter.h:129
void ThrowException(const wxFileName &fileName, const TranslatableString &caption)
Definition: XMLWriter.h:116
const FilePath mOutputPath
Definition: XMLWriter.h:126

References mBackupFile, mBackupName, mCaption, mKeepBackup, mOutputPath, ThrowException(), and wxT().

Here is the call graph for this function:

◆ ~XMLFileWriter()

XMLFileWriter::~XMLFileWriter ( )
virtual

Definition at line 330 of file XMLWriter.cpp.

331{
332 // Don't let a destructor throw!
333 GuardedCall( [&] {
334 if (!mCommitted) {
335 auto fileName = GetName();
336 if ( IsOpened() )
338 ::wxRemoveFile( fileName );
339 }
340 } );
341}
R GuardedCall(const F1 &body, const F2 &handler=F2::Default(), F3 delayedHandler=DefaultDelayedHandlerAction) noexcept(noexcept(handler(std::declval< AudacityException * >())) &&noexcept(handler(nullptr)) &&noexcept(std::function< void(AudacityException *)>{std::move(delayedHandler)}))
Execute some code on any thread; catch any AudacityException; enqueue error report on the main thread...
bool mCommitted
Definition: XMLWriter.h:133
void CloseWithoutEndingTags()
Definition: XMLWriter.cpp:387

References CloseWithoutEndingTags(), GuardedCall(), and mCommitted.

Here is the call graph for this function:

Member Function Documentation

◆ CloseWithoutEndingTags()

void XMLFileWriter::CloseWithoutEndingTags ( )
private

Close file without automatically ending tags. Might throw.

Definition at line 387 of file XMLWriter.cpp.

389{
390 // Before closing, we first flush it, because if Flush() fails because of a
391 // "disk full" condition, we can still at least try to close the file.
392 if (!wxFFile::Flush())
393 {
394 wxFFile::Close();
395 ThrowException( GetName(), mCaption );
396 }
397
398 // Note that this should never fail if flushing worked.
399 if (!wxFFile::Close())
400 ThrowException( GetName(), mCaption );
401}

References mCaption, and ThrowException().

Referenced by PreCommit(), and ~XMLFileWriter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Commit()

void XMLFileWriter::Commit ( )

Close all tags and then close the file. Might throw. If not, then create or modify the file at the output path. Composed of two steps, PreCommit() and PostCommit()

Definition at line 343 of file XMLWriter.cpp.

345{
346 PreCommit();
347 PostCommit();
348}
void PostCommit()
Definition: XMLWriter.cpp:360
void PreCommit()
Does the part of Commit that might fail because of exhaustion of space.
Definition: XMLWriter.cpp:350

References PostCommit(), and PreCommit().

Here is the call graph for this function:

◆ GetBackupName()

FilePath XMLFileWriter::GetBackupName ( ) const
inline

Definition at line 112 of file XMLWriter.h.

112{ return mBackupName; }

◆ PostCommit()

void XMLFileWriter::PostCommit ( )

Does other parts of Commit that are not likely to fail for exhaustion of space, but might for other reasons

Definition at line 360 of file XMLWriter.cpp.

362{
363 FilePath tempPath = GetName();
364 if (mKeepBackup) {
365 if (! mBackupFile.Close() ||
366 ! wxRenameFile( mOutputPath, mBackupName ) )
368 }
369 else {
370 if ( wxFileName::FileExists( mOutputPath ) &&
371 ! wxRemoveFile( mOutputPath ) )
373 }
374
375 // Now we have vacated the file at the output path and are committed.
376 // But not completely finished with steps of the commit operation.
377 // If this step fails, we haven't lost the successfully written data,
378 // but just failed to put it in the right place.
379 if (! wxRenameFile( tempPath, mOutputPath ) )
380 throw FileException{
382 };
383
384 mCommitted = true;
385}
wxString FilePath
Definition: Project.h:21
Thrown for failure of file or database operations in deeply nested places.
Definition: FileException.h:19
@ Rename
involves two filenames

References mBackupFile, mBackupName, mCaption, mCommitted, mKeepBackup, mOutputPath, FileException::Rename, and ThrowException().

Referenced by Commit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PreCommit()

void XMLFileWriter::PreCommit ( )

Does the part of Commit that might fail because of exhaustion of space.

Definition at line 350 of file XMLWriter.cpp.

352{
353 while (mTagstack.size()) {
354 EndTag(mTagstack[0]);
355 }
356
358}
virtual void EndTag(const wxString &name)
Definition: XMLWriter.cpp:102
wxArrayString mTagstack
Definition: XMLWriter.h:67

References CloseWithoutEndingTags(), XMLWriter::EndTag(), and XMLWriter::mTagstack.

Referenced by Commit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ThrowException()

void XMLFileWriter::ThrowException ( const wxFileName &  fileName,
const TranslatableString caption 
)
inlineprivate

Definition at line 116 of file XMLWriter.h.

118 {
119 throw FileException{ FileException::Cause::Write, fileName, caption };
120 }
@ Write
most important to detect when storage space is exhausted

References FileException::Write.

Referenced by CloseWithoutEndingTags(), PostCommit(), Write(), and XMLFileWriter().

Here is the caller graph for this function:

◆ Write()

void XMLFileWriter::Write ( const wxString &  data)
overridevirtual

Write to file. Might throw.

Implements XMLWriter.

Definition at line 403 of file XMLWriter.cpp.

405{
406 if (!wxFFile::Write(data, wxConvUTF8) || Error())
407 {
408 // When writing fails, we try to close the file before throwing the
409 // exception, so it can at least be deleted.
410 wxFFile::Close();
411 ThrowException( GetName(), mCaption );
412 }
413}

References Error, mCaption, and ThrowException().

Here is the call graph for this function:

Member Data Documentation

◆ mBackupFile

wxFFile XMLFileWriter::mBackupFile
private

Definition at line 131 of file XMLWriter.h.

Referenced by PostCommit(), and XMLFileWriter().

◆ mBackupName

FilePath XMLFileWriter::mBackupName
private

Definition at line 128 of file XMLWriter.h.

Referenced by PostCommit(), and XMLFileWriter().

◆ mCaption

const TranslatableString XMLFileWriter::mCaption
private

Definition at line 127 of file XMLWriter.h.

Referenced by CloseWithoutEndingTags(), PostCommit(), Write(), and XMLFileWriter().

◆ mCommitted

bool XMLFileWriter::mCommitted { false }
private

Definition at line 133 of file XMLWriter.h.

Referenced by PostCommit(), and ~XMLFileWriter().

◆ mKeepBackup

const bool XMLFileWriter::mKeepBackup
private

Definition at line 129 of file XMLWriter.h.

Referenced by PostCommit(), and XMLFileWriter().

◆ mOutputPath

const FilePath XMLFileWriter::mOutputPath
private

Definition at line 126 of file XMLWriter.h.

Referenced by PostCommit(), and XMLFileWriter().


The documentation for this class was generated from the following files: