Added an MmapFile helper and changed iffexplore and treexplore to use it. WIP: MveDecoder Misc. changes/fixes
185 lines
3.6 KiB
C++
185 lines
3.6 KiB
C++
#ifndef WC3RE_IFFFILE_HH__
|
|
#define WC3RE_IFFFILE_HH__
|
|
|
|
#include <cstddef>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <memory>
|
|
#include <typeinfo>
|
|
|
|
class IffFile {
|
|
public:
|
|
IffFile(char const* base, size_t length);
|
|
|
|
~IffFile();
|
|
|
|
class Object {
|
|
public:
|
|
Object(std::string type, char const* base, size_t length);
|
|
Object(Object const& copy) = delete;
|
|
|
|
virtual ~Object() {
|
|
}
|
|
|
|
std::string const& getType() const {
|
|
return type_;
|
|
}
|
|
|
|
bool isForm() const {
|
|
return (typeid(*this) == typeid(Form));
|
|
}
|
|
|
|
size_t getSize() const {
|
|
return length_;
|
|
}
|
|
|
|
char const* begin() const {
|
|
return base_;
|
|
}
|
|
|
|
char const* end() const {
|
|
return base_+length_;
|
|
}
|
|
|
|
operator std::string() const;
|
|
|
|
protected:
|
|
char const* base_;
|
|
const size_t length_;
|
|
std::string const type_;
|
|
};
|
|
|
|
class Form final : public Object {
|
|
public:
|
|
Form(std::string type, char const* base, size_t length);
|
|
|
|
~Form() {}
|
|
|
|
std::string const& getSubtype() const {
|
|
return subtype_;
|
|
}
|
|
|
|
size_t getChildCount() const {
|
|
return children_.size();
|
|
}
|
|
|
|
class ObjectIterator : public std::iterator<std::random_access_iterator_tag, const Object> {
|
|
public:
|
|
Object const& operator*() const {
|
|
return **implIt_;
|
|
}
|
|
|
|
ObjectIterator& operator++() {
|
|
++implIt_;
|
|
return *this;
|
|
}
|
|
|
|
ObjectIterator& operator--() {
|
|
--implIt_;
|
|
return *this;
|
|
}
|
|
|
|
bool operator!=(ObjectIterator const& other) const {
|
|
return (implIt_ != other.implIt_);
|
|
}
|
|
|
|
bool operator>(ObjectIterator const& other) const {
|
|
return (implIt_ > other.implIt_);
|
|
}
|
|
|
|
bool operator>=(ObjectIterator const& other) const {
|
|
return (implIt_ >= other.implIt_);
|
|
}
|
|
|
|
bool operator<(ObjectIterator const& other) const {
|
|
return (implIt_ < other.implIt_);
|
|
}
|
|
|
|
bool operator<=(ObjectIterator const& other) const {
|
|
return (implIt_ <= other.implIt_);
|
|
}
|
|
|
|
Object const* operator->() const {
|
|
return implIt_->get();
|
|
}
|
|
|
|
ObjectIterator operator++(int) {
|
|
ObjectIterator ret = *this;
|
|
++implIt_;
|
|
return ret;
|
|
}
|
|
|
|
ObjectIterator operator--(int) {
|
|
ObjectIterator ret = *this;
|
|
--implIt_;
|
|
return ret;
|
|
}
|
|
|
|
ObjectIterator& operator+=(ptrdiff_t n) {
|
|
implIt_ += n;
|
|
return *this;
|
|
}
|
|
|
|
ObjectIterator operator+(ptrdiff_t n) const {
|
|
ObjectIterator ret = *this;
|
|
ret += n;
|
|
return ret;
|
|
}
|
|
|
|
ObjectIterator& operator-=(ptrdiff_t n) {
|
|
implIt_ -= n;
|
|
return *this;
|
|
}
|
|
|
|
ObjectIterator operator-(ptrdiff_t n) const {
|
|
ObjectIterator ret = *this;
|
|
ret -= n;
|
|
return ret;
|
|
}
|
|
|
|
ptrdiff_t operator-(ObjectIterator const& other) const {
|
|
return implIt_ - other.implIt_;
|
|
}
|
|
|
|
Object const& operator[](ptrdiff_t n) const {
|
|
return *(implIt_[n]);
|
|
}
|
|
|
|
private:
|
|
ObjectIterator(std::vector<std::unique_ptr<Object> >::const_iterator implIt)
|
|
: implIt_(std::move(implIt)) {
|
|
}
|
|
|
|
friend class Form;
|
|
|
|
std::vector<std::unique_ptr<Object> >::const_iterator implIt_;
|
|
};
|
|
|
|
ObjectIterator childrenBegin() const {
|
|
return ObjectIterator(children_.cbegin());
|
|
}
|
|
|
|
ObjectIterator childrenEnd() const {
|
|
return ObjectIterator(children_.cend());
|
|
}
|
|
private:
|
|
std::vector<std::unique_ptr<Object> > children_;
|
|
std::string subtype_;
|
|
};
|
|
|
|
Object const& getRoot() const {
|
|
return *root_;
|
|
}
|
|
|
|
void printStructure(unsigned level = 0) const;
|
|
|
|
private:
|
|
static std::unique_ptr<Object> parseObject(char const* base, size_t length);
|
|
|
|
char const* base_;
|
|
const size_t length_;
|
|
std::unique_ptr<Object> root_;
|
|
};
|
|
|
|
#endif
|