parse patterns section
This commit is contained in:
73
src/abr.h
73
src/abr.h
@@ -33,7 +33,10 @@ public:
|
||||
m_byte_order = byte_order;
|
||||
m_swap = byte_order == ByteOrder::Host ? false : byte_order != sys_order();
|
||||
}
|
||||
size_t pos() { return std::distance(m_ptr, m_cur); }
|
||||
void skip(size_t bytes) { m_cur += bytes; }
|
||||
// snap to the next 4-alignment
|
||||
void snap() { if ((size_t)m_cur % 4 != 0) m_cur += 4 - (size_t)m_cur % 4; }
|
||||
bool eof() { return std::distance(m_ptr, m_cur) >= m_size; }
|
||||
bool has_data(size_t sz) { return std::distance(m_ptr, m_cur + sz) < m_size; }
|
||||
uint8_t ru8() { return read<uint8_t>(); }
|
||||
@@ -65,6 +68,23 @@ public:
|
||||
std::swap(ptr[i * 2], ptr[i * 2 + 1]);
|
||||
return std::wstring((wchar_t*)ptr, len);
|
||||
}
|
||||
std::string rpascal()
|
||||
{
|
||||
auto len = ru8();
|
||||
return rstring(len);
|
||||
}
|
||||
std::vector<uint8_t> rraw()
|
||||
{
|
||||
auto size = ru32();
|
||||
return rraw(size);
|
||||
}
|
||||
std::vector<uint8_t> rraw(size_t bytes)
|
||||
{
|
||||
std::vector<uint8_t> ret(bytes);
|
||||
std::copy_n(m_cur, bytes, ret.data());
|
||||
skip(bytes);
|
||||
return ret;
|
||||
}
|
||||
protected:
|
||||
template<typename T> inline T align4(T x) { return ((x - T{1}) & (~(T{3}))) + T{4}; }
|
||||
template<typename T> T read()
|
||||
@@ -161,10 +181,9 @@ class ABR : public BinaryStream
|
||||
Type::Vec items;
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{
|
||||
//return std::string(indent, '-') + prefix + fmt::format("list: {} items", items.size());
|
||||
auto ret = std::string(indent, '-') + fmt::format("list: {} props:\n", items.size());
|
||||
auto ret = std::string(indent, '-') + fmt::format("list: {} props:", items.size());
|
||||
for (int i = 0; i < items.size(); i++)
|
||||
ret += items[i]->str(indent + 1, fmt::format("{}) ", i) + "\n");
|
||||
ret += "\n" + items[i]->str(indent + 1, fmt::format("{}) ", i));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
@@ -213,6 +232,12 @@ class ABR : public BinaryStream
|
||||
{ return std::string(indent, '-') + prefix + fmt::format("bool: {}", value); }
|
||||
};
|
||||
struct Alias : public Type { };
|
||||
struct RawData : public Type
|
||||
{
|
||||
std::vector<uint8_t> data;
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{ return std::string(indent, '-') + prefix + fmt::format("raw: {} bytes", data.size()); }
|
||||
};
|
||||
struct Descriptor : public Type
|
||||
{
|
||||
std::wstring name;
|
||||
@@ -220,24 +245,60 @@ class ABR : public BinaryStream
|
||||
Type::Map props;
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{
|
||||
auto ret = std::string(indent, '-') + fmt::format("objc: {} props:", props.size());
|
||||
auto ret = std::string(indent, '-') +
|
||||
fmt::format("objc {} ({}): {} props:", wstr2str(name), class_id, props.size());
|
||||
for (const auto& p : props)
|
||||
ret += "\n" + p.second->str(indent + 1, fmt::format("'{}' ", p.first));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
struct Rectangle : public Type
|
||||
{
|
||||
uint32_t top;
|
||||
uint32_t left;
|
||||
uint32_t bottom;
|
||||
uint32_t right;
|
||||
uint32_t area() const { return (right - left) * (bottom - top); }
|
||||
uint32_t width() const { return right - left; }
|
||||
uint32_t height() const { return bottom - top; }
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{ return std::string(indent, '-') + prefix + fmt::format("rect: [{}, {}, {}, {}]", top, left, bottom, right); }
|
||||
};
|
||||
struct Point : public Type
|
||||
{
|
||||
uint32_t x;
|
||||
uint32_t y;
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{ return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y); }
|
||||
};
|
||||
|
||||
public:
|
||||
ABR();
|
||||
bool open(const std::string& path);
|
||||
private:
|
||||
inline std::string rkey_or_string()
|
||||
std::string rkey_or_string()
|
||||
{
|
||||
auto len = ru32();
|
||||
if (len == 0)
|
||||
len = 4;
|
||||
return rstring(len);
|
||||
}
|
||||
Rectangle rrect()
|
||||
{
|
||||
Rectangle ret;
|
||||
ret.top = ru32();
|
||||
ret.left = ru32();
|
||||
ret.bottom = ru32();
|
||||
ret.right = ru32();
|
||||
return ret;
|
||||
}
|
||||
Point rpoint()
|
||||
{
|
||||
Point ret;
|
||||
ret.x = ru16();
|
||||
ret.y = ru16();
|
||||
return ret;
|
||||
}
|
||||
bool section_desc();
|
||||
bool section_samp();
|
||||
bool section_patt();
|
||||
@@ -249,7 +310,7 @@ private:
|
||||
std::shared_ptr<Integer> parse_long();
|
||||
std::shared_ptr<Double> parse_doub();
|
||||
std::shared_ptr<Enum> parse_enum();
|
||||
//Type::Ref parse_prop();
|
||||
std::shared_ptr<RawData> parse_tdta();
|
||||
Type::Ref call(std::string t)
|
||||
{
|
||||
if (m_parser_table.find(t) != m_parser_table.end())
|
||||
|
||||
Reference in New Issue
Block a user