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

a class used to (de)serialize the project catalog More...

#include <ProjectSerializer.h>

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

Public Member Functions

 ProjectSerializer (size_t allocSize=1024 *1024)
 
virtual ~ProjectSerializer ()
 
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
 
const MemoryStreamGetDict () const
 
const MemoryStreamGetData () const
 
bool IsEmpty () const
 
bool DictChanged () 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
 

Static Public Member Functions

static TranslatableString FailureMessage (const FilePath &filePath)
 
static bool Decode (BufferedStreamReader &in, XMLTagHandler *handler)
 

Private Member Functions

void WriteName (const wxString &name)
 

Private Attributes

MemoryStream mBuffer
 
bool mDictChanged
 

Static Private Attributes

static NameMap mNames
 
static MemoryStream mDict
 

Additional Inherited Members

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

Detailed Description

a class used to (de)serialize the project catalog

Definition at line 36 of file ProjectSerializer.h.

Constructor & Destructor Documentation

◆ ProjectSerializer()

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

Definition at line 350 of file ProjectSerializer.cpp.

351{
352 static std::once_flag flag;
353 std::call_once(flag, []{
354 // Just once per run, store header information in the unique static
355 // dictionary that will be written into each project that is saved.
356 // Store the size of "wxStringCharType" so we can convert during recovery
357 // in case the file is used on a system with a different character size.
358 char size = sizeof(wxStringCharType);
360 mDict.AppendData(&size, 1);
361 });
362
363 mDictChanged = false;
364}
@ FT_CharSize
static std::once_flag flag
void AppendData(const void *data, const size_t length)
void AppendByte(char data)
static MemoryStream mDict

References MemoryStream::AppendByte(), MemoryStream::AppendData(), flag, FT_CharSize, mDict, mDictChanged, and size.

Here is the call graph for this function:

◆ ~ProjectSerializer()

ProjectSerializer::~ProjectSerializer ( )
virtual

Definition at line 366 of file ProjectSerializer.cpp.

367{
368}

Member Function Documentation

◆ Decode()

bool ProjectSerializer::Decode ( BufferedStreamReader in,
XMLTagHandler handler 
)
static

Definition at line 523 of file ProjectSerializer.cpp.

524{
525 if (handler == nullptr)
526 return false;
527
528 XMLTagHandlerAdapter adapter(handler);
529
530 std::vector<char> bytes;
531 IdMap mIds;
532 std::vector<IdMap> mIdStack;
533 char mCharSize = 0;
534
535 mIds.clear();
536
537 struct Error{}; // exception type for short-range try/catch
538 auto Lookup = [&mIds]( UShort id ) -> std::string_view
539 {
540 auto iter = mIds.find( id );
541 if (iter == mIds.end())
542 {
543 throw Error{};
544 }
545
546 return iter->second;
547 };
548
549 int64_t stringsCount = 0;
550 int64_t stringsLength = 0;
551
552 auto ReadString = [&mCharSize, &in, &bytes, &stringsCount, &stringsLength](int len) -> std::string
553 {
554 bytes.reserve( len );
555 auto data = bytes.data();
556 in.Read( data, len );
557
558 stringsCount++;
559 stringsLength += len;
560
561 switch (mCharSize)
562 {
563 case 1:
564 return std::string(bytes.data(), len);
565
566 case 2:
567 return FastStringConvert<char16_t>(bytes.data(), len);
568
569 case 4:
570 return FastStringConvert<char32_t>(bytes.data(), len);
571
572 default:
573 wxASSERT_MSG(false, wxT("Characters size not 1, 2, or 4"));
574 break;
575 }
576
577 return {};
578 };
579
580 try
581 {
582 while (!in.Eof())
583 {
584 UShort id;
585
586 switch (in.GetC())
587 {
588 case FT_Push:
589 {
590 mIdStack.push_back(mIds);
591 mIds.clear();
592 }
593 break;
594
595 case FT_Pop:
596 {
597 mIds = mIdStack.back();
598 mIdStack.pop_back();
599 }
600 break;
601
602 case FT_Name:
603 {
604 id = ReadUShort( in );
605 auto len = ReadUShort( in );
606 mIds[id] = ReadString(len);
607 }
608 break;
609
610 case FT_StartTag:
611 {
612 id = ReadUShort( in );
613
614 adapter.EmitStartTag(Lookup(id));
615 }
616 break;
617
618 case FT_EndTag:
619 {
620 id = ReadUShort( in );
621
622 adapter.EndTag(Lookup(id));
623 }
624 break;
625
626 case FT_String:
627 {
628 id = ReadUShort( in );
629 int len = ReadLength( in );
630
631 adapter.WriteAttr(Lookup(id), ReadString(len));
632 }
633 break;
634
635 case FT_Float:
636 {
637 float val;
638
639 id = ReadUShort( in );
640 in.Read(&val, sizeof(val));
641 /* int dig = */ReadDigits(in);
642
643 adapter.WriteAttr(Lookup(id), val);
644 }
645 break;
646
647 case FT_Double:
648 {
649 double val;
650
651 id = ReadUShort( in );
652 in.Read(&val, sizeof(val));
653 /*int dig = */ReadDigits(in);
654
655 adapter.WriteAttr(Lookup(id), val);
656 }
657 break;
658
659 case FT_Int:
660 {
661 id = ReadUShort( in );
662 int val = ReadInt( in );
663
664 adapter.WriteAttr(Lookup(id), val);
665 }
666 break;
667
668 case FT_Bool:
669 {
670 unsigned char val;
671
672 id = ReadUShort( in );
673 in.Read(&val, 1);
674
675 adapter.WriteAttr(Lookup(id), val);
676 }
677 break;
678
679 case FT_Long:
680 {
681 id = ReadUShort( in );
682 long val = ReadLong( in );
683
684 adapter.WriteAttr(Lookup(id), val);
685 }
686 break;
687
688 case FT_LongLong:
689 {
690 id = ReadUShort( in );
691 long long val = ReadLongLong( in );
692 adapter.WriteAttr(Lookup(id), val);
693 }
694 break;
695
696 case FT_SizeT:
697 {
698 id = ReadUShort( in );
699 size_t val = ReadULong( in );
700
701 adapter.WriteAttr(Lookup(id), val);
702 }
703 break;
704
705 case FT_Data:
706 {
707 int len = ReadLength( in );
708 adapter.WriteData(ReadString(len));
709 }
710 break;
711
712 case FT_Raw:
713 {
714 int len = ReadLength( in );
715 adapter.WriteRaw(ReadString(len));
716 }
717 break;
718
719 case FT_CharSize:
720 {
721 in.Read(&mCharSize, 1);
722 }
723 break;
724
725 default:
726 wxASSERT(true);
727 break;
728 }
729 }
730 }
731 catch( const Error& )
732 {
733 // Document was corrupt, or platform differences in size or endianness
734 // were not well canonicalized
735 return false;
736 }
737
738 wxLogInfo(
739 "Loaded %lld string %f Kb in size", stringsCount, stringsLength / 1024.0);
740
741 return adapter.Finalize();
742}
wxT("CloseDown"))
@ FT_Int
@ FT_String
@ FT_EndTag
@ FT_Push
@ FT_Raw
@ FT_Float
@ FT_Bool
@ FT_Name
@ FT_Double
@ FT_Pop
@ FT_SizeT
@ FT_Data
@ FT_LongLong
@ FT_StartTag
@ FT_Long
std::unordered_map< unsigned short, std::string > IdMap
int id
NUMERIC_FORMATS_API NumericFormatSymbol Lookup(const FormatterContext &context, const NumericConverterType &type, const NumericFormatID &formatIdentifier)
Looks up the format, returns Default for the type if the format is not registered.

References Error, FT_Bool, FT_CharSize, 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, audacity::cloud::audiocom::anonymous_namespace{AuthorizationHandler.cpp}::handler, id, NumericConverterFormats::Lookup(), BufferedStreamReader::Read(), anonymous_namespace{ProjectSerializer.cpp}::ReadDigits, anonymous_namespace{ProjectSerializer.cpp}::ReadInt, anonymous_namespace{ProjectSerializer.cpp}::ReadLength, anonymous_namespace{ProjectSerializer.cpp}::ReadLong, anonymous_namespace{ProjectSerializer.cpp}::ReadLongLong, anonymous_namespace{ProjectSerializer.cpp}::ReadULong, anonymous_namespace{ProjectSerializer.cpp}::ReadUShort, and wxT().

Referenced by ProjectFileIO::LoadProject().

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

◆ DictChanged()

bool ProjectSerializer::DictChanged ( ) const

Definition at line 517 of file ProjectSerializer.cpp.

518{
519 return mDictChanged;
520}

References mDictChanged.

◆ EndTag()

void ProjectSerializer::EndTag ( const wxString &  name)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 376 of file ProjectSerializer.cpp.

377{
380}
const TranslatableString name
Definition: Distortion.cpp:76
void WriteName(const wxString &name)
MemoryStream mBuffer

References MemoryStream::AppendByte(), FT_EndTag, mBuffer, name, and WriteName().

Here is the call graph for this function:

◆ FailureMessage()

TranslatableString ProjectSerializer::FailureMessage ( const FilePath filePath)
static

Definition at line 90 of file ProjectSerializer.cpp.

91{
92 return
93XO("This recovery file was saved by Audacity 2.3.0 or before.\n"
94 "You need to run that version of Audacity to recover the project." );
95}
XO("Cut/Copy/Paste")

References XO().

Here is the call graph for this function:

◆ GetData()

const MemoryStream & ProjectSerializer::GetData ( ) const

Definition at line 507 of file ProjectSerializer.cpp.

508{
509 return mBuffer;
510}

References mBuffer.

Referenced by audacity::cloud::audiocom::sync::ProjectCloudExtension::OnUpdateSaved(), and ProjectFileIO::WriteDoc().

Here is the caller graph for this function:

◆ GetDict()

const MemoryStream & ProjectSerializer::GetDict ( ) const

Definition at line 502 of file ProjectSerializer.cpp.

503{
504 return mDict;
505}

References mDict.

Referenced by audacity::cloud::audiocom::sync::ProjectCloudExtension::OnUpdateSaved(), and ProjectFileIO::WriteDoc().

Here is the caller graph for this function:

◆ IsEmpty()

bool ProjectSerializer::IsEmpty ( ) const

Definition at line 512 of file ProjectSerializer.cpp.

513{
514 return mBuffer.GetSize() == 0;
515}
const size_t GetSize() const noexcept

References MemoryStream::GetSize(), and mBuffer.

Here is the call graph for this function:

◆ StartTag()

void ProjectSerializer::StartTag ( const wxString &  name)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 370 of file ProjectSerializer.cpp.

371{
374}

References MemoryStream::AppendByte(), FT_StartTag, mBuffer, name, and WriteName().

Here is the call graph for this function:

◆ Write()

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

Implements XMLWriter.

Definition at line 464 of file ProjectSerializer.cpp.

465{
467 Length len = value.length() * sizeof(wxStringCharType);
468 WriteLength( mBuffer, len );
469 mBuffer.AppendData(value.wx_str(), len);
470}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Raw, mBuffer, and anonymous_namespace{ProjectSerializer.cpp}::WriteLength.

Here is the call graph for this function:

◆ WriteAttr() [1/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
bool  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 405 of file ProjectSerializer.cpp.

406{
409
410 mBuffer.AppendByte(value);
411}

References MemoryStream::AppendByte(), FT_Bool, mBuffer, name, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [2/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
const wxChar *  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 382 of file ProjectSerializer.cpp.

383{
384 WriteAttr(name, wxString(value));
385}
void WriteAttr(const wxString &name, const wxString &value) override

References name, and WriteAttr().

Here is the call graph for this function:

◆ WriteAttr() [3/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
const wxString &  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 387 of file ProjectSerializer.cpp.

388{
391
392 const Length len = value.length() * sizeof(wxStringCharType);
393 WriteLength( mBuffer, len );
394 mBuffer.AppendData(value.wx_str(), len);
395}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_String, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteLength, and WriteName().

Referenced by WriteAttr().

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

◆ WriteAttr() [4/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
double  value,
int  digits = -1 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 446 of file ProjectSerializer.cpp.

447{
450
451 mBuffer.AppendData(&value, sizeof(value));
452 WriteDigits( mBuffer, digits );
453}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Double, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteDigits, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [5/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
float  value,
int  digits = -1 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 437 of file ProjectSerializer.cpp.

438{
441
442 mBuffer.AppendData(&value, sizeof(value));
443 WriteDigits( mBuffer, digits );
444}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Float, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteDigits, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [6/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
int  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 397 of file ProjectSerializer.cpp.

398{
401
402 WriteInt( mBuffer, value );
403}

References MemoryStream::AppendByte(), FT_Int, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteInt, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [7/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
long long  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 421 of file ProjectSerializer.cpp.

References MemoryStream::AppendByte(), FT_LongLong, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteLongLong, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [8/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
long  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 413 of file ProjectSerializer.cpp.

414{
417
418 WriteLong( mBuffer, value );
419}

References MemoryStream::AppendByte(), FT_Long, mBuffer, name, anonymous_namespace{ProjectSerializer.cpp}::WriteLong, and WriteName().

Here is the call graph for this function:

◆ WriteAttr() [9/9]

void ProjectSerializer::WriteAttr ( const wxString &  name,
size_t  value 
)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 429 of file ProjectSerializer.cpp.

430{
433
434 WriteULong( mBuffer, value );
435}

References MemoryStream::AppendByte(), FT_SizeT, mBuffer, name, WriteName(), and anonymous_namespace{ProjectSerializer.cpp}::WriteULong.

Here is the call graph for this function:

◆ WriteData()

void ProjectSerializer::WriteData ( const wxString &  value)
overridevirtual

Reimplemented from XMLWriter.

Definition at line 455 of file ProjectSerializer.cpp.

456{
458
459 Length len = value.length() * sizeof(wxStringCharType);
460 WriteLength( mBuffer, len );
461 mBuffer.AppendData(value.wx_str(), len);
462}

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Data, mBuffer, and anonymous_namespace{ProjectSerializer.cpp}::WriteLength.

Here is the call graph for this function:

◆ WriteName()

void ProjectSerializer::WriteName ( const wxString &  name)
private

Definition at line 472 of file ProjectSerializer.cpp.

473{
474 wxASSERT(name.length() * sizeof(wxStringCharType) <= SHRT_MAX);
475 UShort id;
476
477 auto nameiter = mNames.find(name);
478 if (nameiter != mNames.end())
479 {
480 id = nameiter->second;
481 }
482 else
483 {
484 // mNames is static. This appends each name to static mDict only once
485 // in each run.
486 UShort len = name.length() * sizeof(wxStringCharType);
487
488 id = mNames.size();
489 mNames[name] = id;
490
492 WriteUShort( mDict, id );
493 WriteUShort( mDict, len );
494 mDict.AppendData(name.wx_str(), len);
495
496 mDictChanged = true;
497 }
498
499 WriteUShort( mBuffer, id );
500}
static NameMap mNames

References MemoryStream::AppendByte(), MemoryStream::AppendData(), FT_Name, id, mBuffer, mDict, mDictChanged, mNames, name, and anonymous_namespace{ProjectSerializer.cpp}::WriteUShort.

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

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

Member Data Documentation

◆ mBuffer

MemoryStream ProjectSerializer::mBuffer
private

Definition at line 75 of file ProjectSerializer.h.

Referenced by EndTag(), GetData(), IsEmpty(), StartTag(), Write(), WriteAttr(), WriteData(), and WriteName().

◆ mDict

MemoryStream ProjectSerializer::mDict
staticprivate

Definition at line 79 of file ProjectSerializer.h.

Referenced by GetDict(), ProjectSerializer(), and WriteName().

◆ mDictChanged

bool ProjectSerializer::mDictChanged
private

Definition at line 76 of file ProjectSerializer.h.

Referenced by DictChanged(), ProjectSerializer(), and WriteName().

◆ mNames

NameMap ProjectSerializer::mNames
staticprivate

Definition at line 78 of file ProjectSerializer.h.

Referenced by WriteName().


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