Update of /cvsroot/audacity/audacity-src/src/xml
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv14899/xml

Modified Files:
        XMLWriter.cpp XMLWriter.h 
Log Message:
* XMLFileWriter now throws an exception if writing the .xml file fails
* Add error checking (exception handling) to most places where XML files are 
written
* If the new .aup file was successfully saved, delete the old .aup.bak file in 
order not to confuse users
* Never open .aup.bak files, show an informational message instead

Index: XMLWriter.h
===================================================================
RCS file: /cvsroot/audacity/audacity-src/src/xml/XMLWriter.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- XMLWriter.h 8 Jul 2008 19:19:19 -0000       1.3
+++ XMLWriter.h 1 May 2009 18:26:33 -0000       1.4
@@ -75,9 +75,17 @@
    XMLFileWriter();
    virtual ~XMLFileWriter();
 
-   bool Open(const wxString &name, const wxString &mode);
-   bool Close();
+   /// Open the file. Might throw XMLFileWriterException.
+   void Open(const wxString &name, const wxString &mode);
+   
+   /// Close file. Might throw XMLFileWriterException.
+   void Close();
 
+   /// Close file without automatically ending tags.
+   /// Might throw XMLFileWriterException.
+   void CloseWithoutEndingTags(); // for auto-save files
+
+   /// Write to file. Might throw XMLFileWriterException.
    void Write(const wxString &data);
 
  private:
@@ -85,6 +93,19 @@
 };
 
 ///
+/// Exception thrown by various XMLFileWriter methods
+///
+class XMLFileWriterException
+{
+public:
+   XMLFileWriterException(const wxString& message) { mMessage = message; }
+   wxString GetMessage() const { return mMessage; }
+
+protected:
+   wxString mMessage;
+};
+
+///
 /// XMLStringWriter
 ///
 class XMLStringWriter:public wxString, public XMLWriter {

Index: XMLWriter.cpp
===================================================================
RCS file: /cvsroot/audacity/audacity-src/src/xml/XMLWriter.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- XMLWriter.cpp       8 Jul 2008 19:19:19 -0000       1.3
+++ XMLWriter.cpp       1 May 2009 18:26:33 -0000       1.4
@@ -256,23 +256,45 @@
    }
 }
 
-bool XMLFileWriter::Open(const wxString &name, const wxString &mode)
+void XMLFileWriter::Open(const wxString &name, const wxString &mode)
 {
-   return wxFFile::Open(name, mode);
+   if (!wxFFile::Open(name, mode))
+      throw new XMLFileWriterException(_("Error opening file"));
 }
 
-bool XMLFileWriter::Close()
+void XMLFileWriter::Close()
 {
    while (mTagstack.GetCount()) {
       EndTag(mTagstack[0]);
    }
 
-   return wxFFile::Close();
+   CloseWithoutEndingTags();
+}
+
+void XMLFileWriter::CloseWithoutEndingTags()
+{
+   // Before closing, we first flush it, because if Flush() fails because of a
+   // "disk full" condition, we can still at least try to close the file.
+   if (!wxFFile::Flush())
+   {
+      wxFFile::Close();
+      throw new XMLFileWriterException(_("Error flushing file"));
+   }
+
+   // Note that this should never fail if flushing worked.
+   if (!wxFFile::Close())
+      throw new XMLFileWriterException(_("Error closing file"));
 }
 
 void XMLFileWriter::Write(const wxString &data)
 {
-   wxFFile::Write(data);
+   if (!wxFFile::Write(data, wxConvUTF8))
+   {
+      // When writing fails, we try to close the file before throwing the
+      // exception, so it can at least be deleted.
+      wxFFile::Close();
+      throw new XMLFileWriterException(_("Error writing to file"));
+   }
 }
 
 ///


------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations 
Conference from O'Reilly Media. Velocity features a full day of 
expert-led, hands-on workshops and two days of sessions from industry 
leaders in dedicated Performance & Operations tracks. Use code vel09scf 
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
Audacity-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/audacity-cvs

Reply via email to