Audacity  2.2.2
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 483 of file AutoRecovery.cpp.

References mAllocSize.

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

Definition at line 488 of file AutoRecovery.cpp.

489 {
490 }

Member Function Documentation

bool AutoSaveFile::Append ( wxFFile &  file) const

Definition at line 622 of file AutoRecovery.cpp.

References mBuffer, and mDict.

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

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

Definition at line 636 of file AutoRecovery.cpp.

References mAllocSize.

Referenced by WriteName().

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

Definition at line 680 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, ident(), and name.

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

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

References FT_EndTag, mBuffer, and WriteName().

Referenced by AudioIO::FillBuffers().

499 {
500  mBuffer.PutC(FT_EndTag);
501  WriteName(name);
502 }
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 675 of file AutoRecovery.cpp.

References mBuffer.

Referenced by AudioIO::FillBuffers().

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

Reimplemented from XMLWriter.

Definition at line 492 of file AutoRecovery.cpp.

References FT_StartTag, mBuffer, and WriteName().

Referenced by AudioIO::FillBuffers().

493 {
494  mBuffer.PutC(FT_StartTag);
495  WriteName(name);
496 }
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 588 of file AutoRecovery.cpp.

References FT_Raw, and mBuffer.

Referenced by AudacityProject::AutoSave().

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

Definition at line 611 of file AutoRecovery.cpp.

References Append(), and AutoSaveIdent.

612 {
613  bool success = file.Write(AutoSaveIdent, strlen(AutoSaveIdent)) == strlen(AutoSaveIdent);
614  if (success)
615  {
616  success = Append(file);
617  }
618 
619  return success;
620 }
#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 509 of file AutoRecovery.cpp.

References FT_String, mBuffer, and WriteName().

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

510 {
511  mBuffer.PutC(FT_String);
512  WriteName(name);
513 
514  int len = value.Length() * sizeof(wxChar);
515 
516  mBuffer.Write(&len, sizeof(len));
517  mBuffer.Write(value.wx_str(), len);
518 }
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 504 of file AutoRecovery.cpp.

References WriteAttr().

505 {
506  WriteAttr(name, wxString(value));
507 }
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 520 of file AutoRecovery.cpp.

References FT_Int, mBuffer, and WriteName().

521 {
522  mBuffer.PutC(FT_Int);
523  WriteName(name);
524 
525  mBuffer.Write(&value, sizeof(value));
526 }
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 528 of file AutoRecovery.cpp.

References FT_Bool, mBuffer, and WriteName().

529 {
530  mBuffer.PutC(FT_Bool);
531  WriteName(name);
532 
533  mBuffer.Write(&value, sizeof(value));
534 }
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 536 of file AutoRecovery.cpp.

References FT_Long, mBuffer, and WriteName().

537 {
538  mBuffer.PutC(FT_Long);
539  WriteName(name);
540 
541  mBuffer.Write(&value, sizeof(value));
542 }
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 544 of file AutoRecovery.cpp.

References FT_LongLong, mBuffer, and WriteName().

545 {
546  mBuffer.PutC(FT_LongLong);
547  WriteName(name);
548 
549  mBuffer.Write(&value, sizeof(value));
550 }
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 552 of file AutoRecovery.cpp.

References FT_SizeT, mBuffer, and WriteName().

553 {
554  mBuffer.PutC(FT_SizeT);
555  WriteName(name);
556 
557  mBuffer.Write(&value, sizeof(value));
558 }
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 560 of file AutoRecovery.cpp.

References FT_Float, mBuffer, and WriteName().

561 {
562  mBuffer.PutC(FT_Float);
563  WriteName(name);
564 
565  mBuffer.Write(&value, sizeof(value));
566  mBuffer.Write(&digits, sizeof(digits));
567 }
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 569 of file AutoRecovery.cpp.

References FT_Double, mBuffer, and WriteName().

570 {
571  mBuffer.PutC(FT_Double);
572  WriteName(name);
573 
574  mBuffer.Write(&value, sizeof(value));
575  mBuffer.Write(&digits, sizeof(digits));
576 }
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 578 of file AutoRecovery.cpp.

References FT_Data, and mBuffer.

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

Definition at line 649 of file AutoRecovery.cpp.

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

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

650 {
651  wxASSERT(name.Length() * sizeof(wxChar) <= SHRT_MAX);
652  short len = name.Length() * sizeof(wxChar);
653  short id;
654 
655  if (mNames.count(name))
656  {
657  id = mNames[name];
658  }
659  else
660  {
661  id = mNames.size();
662  mNames[name] = id;
663 
664  CheckSpace(mDict);
665  mDict.PutC(FT_Name);
666  mDict.Write(&id, sizeof(id));
667  mDict.Write(&len, sizeof(len));
668  mDict.Write(name.wx_str(), len);
669  }
670 
672  mBuffer.Write(&id, sizeof(id));
673 }
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 598 of file AutoRecovery.cpp.

References FT_Pop, FT_Push, mBuffer, and mDict.

Referenced by AudioIO::FillBuffers().

599 {
600  mBuffer.PutC(FT_Push);
601 
602  wxStreamBuffer *buf = value.mDict.GetOutputStreamBuffer();
603  mBuffer.Write(buf->GetBufferStart(), buf->GetIntPosition());
604 
605  buf = value.mBuffer.GetOutputStreamBuffer();
606  mBuffer.Write(buf->GetBufferStart(), buf->GetIntPosition());
607 
608  mBuffer.PutC(FT_Pop);
609 }
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: