21#include <wx/ustring.h>
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." );
130template <
typename Number>
139 auto begin =
static_cast<unsigned char*
>(
static_cast<void*
>(&value));
148 in.ReadValue(result);
156 in.ReadValue(result);
157 auto begin =
static_cast<unsigned char*
>(
static_cast<void*
>(&result));
164 IsLittleEndian() ? &WriteLittleEndian<UShort> : &WriteBigEndian<UShort>;
166 IsLittleEndian() ? &WriteLittleEndian<Int> : &WriteBigEndian<Int>;
168 IsLittleEndian() ? &WriteLittleEndian<Long> : &WriteBigEndian<Long>;
170 IsLittleEndian() ? &WriteLittleEndian<ULong> : &WriteBigEndian<ULong>;
172 IsLittleEndian() ? &WriteLittleEndian<LongLong> : &WriteBigEndian<LongLong>;
175 IsLittleEndian() ? &ReadLittleEndian<UShort> : &ReadBigEndian<UShort>;
179 IsLittleEndian() ? &ReadLittleEndian<Long> : &ReadBigEndian<Long>;
181 IsLittleEndian() ? &ReadLittleEndian<ULong> : &ReadBigEndian<ULong>;
183 IsLittleEndian() ? &ReadLittleEndian<LongLong> : &ReadBigEndian<LongLong>;
209 mCurrentTagName =
name;
221 mHandlers.pop_back();
231 mAttributes.emplace_back(
name, CacheString(std::move(value)));
250 handler->HandleXMLContent(CacheString(std::move(value)));
266 EndTag(mCurrentTagName);
269 return mBaseHandler !=
nullptr;
275 if (mHandlers.empty())
277 mHandlers.push_back(mBaseHandler);
282 mHandlers.push_back(
handler->HandleXMLChild(mCurrentTagName));
284 mHandlers.push_back(NULL);
289 if (!
handler->HandleXMLTag(mCurrentTagName, mAttributes))
293 if (mHandlers.size() == 1)
294 mBaseHandler =
nullptr;
298 mStringsCache.clear();
305 mStringsCache.emplace_back(std::move(
string));
306 return mStringsCache.back();
318 bool mInTag {
false };
327template<
typename BaseCharType>
330 constexpr int charSize =
sizeof(BaseCharType);
332 assert(bytesCount % charSize == 0);
334 const auto begin =
static_cast<const BaseCharType*
>(bytes);
335 const auto end =
begin + bytesCount / charSize;
337 const bool isAscii = std::all_of(
340 {
return static_cast<std::make_unsigned_t<BaseCharType>
>(c) < 0x7f; });
345 return std::wstring_convert<std::codecvt_utf8<BaseCharType>, BaseCharType>()
352 static std::once_flag
flag;
353 std::call_once(
flag, []{
358 char size =
sizeof(wxStringCharType);
392 const Length len = value.length() *
sizeof(wxStringCharType);
459 Length len = value.length() *
sizeof(wxStringCharType);
467 Length len = value.length() *
sizeof(wxStringCharType);
474 wxASSERT(
name.length() *
sizeof(wxStringCharType) <= SHRT_MAX);
478 if (nameiter !=
mNames.end())
480 id = nameiter->second;
486 UShort len =
name.length() *
sizeof(wxStringCharType);
528 XMLTagHandlerAdapter adapter(
handler);
530 std::vector<char> bytes;
532 std::vector<IdMap> mIdStack;
540 auto iter = mIds.find(
id );
541 if (iter == mIds.end())
549 int64_t stringsCount = 0;
550 int64_t stringsLength = 0;
552 auto ReadString = [&mCharSize, &in, &bytes, &stringsCount, &stringsLength](
int len) -> std::string
554 bytes.reserve( len );
555 auto data = bytes.data();
556 in.
Read( data, len );
559 stringsLength += len;
564 return std::string(bytes.data(), len);
567 return FastStringConvert<char16_t>(bytes.data(), len);
570 return FastStringConvert<char32_t>(bytes.data(), len);
573 wxASSERT_MSG(
false,
wxT(
"Characters size not 1, 2, or 4"));
590 mIdStack.push_back(mIds);
597 mIds = mIdStack.back();
606 mIds[
id] = ReadString(len);
614 adapter.EmitStartTag(
Lookup(
id));
622 adapter.EndTag(
Lookup(
id));
631 adapter.WriteAttr(
Lookup(
id), ReadString(len));
640 in.
Read(&val,
sizeof(val));
643 adapter.WriteAttr(
Lookup(
id), val);
652 in.
Read(&val,
sizeof(val));
655 adapter.WriteAttr(
Lookup(
id), val);
664 adapter.WriteAttr(
Lookup(
id), val);
675 adapter.WriteAttr(
Lookup(
id), val);
684 adapter.WriteAttr(
Lookup(
id), val);
692 adapter.WriteAttr(
Lookup(
id), val);
701 adapter.WriteAttr(
Lookup(
id), val);
708 adapter.WriteData(ReadString(len));
715 adapter.WriteRaw(ReadString(len));
721 in.
Read(&mCharSize, 1);
731 catch(
const Error& )
739 "Loaded %lld string %f Kb in size", stringsCount, stringsLength / 1024.0);
741 return adapter.Finalize();
bool IsLittleEndian() noexcept
Check that machine is little-endian.
std::unordered_map< unsigned short, std::string > IdMap
std::unordered_map< wxString, unsigned short > NameMap
std::vector< Attribute > AttributesList
A facade-like class, that implements buffered reading from the underlying data stream.
size_t Read(void *buffer, size_t maxBytes)
Read up to maxBytes into the buffer. Returns the number of bytes read.
A low overhead memory stream with O(1) append, low heap fragmentation and a linear memory view.
const size_t GetSize() const noexcept
void AppendData(const void *data, const size_t length)
void AppendByte(char data)
void StartTag(const wxString &name) override
void EndTag(const wxString &name) override
static bool Decode(BufferedStreamReader &in, XMLTagHandler *handler)
static MemoryStream mDict
static TranslatableString FailureMessage(const FilePath &filePath)
const MemoryStream & GetData() const
void WriteName(const wxString &name)
virtual ~ProjectSerializer()
void WriteAttr(const wxString &name, const wxString &value) override
const MemoryStream & GetDict() const
void WriteData(const wxString &value) override
void Write(const wxString &data) override
ProjectSerializer(size_t allocSize=1024 *1024)
Holds a msgid for the translation catalog; may also bind format arguments.
A view into an attribute value. The class does not take the ownership of the data.
This class is an interface which should be implemented by classes which wish to be able to load and s...
std::deque< std::string > mStringsCache
void WriteAttr(const std::string_view &name, std::string value)
std::string_view CacheString(std::string string)
void EndTag(const std::string_view &name)
XMLTagHandler * mBaseHandler
XMLTagHandlerAdapter(XMLTagHandler *handler) noexcept
void WriteRaw(std::string)
void EmitStartTag(const std::string_view &name)
std::vector< XMLTagHandler * > mHandlers
AttributesList mAttributes
std::string_view mCurrentTagName
void WriteAttr(const std::string_view &name, T value)
void WriteData(std::string value)
static const auto WriteLongLong
static const auto ReadDigits
static const auto ReadUShort
static const auto ReadLong
static const auto WriteDigits
Number ReadLittleEndian(BufferedStreamReader &in)
static const auto WriteUShort
static const auto WriteInt
static const auto WriteLength
std::string FastStringConvert(const void *bytes, int bytesCount)
static const auto ReadLongLong
static const auto WriteLong
static const auto ReadULong
static const auto ReadInt
static const auto ReadLength
void WriteLittleEndian(MemoryStream &out, Number value)
static const auto WriteULong
Number ReadBigEndian(BufferedStreamReader &in)
void WriteBigEndian(MemoryStream &out, Number value)
BuiltinEffectsModule::Registration< Reverse > reverse
AuthorizationHandler handler
const char * end(const char *str) noexcept
const char * begin(const char *str) noexcept