Audacity  2.3.1
Public Member Functions | Private Member Functions | Private Attributes | List of all members
AutoSaveFile Class Referencefinal

a class wrapping reading and writing of arbitrary data in text or binary format to a file. More...

#include <AutoRecovery.h>

Inheritance diagram for AutoSaveFile:
XMLWriter

Public Member Functions

 AutoSaveFile (size_t allocSize=1024 *1024)
 
virtual ~AutoSaveFile ()
 
void StartTag (const wxString &name) override
 
void EndTag (const wxString &name) override
 
void WriteAttr (const wxString &name, const wxString &value) override
 
void WriteAttr (const wxString &name, const wxChar *value) override
 
void WriteAttr (const wxString &name, int value) override
 
void WriteAttr (const wxString &name, bool value) override
 
void WriteAttr (const wxString &name, long value) override
 
void WriteAttr (const wxString &name, long long value) override
 
void WriteAttr (const wxString &name, size_t value) override
 
void WriteAttr (const wxString &name, float value, int digits=-1) override
 
void WriteAttr (const wxString &name, double value, int digits=-1) override
 
void WriteData (const wxString &value) override
 
void Write (const wxString &data) override
 
void WriteSubTree (const AutoSaveFile &value)
 
bool Write (wxFFile &file) const
 
bool Append (wxFFile &file) const
 
bool IsEmpty () const
 
bool Decode (const wxString &fileName)
 
- Public Member Functions inherited from XMLWriter
 XMLWriter ()
 
virtual ~XMLWriter ()
 
virtual void WriteSubTree (const wxString &value)
 
wxString XMLEsc (const wxString &s)
 

Private Member Functions

void WriteName (const wxString &name)
 
void CheckSpace (wxMemoryOutputStream &buf)
 

Private Attributes

wxMemoryOutputStream mBuffer
 
wxMemoryOutputStream mDict
 
NameMap mNames
 
size_t mAllocSize
 

Additional Inherited Members

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

Detailed Description

a class wrapping reading and writing of arbitrary data in text or binary format to a file.

Definition at line 76 of file AutoRecovery.h.

Constructor & Destructor Documentation

AutoSaveFile::AutoSaveFile ( size_t  allocSize = 1024 * 1024)

Definition at line 484 of file AutoRecovery.cpp.

References mAllocSize.

485 {
486  mAllocSize = allocSize;
487 }
size_t mAllocSize
Definition: AutoRecovery.h:118
AutoSaveFile::~AutoSaveFile ( )
virtual

Definition at line 489 of file AutoRecovery.cpp.

490 {
491 }

Member Function Documentation

bool AutoSaveFile::Append ( wxFFile &  file) const

Definition at line 623 of file AutoRecovery.cpp.

References mBuffer, and mDict.

Referenced by AudacityProject::OnAudioIONewBlockFiles(), and Write().

624 {
625  wxStreamBuffer *buf = mDict.GetOutputStreamBuffer();
626 
627  bool success = file.Write(buf->GetBufferStart(), buf->GetIntPosition()) == buf->GetIntPosition();
628  if (success)
629  {
630  buf = mBuffer.GetOutputStreamBuffer();
631  success = file.Write(buf->GetBufferStart(), buf->GetIntPosition()) == buf->GetIntPosition();
632  }
633 
634  return success;
635 }
wxMemoryOutputStream mDict
Definition: AutoRecovery.h:116
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::CheckSpace ( wxMemoryOutputStream &  buf)
private

Definition at line 637 of file AutoRecovery.cpp.

References mAllocSize.

Referenced by WriteName().

638 {
639  wxStreamBuffer *buf = os.GetOutputStreamBuffer();
640  size_t left = buf->GetBytesLeft();
641  if (left == 0)
642  {
643  size_t origPos = buf->GetIntPosition();
644  ArrayOf<char> temp{ mAllocSize };
645  buf->Write(temp.get(), mAllocSize);
646  buf->SetIntPosition(origPos);
647  }
648 }
size_t mAllocSize
Definition: AutoRecovery.h:118
bool AutoSaveFile::Decode ( const wxString &  fileName)

Definition at line 681 of file AutoRecovery.cpp.

References _(), AutoSaveIdent, FT_Bool, FT_Data, FT_Double, FT_EndTag, FT_Float, FT_Int, FT_Long, FT_LongLong, FT_Name, FT_Pop, FT_Push, FT_Raw, FT_SizeT, FT_StartTag, FT_String, and name.

Referenced by AudacityApp::OnInit(), and AudacityProject::OpenFile().

682 {
683  char ident[sizeof(AutoSaveIdent)];
684  size_t len = strlen(AutoSaveIdent);
685 
686  const wxFileName fn(fileName);
687  const wxString fnPath{fn.GetFullPath()};
688  wxFFile file;
689 
690  if (!file.Open(fnPath, wxT("rb")))
691  {
692  return false;
693  }
694 
695  if (file.Read(&ident, len) != len || strncmp(ident, AutoSaveIdent, len) != 0)
696  {
697  // It could be that the file has already been decoded or that it is one
698  // from 2.1.0 or earlier. In the latter case, we need to ensure the
699  // closing </project> tag is preset.
700 
701  // Close the file so we can reopen it in read/write mode
702  file.Close();
703 
704  // Add </project> tag, if necessary
705  if (!file.Open(fnPath, wxT("r+b")))
706  {
707  // Really shouldn't happen, but let the caller deal with it
708  return false;
709  }
710 
711  // Read the last 16 bytes of the file and check if they contain
712  // "</project>" somewhere.
713  const int bufsize = 16;
714  char buf[bufsize + 1];
715 
716  // FIXME: TRAP_ERR AutoSaveFile::Decode reports OK even when wxFFile errors.
717  // Might be incompletely written file, but not clear that is OK to be
718  // silent about.
719  if (file.SeekEnd(-bufsize))
720  {
721  if (file.Read(buf, bufsize) == bufsize)
722  {
723  buf[bufsize] = 0;
724  if (strstr(buf, "</project>") == 0)
725  {
726  // End of file does not contain closing </project> tag, so add it
727  if (file.Seek(0, wxFromEnd))
728  {
729  strcpy(buf, "</project>\n");
730  file.Write(buf, strlen(buf));
731  }
732  }
733  }
734  }
735 
736  file.Close();
737 
738  return true;
739  }
740 
741  len = file.Length() - len;
742  using Chars = ArrayOf < char >;
743  using WxChars = ArrayOf < wxChar >;
744  Chars buf{ len };
745  if (file.Read(buf.get(), len) != len)
746  {
747  return false;
748  }
749 
750  wxMemoryInputStream in(buf.get(), len);
751 
752  file.Close();
753 
754  // JKC: ANSWER-ME: Is the try catch actually doing anything?
755  // If it is useful, why are we not using it everywhere?
756  // If it isn't useful, why are we doing it here?
757  // PRL: Yes, now we are doing GuardedCall everywhere that XMLFileWriter is
758  // used.
759  return GuardedCall< bool >( [&] {
760  XMLFileWriter out{ fileName, _("Error Decoding File") };
761 
762  IdMap mIds;
763  std::vector<IdMap> mIdStack;
764 
765  mIds.clear();
766 
767  while ( !in.Eof() )
768  {
769  short id;
770 
771  switch (in.GetC())
772  {
773  case FT_Push:
774  {
775  mIdStack.push_back(mIds);
776  mIds.clear();
777  }
778  break;
779 
780  case FT_Pop:
781  {
782  mIds = mIdStack.back();
783  mIdStack.pop_back();
784  }
785  break;
786 
787  case FT_Name:
788  {
789  short len;
790 
791  in.Read(&id, sizeof(id));
792  in.Read(&len, sizeof(len));
793  WxChars name{ len / sizeof(wxChar) };
794  in.Read(name.get(), len);
795 
796  mIds[id] = wxString(name.get(), len / sizeof(wxChar));
797  }
798  break;
799 
800  case FT_StartTag:
801  {
802  in.Read(&id, sizeof(id));
803 
804  out.StartTag(mIds[id]);
805  }
806  break;
807 
808  case FT_EndTag:
809  {
810  in.Read(&id, sizeof(id));
811 
812  out.EndTag(mIds[id]);
813  }
814  break;
815 
816  case FT_String:
817  {
818  int len;
819 
820  in.Read(&id, sizeof(id));
821  in.Read(&len, sizeof(len));
822  WxChars val{ len / sizeof(wxChar) };
823  in.Read(val.get(), len);
824 
825  out.WriteAttr(mIds[id], wxString(val.get(), len / sizeof(wxChar)));
826  }
827  break;
828 
829  case FT_Float:
830  {
831  float val;
832  int dig;
833 
834  in.Read(&id, sizeof(id));
835  in.Read(&val, sizeof(val));
836  in.Read(&dig, sizeof(dig));
837 
838  out.WriteAttr(mIds[id], val, dig);
839  }
840  break;
841 
842  case FT_Double:
843  {
844  double val;
845  int dig;
846 
847  in.Read(&id, sizeof(id));
848  in.Read(&val, sizeof(val));
849  in.Read(&dig, sizeof(dig));
850 
851  out.WriteAttr(mIds[id], val, dig);
852  }
853  break;
854 
855  case FT_Int:
856  {
857  int val;
858 
859  in.Read(&id, sizeof(id));
860  in.Read(&val, sizeof(val));
861 
862  out.WriteAttr(mIds[id], val);
863  }
864  break;
865 
866  case FT_Bool:
867  {
868  bool val;
869 
870  in.Read(&id, sizeof(id));
871  in.Read(&val, sizeof(val));
872 
873  out.WriteAttr(mIds[id], val);
874  }
875  break;
876 
877  case FT_Long:
878  {
879  long val;
880 
881  in.Read(&id, sizeof(id));
882  in.Read(&val, sizeof(val));
883 
884  out.WriteAttr(mIds[id], val);
885  }
886  break;
887 
888  case FT_LongLong:
889  {
890  long long val;
891 
892  in.Read(&id, sizeof(id));
893  in.Read(&val, sizeof(val));
894 
895  out.WriteAttr(mIds[id], val);
896  }
897  break;
898 
899  case FT_SizeT:
900  {
901  size_t val;
902 
903  in.Read(&id, sizeof(id));
904  in.Read(&val, sizeof(val));
905 
906  out.WriteAttr(mIds[id], val);
907  }
908  break;
909 
910  case FT_Data:
911  {
912  int len;
913 
914  in.Read(&len, sizeof(len));
915  WxChars val{ len / sizeof(wxChar) };
916  in.Read(val.get(), len);
917 
918  out.WriteData(wxString(val.get(), len / sizeof(wxChar)));
919  }
920  break;
921 
922  case FT_Raw:
923  {
924  int len;
925 
926  in.Read(&len, sizeof(len));
927  WxChars val{ len / sizeof(wxChar) };
928  in.Read(val.get(), len);
929 
930  out.Write(wxString(val.get(), len / sizeof(wxChar)));
931  }
932  break;
933 
934  default:
935  wxASSERT(true);
936  break;
937  }
938  }
939 
940  out.Commit();
941 
942  return true;
943  } );
944 }
#define AutoSaveIdent
Definition: AutoRecovery.h:70
Wrapper to output XML data to files.
Definition: XMLWriter.h:74
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
const wxChar * name
Definition: Distortion.cpp:94
std::unordered_map< short, wxString > IdMap
Definition: AutoRecovery.h:73
void AutoSaveFile::EndTag ( const wxString &  name)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 499 of file AutoRecovery.cpp.

References FT_EndTag, mBuffer, and WriteName().

Referenced by AudioIO::FillBuffers().

500 {
501  mBuffer.PutC(FT_EndTag);
502  WriteName(name);
503 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
bool AutoSaveFile::IsEmpty ( ) const

Definition at line 676 of file AutoRecovery.cpp.

References mBuffer.

Referenced by AudioIO::FillBuffers().

677 {
678  return mBuffer.GetLength() == 0;
679 }
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::StartTag ( const wxString &  name)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 493 of file AutoRecovery.cpp.

References FT_StartTag, mBuffer, and WriteName().

Referenced by AudioIO::FillBuffers().

494 {
495  mBuffer.PutC(FT_StartTag);
496  WriteName(name);
497 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::Write ( const wxString &  data)
overridevirtual

Implements XMLWriter.

Definition at line 589 of file AutoRecovery.cpp.

References FT_Raw, and mBuffer.

Referenced by AudacityProject::AutoSave().

590 {
591  mBuffer.PutC(FT_Raw);
592 
593  int len = value.Length() * sizeof(wxChar);
594 
595  mBuffer.Write(&len, sizeof(len));
596  mBuffer.Write(value.wx_str(), len);
597 }
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
bool AutoSaveFile::Write ( wxFFile &  file) const

Definition at line 612 of file AutoRecovery.cpp.

References Append(), and AutoSaveIdent.

613 {
614  bool success = file.Write(AutoSaveIdent, strlen(AutoSaveIdent)) == strlen(AutoSaveIdent);
615  if (success)
616  {
617  success = Append(file);
618  }
619 
620  return success;
621 }
#define AutoSaveIdent
Definition: AutoRecovery.h:70
bool Append(wxFFile &file) const
void AutoSaveFile::WriteAttr ( const wxString &  name,
const wxString &  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 510 of file AutoRecovery.cpp.

References FT_String, mBuffer, and WriteName().

Referenced by AudioIO::FillBuffers(), and WriteAttr().

511 {
512  mBuffer.PutC(FT_String);
513  WriteName(name);
514 
515  int len = value.Length() * sizeof(wxChar);
516 
517  mBuffer.Write(&len, sizeof(len));
518  mBuffer.Write(value.wx_str(), len);
519 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteAttr ( const wxString &  name,
const wxChar *  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 505 of file AutoRecovery.cpp.

References WriteAttr().

506 {
507  WriteAttr(name, wxString(value));
508 }
void WriteAttr(const wxString &name, const wxString &value) override
const wxChar * name
Definition: Distortion.cpp:94
void AutoSaveFile::WriteAttr ( const wxString &  name,
int  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 521 of file AutoRecovery.cpp.

References FT_Int, mBuffer, and WriteName().

522 {
523  mBuffer.PutC(FT_Int);
524  WriteName(name);
525 
526  mBuffer.Write(&value, sizeof(value));
527 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteAttr ( const wxString &  name,
bool  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 529 of file AutoRecovery.cpp.

References FT_Bool, mBuffer, and WriteName().

530 {
531  mBuffer.PutC(FT_Bool);
532  WriteName(name);
533 
534  mBuffer.Write(&value, sizeof(value));
535 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteAttr ( const wxString &  name,
long  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 537 of file AutoRecovery.cpp.

References FT_Long, mBuffer, and WriteName().

538 {
539  mBuffer.PutC(FT_Long);
540  WriteName(name);
541 
542  mBuffer.Write(&value, sizeof(value));
543 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteAttr ( const wxString &  name,
long long  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 545 of file AutoRecovery.cpp.

References FT_LongLong, mBuffer, and WriteName().

546 {
547  mBuffer.PutC(FT_LongLong);
548  WriteName(name);
549 
550  mBuffer.Write(&value, sizeof(value));
551 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteAttr ( const wxString &  name,
size_t  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 553 of file AutoRecovery.cpp.

References FT_SizeT, mBuffer, and WriteName().

554 {
555  mBuffer.PutC(FT_SizeT);
556  WriteName(name);
557 
558  mBuffer.Write(&value, sizeof(value));
559 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteAttr ( const wxString &  name,
float  value,
int  digits = -1 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 561 of file AutoRecovery.cpp.

References FT_Float, mBuffer, and WriteName().

562 {
563  mBuffer.PutC(FT_Float);
564  WriteName(name);
565 
566  mBuffer.Write(&value, sizeof(value));
567  mBuffer.Write(&digits, sizeof(digits));
568 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteAttr ( const wxString &  name,
double  value,
int  digits = -1 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 570 of file AutoRecovery.cpp.

References FT_Double, mBuffer, and WriteName().

571 {
572  mBuffer.PutC(FT_Double);
573  WriteName(name);
574 
575  mBuffer.Write(&value, sizeof(value));
576  mBuffer.Write(&digits, sizeof(digits));
577 }
void WriteName(const wxString &name)
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteData ( const wxString &  value)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 579 of file AutoRecovery.cpp.

References FT_Data, and mBuffer.

580 {
581  mBuffer.PutC(FT_Data);
582 
583  int len = value.Length() * sizeof(wxChar);
584 
585  mBuffer.Write(&len, sizeof(len));
586  mBuffer.Write(value.wx_str(), len);
587 }
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteName ( const wxString &  name)
private

Definition at line 650 of file AutoRecovery.cpp.

References CheckSpace(), FT_Name, mBuffer, mDict, mNames, and name.

Referenced by EndTag(), StartTag(), and WriteAttr().

651 {
652  wxASSERT(name.Length() * sizeof(wxChar) <= SHRT_MAX);
653  short len = name.Length() * sizeof(wxChar);
654  short id;
655 
656  if (mNames.count(name))
657  {
658  id = mNames[name];
659  }
660  else
661  {
662  id = mNames.size();
663  mNames[name] = id;
664 
665  CheckSpace(mDict);
666  mDict.PutC(FT_Name);
667  mDict.Write(&id, sizeof(id));
668  mDict.Write(&len, sizeof(len));
669  mDict.Write(name.wx_str(), len);
670  }
671 
673  mBuffer.Write(&id, sizeof(id));
674 }
wxMemoryOutputStream mDict
Definition: AutoRecovery.h:116
void CheckSpace(wxMemoryOutputStream &buf)
NameMap mNames
Definition: AutoRecovery.h:117
const wxChar * name
Definition: Distortion.cpp:94
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115
void AutoSaveFile::WriteSubTree ( const AutoSaveFile value)

Definition at line 599 of file AutoRecovery.cpp.

References FT_Pop, FT_Push, mBuffer, and mDict.

Referenced by AudioIO::FillBuffers().

600 {
601  mBuffer.PutC(FT_Push);
602 
603  wxStreamBuffer *buf = value.mDict.GetOutputStreamBuffer();
604  mBuffer.Write(buf->GetBufferStart(), buf->GetIntPosition());
605 
606  buf = value.mBuffer.GetOutputStreamBuffer();
607  mBuffer.Write(buf->GetBufferStart(), buf->GetIntPosition());
608 
609  mBuffer.PutC(FT_Pop);
610 }
wxMemoryOutputStream mDict
Definition: AutoRecovery.h:116
wxMemoryOutputStream mBuffer
Definition: AutoRecovery.h:115

Member Data Documentation

size_t AutoSaveFile::mAllocSize
private

Definition at line 118 of file AutoRecovery.h.

Referenced by AutoSaveFile(), and CheckSpace().

wxMemoryOutputStream AutoSaveFile::mBuffer
private
wxMemoryOutputStream AutoSaveFile::mDict
private

Definition at line 116 of file AutoRecovery.h.

Referenced by Append(), WriteName(), and WriteSubTree().

NameMap AutoSaveFile::mNames
private

Definition at line 117 of file AutoRecovery.h.

Referenced by WriteName().


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