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 356 of file ProjectSerializer.cpp.

357{
358 static std::once_flag flag;
359 std::call_once(flag, []{
360 // Just once per run, store header information in the unique static
361 // dictionary that will be written into each project that is saved.
362 // Store the size of "wxStringCharType" so we can convert during recovery
363 // in case the file is used on a system with a different character size.
364 char size = sizeof(wxStringCharType);
366 mDict.AppendData(&size, 1);
367 });
368
369 mDictChanged = false;
370}
@ 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 372 of file ProjectSerializer.cpp.

373{
374}

Member Function Documentation

◆ Decode()

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

Definition at line 529 of file ProjectSerializer.cpp.

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

524{
525 return mDictChanged;
526}

References mDictChanged.

◆ EndTag()

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

Reimplemented from XMLWriter.

Definition at line 382 of file ProjectSerializer.cpp.

383{
386}
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 89 of file ProjectSerializer.cpp.

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

References XO().

Here is the call graph for this function:

◆ GetData()

const MemoryStream & ProjectSerializer::GetData ( ) const

Definition at line 513 of file ProjectSerializer.cpp.

514{
515 return mBuffer;
516}

References mBuffer.

Referenced by ProjectFileIO::WriteDoc().

Here is the caller graph for this function:

◆ GetDict()

const MemoryStream & ProjectSerializer::GetDict ( ) const

Definition at line 508 of file ProjectSerializer.cpp.

509{
510 return mDict;
511}

References mDict.

Referenced by ProjectFileIO::WriteDoc().

Here is the caller graph for this function:

◆ IsEmpty()

bool ProjectSerializer::IsEmpty ( ) const

Definition at line 518 of file ProjectSerializer.cpp.

519{
520 return mBuffer.GetSize() == 0;
521}
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 376 of file ProjectSerializer.cpp.

377{
380}

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 470 of file ProjectSerializer.cpp.

471{
473 Length len = value.length() * sizeof(wxStringCharType);
474 WriteLength( mBuffer, len );
475 mBuffer.AppendData(value.wx_str(), len);
476}

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 411 of file ProjectSerializer.cpp.

412{
415
416 mBuffer.AppendByte(value);
417}

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 388 of file ProjectSerializer.cpp.

389{
390 WriteAttr(name, wxString(value));
391}
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 393 of file ProjectSerializer.cpp.

394{
397
398 const Length len = value.length() * sizeof(wxStringCharType);
399 WriteLength( mBuffer, len );
400 mBuffer.AppendData(value.wx_str(), len);
401}

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 452 of file ProjectSerializer.cpp.

453{
456
457 mBuffer.AppendData(&value, sizeof(value));
458 WriteDigits( mBuffer, digits );
459}

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 443 of file ProjectSerializer.cpp.

444{
447
448 mBuffer.AppendData(&value, sizeof(value));
449 WriteDigits( mBuffer, digits );
450}

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 403 of file ProjectSerializer.cpp.

404{
407
408 WriteInt( mBuffer, value );
409}

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 427 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 419 of file ProjectSerializer.cpp.

420{
423
424 WriteLong( mBuffer, value );
425}

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 435 of file ProjectSerializer.cpp.

436{
439
440 WriteULong( mBuffer, value );
441}

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 461 of file ProjectSerializer.cpp.

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

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 478 of file ProjectSerializer.cpp.

479{
480 wxASSERT(name.length() * sizeof(wxStringCharType) <= SHRT_MAX);
481 UShort id;
482
483 auto nameiter = mNames.find(name);
484 if (nameiter != mNames.end())
485 {
486 id = nameiter->second;
487 }
488 else
489 {
490 // mNames is static. This appends each name to static mDict only once
491 // in each run.
492 UShort len = name.length() * sizeof(wxStringCharType);
493
494 id = mNames.size();
495 mNames[name] = id;
496
498 WriteUShort( mDict, id );
499 WriteUShort( mDict, len );
500 mDict.AppendData(name.wx_str(), len);
501
502 mDictChanged = true;
503 }
504
505 WriteUShort( mBuffer, id );
506}
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: