Cleanup fsTinyXml implementation

This commit is contained in:
Lukas Aldershaab 2022-01-03 21:16:34 +01:00
parent 5abd66dfa3
commit 3ef57168b2
2 changed files with 26 additions and 38 deletions

View file

@ -266,21 +266,21 @@ void VfsXMLDocument::SetError(tinyxml2::XMLError error, int lineNum, const char*
// Overwrite Visitation of elements to add newlines before attributes // Overwrite Visitation of elements to add newlines before attributes
PrettyXMLPrinter::PrettyXMLPrinter(VfsXMLPrinter& innerPrinter, int depth) PrettyXMLPrinter::PrettyXMLPrinter(VfsXMLPrinter& innerPrinter, int depth)
: mInnerPrinter(innerPrinter), : mInnerPrinter(innerPrinter),
_depth(depth) mDepth(depth)
{ {
for (int i = 0; i < ENTITY_RANGE; ++i) { for (int i = 0; i < ENTITY_RANGE; ++i) {
_entityFlag[i] = false; mEntityFlag[i] = false;
_restrictedEntityFlag[i] = false; mRestrictedEntityFlag[i] = false;
} }
for (int i = 0; i < NUM_ENTITIES; ++i) { for (int i = 0; i < NUM_ENTITIES; ++i) {
const char entityValue = entities[i].value; const char entityValue = entities[i].value;
const unsigned char flagIndex = static_cast<unsigned char>(entityValue); const unsigned char flagIndex = static_cast<unsigned char>(entityValue);
TIXMLASSERT(flagIndex < ENTITY_RANGE); TIXMLASSERT(flagIndex < ENTITY_RANGE);
_entityFlag[flagIndex] = true; mEntityFlag[flagIndex] = true;
} }
_restrictedEntityFlag[static_cast<unsigned char>('&')] = true; mRestrictedEntityFlag[static_cast<unsigned char>('&')] = true;
_restrictedEntityFlag[static_cast<unsigned char>('<')] = true; mRestrictedEntityFlag[static_cast<unsigned char>('<')] = true;
_restrictedEntityFlag[static_cast<unsigned char>('>')] = true; // not required, but consistency is nice mRestrictedEntityFlag[static_cast<unsigned char>('>')] = true; // not required, but consistency is nice
} }
void PrettyXMLPrinter::PrintString(const char* p, bool restricted) void PrettyXMLPrinter::PrintString(const char* p, bool restricted)
@ -288,8 +288,8 @@ void PrettyXMLPrinter::PrintString(const char* p, bool restricted)
// Look for runs of bytes between entities to print. // Look for runs of bytes between entities to print.
const char* q = p; const char* q = p;
if (_processEntities) { if (mProcessEntities) {
const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag; const bool* flag = restricted ? mRestrictedEntityFlag : mEntityFlag;
while (*q) { while (*q) {
TIXMLASSERT(p <= q); TIXMLASSERT(p <= q);
// Remember, char is sometimes signed. (How many times has that bitten me?) // Remember, char is sometimes signed. (How many times has that bitten me?)
@ -345,7 +345,7 @@ bool PrettyXMLPrinter::VisitEnter(const tinyxml2::XMLElement& element, const tin
} }
const bool compactMode = parentElem ? mInnerPrinter.CompactMode(*parentElem) : mInnerPrinter.CompactMode(element); const bool compactMode = parentElem ? mInnerPrinter.CompactMode(*parentElem) : mInnerPrinter.CompactMode(element);
mInnerPrinter.OpenElement(element.Name(), compactMode); mInnerPrinter.OpenElement(element.Name(), compactMode);
_depth++; mDepth++;
while (attribute) { while (attribute) {
PushAttribute(attribute->Name(), attribute->Value(), compactMode); PushAttribute(attribute->Name(), attribute->Value(), compactMode);
attribute = attribute->Next(); attribute = attribute->Next();
@ -362,7 +362,7 @@ void PrettyXMLPrinter::PushAttribute(const char* name, const char* value, bool c
else else
{ {
mInnerPrinter.Putc('\n'); mInnerPrinter.Putc('\n');
mInnerPrinter.PrintSpace(_depth); mInnerPrinter.PrintSpace(mDepth);
} }
mInnerPrinter.Write(name); mInnerPrinter.Write(name);
mInnerPrinter.Write("=\""); mInnerPrinter.Write("=\"");

View file

@ -40,34 +40,19 @@ public:
VfsXMLPrinter(FileStream& stream, bool compact = false, int depth = 0); VfsXMLPrinter(FileStream& stream, bool compact = false, int depth = 0);
~VfsXMLPrinter() override; ~VfsXMLPrinter() override;
// Re-implement private functionality in TinyXML2 library, this is just a copy-paste job // Re-implement protected functionality in TinyXML2 library, and make it public
// (This is a bit dirty, but it's necessary for the PrettyXMLPrinter)
bool CompactMode(const tinyxml2::XMLElement& element) override { return tinyxml2::XMLPrinter::CompactMode(element); } bool CompactMode(const tinyxml2::XMLElement& element) override { return tinyxml2::XMLPrinter::CompactMode(element); }
void PrintSpace(int depth) override { tinyxml2::XMLPrinter::PrintSpace(depth); }
inline void Write(const char* data) { Write(data, strlen(data)); }
// Add VFS friendly implementations of output functions // Add VFS friendly implementations of output functions
void Print(const char* format, ...) override; void Print(const char* format, ...) override;
void Write(const char* data, size_t size) override; void Write(const char* data, size_t size) override;
inline void Write(const char* data) { Write(data, strlen(data)); }
void Putc(char ch) override; void Putc(char ch) override;
void PrintSpace(int depth) override { tinyxml2::XMLPrinter::PrintSpace(depth); }
// Overwrite Visitation of elements to add newlines before attributes
void PushAttribute(const char* name, const char* value, bool compactMode);
// Accept a virtual FileStream instead of a FILE pointer // Accept a virtual FileStream instead of a FILE pointer
FileStream& m_Stream; FileStream& m_Stream;
// Track private fields that are necessary for private functionality in TinyXML2
int _depth;
bool _processEntities;
enum
{
ENTITY_RANGE = 64,
BUF_SIZE = 200
};
bool _entityFlag[ENTITY_RANGE];
bool _restrictedEntityFlag[ENTITY_RANGE];
}; };
class VfsXMLDocument : public tinyxml2::XMLDocument class VfsXMLDocument : public tinyxml2::XMLDocument
@ -182,7 +167,7 @@ public:
/// Visit a document. /// Visit a document.
virtual bool VisitEnter(const tinyxml2::XMLDocument& doc) virtual bool VisitEnter(const tinyxml2::XMLDocument& doc)
{ {
_processEntities = doc.ProcessEntities(); mProcessEntities = doc.ProcessEntities();
return mInnerPrinter.VisitEnter(doc); return mInnerPrinter.VisitEnter(doc);
} }
@ -197,7 +182,7 @@ public:
/// Visit an element. /// Visit an element.
virtual bool VisitExit(const tinyxml2::XMLElement& element) virtual bool VisitExit(const tinyxml2::XMLElement& element)
{ {
_depth--; mDepth--;
return mInnerPrinter.VisitExit(element); return mInnerPrinter.VisitExit(element);
} }
@ -229,12 +214,15 @@ public:
// Re-implement private functionality in TinyXML2 library, this is just a copy-paste job // Re-implement private functionality in TinyXML2 library, this is just a copy-paste job
void PrintString(const char*, bool restrictedEntitySet); // prints out, after detecting entities. void PrintString(const char*, bool restrictedEntitySet); // prints out, after detecting entities.
// The inner printer we are wrapping, we only support VfsXMLPrinter based classes because
// stock tinyxml printer is very closed
VfsXMLPrinter& mInnerPrinter; VfsXMLPrinter& mInnerPrinter;
// Track private fields that are necessary for private functionality in TinyXML2 // Track private fields that are necessary for private functionality in TinyXML2
int _depth; int mDepth;
bool _processEntities; bool mProcessEntities;
bool _compactMode; bool mCompactMode;
enum enum
{ {
@ -242,8 +230,8 @@ public:
BUF_SIZE = 200 BUF_SIZE = 200
}; };
bool _entityFlag[ENTITY_RANGE]; bool mEntityFlag[ENTITY_RANGE];
bool _restrictedEntityFlag[ENTITY_RANGE]; bool mRestrictedEntityFlag[ENTITY_RANGE];
}; };