Exemplo n.º 1
0
bool ConvertLegacyProjectFile(wxFileName filename)
{
   wxTextFile f;
   XMLFileWriter xmlFile;
   int index = 0;
   wxString backupName;

   do {
      index++;
      fflush(stdout);
      backupName = filename.GetPath() + wxFILE_SEP_PATH + filename.GetName() +
         wxT("_bak") + wxString::Format(wxT("%d"), index) + wxT(".") + filename.GetExt();
   } while(::wxFileExists(backupName));

   // This will move the original file out of the way, but 
   // move it back if we exit from this function early.
   AutoRollbackRenamer renamer(filename.GetFullPath(), backupName);
   if (!renamer.RenameSucceeded())
      return false;

   f.Open(backupName);
   if (!f.IsOpened())
      return false;

   wxString name = filename.GetFullPath();

   try
   {
      xmlFile.Open(name, wxT("wb"));
   }
   catch (XMLFileWriterException* pException)
   {
      delete pException;
      return false;
   }

   renamer.SetNewFile(xmlFile.fp());

   try
   {
      xmlFile.Write(wxT("<?xml version=\"1.0\"?>\n"));

      wxString label;
      wxString value;

      if (f.GetFirstLine() != wxT("AudacityProject"))
         return false;
      if (f.GetNextLine() != wxT("Version"))
         return false;
      if (f.GetNextLine() != wxT("0.95"))
         return false;
      if (f.GetNextLine() != wxT("projName"))
         return false;

      xmlFile.StartTag(wxT("audacityproject"));
      xmlFile.WriteAttr(wxT("projname"), f.GetNextLine());
      xmlFile.WriteAttr(wxT("version"), wxT("1.1.0"));
      xmlFile.WriteAttr(wxT("audacityversion"),AUDACITY_VERSION_STRING);

      label = f.GetNextLine();
      while (label != wxT("BeginTracks")) {
         xmlFile.WriteAttr(label, f.GetNextLine());
         label = f.GetNextLine();
      }

      label = f.GetNextLine();
      while (label != wxT("EndTracks")) {
         bool success = ConvertLegacyTrack(&f, xmlFile);
         if (!success)
            return false;
         label = f.GetNextLine();
      }

      xmlFile.EndTag(wxT("audacityproject"));
      xmlFile.Close();
   }
   catch (XMLFileWriterException* pException)
   {
      // Error writing XML file (e.g. disk full)
      delete pException;
      return false;
   }

   renamer.Finished();

   ::wxMessageBox(wxString::Format(_("Converted a 1.0 project file to the new format.\nThe old file has been saved as '%s'"), backupName.c_str()),
                  _("Opening Audacity Project"));

   return true;
}