Add Tag print functions and nbtdump tool

This commit is contained in:
Matthias Schiffer 2018-07-20 23:09:33 +02:00
parent 64f81ff50c
commit 4b9bb2ab48
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
16 changed files with 186 additions and 1 deletions

View file

@ -51,6 +51,24 @@ public:
return Type::ByteArray;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << "(" << len << ") [" << std::endl;
std::string inner = indent + " ";
for (size_t i = 0; i < len; i++) {
uint8_t v = value[i];
os << inner
<< (unsigned)v << " / "
<< (int)(int8_t)v << " / "
<< std::hex << "0x" << (unsigned)v << std::dec
<< std::endl;
}
os << indent << "]";
}
uint32_t getLength() const {
return len;
}

View file

@ -47,6 +47,12 @@ public:
return Type::Byte;
}
virtual void print(std::ostream& os, const std::string &) const {
os << (unsigned)getValue() << " / "
<< (int)(int8_t)getValue() << " / "
<< std::hex << "0x" << (unsigned)getValue() << std::dec;
}
uint8_t getValue() const {
return value;
}

View file

@ -54,6 +54,20 @@ public:
return Type::Compound;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << "{" << std::endl;
std::string inner = indent + " ";
for (const auto &item : *this) {
os << inner << item.first << ": " << item.second->getType() << " ";
item.second->print(os, inner);
os << std::endl;
}
os << indent << "}";
}
template<typename T> std::shared_ptr<const T> get(const std::string &key) const {
auto it = find(key);
if (it == end())

View file

@ -46,6 +46,16 @@ public:
virtual Type getType() const {
return Type::Double;
}
virtual void print(std::ostream& os, const std::string &) const {
union {
uint64_t i;
double d;
};
i = Buffer::parse64(ptr);
os << d;
}
};
}

View file

@ -42,6 +42,9 @@ public:
virtual Type getType() const {
return Type::End;
}
virtual void print(std::ostream&, const std::string &) const {
}
};
}

View file

@ -46,6 +46,16 @@ public:
virtual Type getType() const {
return Type::Float;
}
virtual void print(std::ostream& os, const std::string &) const {
union {
uint32_t i;
float f;
};
i = Buffer::parse32(ptr);
os << f;
}
};
}

View file

@ -50,6 +50,24 @@ public:
virtual Type getType() const {
return Type::IntArray;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << "(" << len << ") [" << std::endl;
std::string inner = indent + " ";
for (size_t i = 0; i < len; i++) {
uint32_t v = Buffer::parse32(&ptr[4*i]);
os << inner
<< v << " / "
<< (int32_t)v << " / "
<< std::hex << "0x" << v << std::dec
<< std::endl;
}
os << indent << "]";
}
};
}

View file

@ -47,6 +47,12 @@ public:
return Type::Int;
}
virtual void print(std::ostream& os, const std::string &) const {
os << getValue() << " / "
<< (int32_t)getValue() << " / "
<< std::hex << "0x" << getValue() << std::dec;
}
uint32_t getValue() const {
return Buffer::parse32(ptr);
}

View file

@ -65,6 +65,20 @@ public:
virtual Type getSubtype() const {
return type;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << getSubtype() << " [" << std::endl;
std::string inner = indent + " ";
for (const auto &item : *this) {
os << inner;
item->print(os, inner);
os << std::endl;
}
os << indent << "]";
}
};
}

View file

@ -47,6 +47,12 @@ public:
return Type::Long;
}
virtual void print(std::ostream& os, const std::string &) const {
os << getValue() << " / "
<< (int64_t)getValue() << " / "
<< std::hex << "0x" << getValue() << std::dec;
}
uint64_t getValue() const {
return Buffer::parse64(ptr);
}

View file

@ -47,6 +47,12 @@ public:
return Type::Short;
}
virtual void print(std::ostream& os, const std::string &) const {
os << getValue() << " / "
<< (int16_t)getValue() << " / "
<< std::hex << "0x" << getValue() << std::dec;
}
uint16_t getValue() const {
return Buffer::parse16(ptr);
}

View file

@ -48,6 +48,10 @@ public:
virtual Type getType() const {
return Type::String;
}
virtual void print(std::ostream& os, const std::string &) const {
os << "\"" << std::string(reinterpret_cast<const char *>(ptr), len) << "\"";
}
};
}

View file

@ -60,11 +60,18 @@ public:
static std::pair<std::string, std::shared_ptr<const Tag>> readNamedTag(Buffer *buffer);
virtual Type getType() const = 0;
virtual void print(std::ostream& os, const std::string &indent) const = 0;
virtual ~Tag() {}
};
std::ostream& operator<<(std::ostream& os, Tag::Type type);
static inline std::ostream& operator<<(std::ostream& os, const Tag &tag) {
os << tag.getType() << " ";
tag.print(os, "");
return os;
}
}
}