diff options
| author | Anghelo Carvajal <angheloalf95@gmail.com> | 2021-03-17 19:19:16 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-17 18:19:16 -0400 |
| commit | d698b97d02380b3bf2c898de44941d03e37daff4 (patch) | |
| tree | 86a6928ad6d012cbf16c4839bd47312a7c1d207b /ZAPD/ZSymbol.cpp | |
| parent | b25eb9a568b4dd0dbc5c2fea3d9e85ca69e21aa2 (diff) | |
Add new resource ZSymbol (#92)
* ZSymbol
Signed-off-by: Angie <angheloalf95@gmail.com>
* Add array support to ZSymbol
Signed-off-by: Angie <angheloalf95@gmail.com>
* Export symbol as array
Signed-off-by: Angie <angheloalf95@gmail.com>
* Final cleanup
Signed-off-by: Angie <angheloalf95@gmail.com>
* Ups, forgot the offset in a constructor
Signed-off-by: Angie <angheloalf95@gmail.com>
Diffstat (limited to 'ZAPD/ZSymbol.cpp')
| -rw-r--r-- | ZAPD/ZSymbol.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/ZAPD/ZSymbol.cpp b/ZAPD/ZSymbol.cpp new file mode 100644 index 0000000..7fd6803 --- /dev/null +++ b/ZAPD/ZSymbol.cpp @@ -0,0 +1,87 @@ +#include "ZSymbol.h" +#include "StringHelper.h" + + +ZSymbol::ZSymbol(const std::string& nName, int nRawDataIndex, const std::string& nType, uint32_t nTypeSize, bool nIsArray, uint32_t nCount) + : type(nType), typeSize(nTypeSize), isArray(nIsArray), count(nCount) +{ + name = nName; + rawDataIndex = nRawDataIndex; +} + +ZSymbol::ZSymbol(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData, int nRawDataIndex, ZFile* nParent) +{ + rawData.assign(nRawData.begin(), nRawData.end()); + rawDataIndex = nRawDataIndex; + parent = nParent; + + ParseXML(reader); +} + +ZSymbol* ZSymbol::ExtractFromXML(tinyxml2::XMLElement* reader, const std::vector<uint8_t>& nRawData, int nRawDataIndex, ZFile* parent) +{ + ZSymbol* symbol = new ZSymbol(reader, nRawData, nRawDataIndex, parent); + + return symbol; +} + +void ZSymbol::ParseXML(tinyxml2::XMLElement* reader) +{ + ZResource::ParseXML(reader); + + const char* typeXml = reader->Attribute("Type"); + if (typeXml == nullptr) { + fprintf(stderr, "ZSymbol::ParseXML: Warning in '%s'.\n\t Missing 'Type' attribute in xml. Defaulting to 'void*'.\n", name.c_str()); + type = "void*"; + } + else { + type = std::string(typeXml); + } + + const char* typeSizeXml = reader->Attribute("TypeSize"); + if (typeSizeXml == nullptr) { + fprintf(stderr, "ZSymbol::ParseXML: Warning in '%s'.\n\t Missing 'TypeSize' attribute in xml. Defaulting to '4'.\n", name.c_str()); + typeSize = 4; // Size of a word. + } + else { + typeSize = std::strtoul(typeSizeXml, nullptr, 0); + } + + const char* countXml = reader->Attribute("Count"); + if (countXml != nullptr) { + isArray = true; + if (!std::string(countXml).empty()) { + count = std::strtoul(countXml, nullptr, 0); + } + } +} + +int ZSymbol::GetRawDataSize() +{ + if (isArray) { + return count * typeSize; + } + return typeSize; +} + +std::string ZSymbol::GetSourceOutputHeader(const std::string& prefix) +{ + if (isArray) { + if (count == 0) { + return StringHelper::Sprintf("extern %s %s%s[];\n", type.c_str(), prefix.c_str(), name.c_str()); + } + return StringHelper::Sprintf("extern %s %s%s[%i];\n", type.c_str(), prefix.c_str(), name.c_str(), count); + } + return StringHelper::Sprintf("extern %s %s%s;\n", type.c_str(), prefix.c_str(), name.c_str()); +} + +std::string ZSymbol::GetSourceTypeName() +{ + return type; +} + +ZResourceType ZSymbol::GetResourceType() +{ + return ZResourceType::Symbol; +} + |
