fallout_dat: C++98/STL parsing library

Application

Fallout

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of fallout_dat using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.fallout_dat", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    fallout_dat_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.folder_count() // => get folder count

C++98/STL source code to parse fallout_dat

fallout_dat.h

#ifndef FALLOUT_DAT_H_
#define FALLOUT_DAT_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

class fallout_dat_t : public kaitai::kstruct {

public:
    class pstr_t;
    class folder_t;
    class file_t;

    enum compression_t {
        COMPRESSION_NONE = 32,
        COMPRESSION_LZSS = 64
    };

    fallout_dat_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, fallout_dat_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~fallout_dat_t();

    class pstr_t : public kaitai::kstruct {

    public:

        pstr_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, fallout_dat_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~pstr_t();

    private:
        uint8_t m_size;
        std::string m_str;
        fallout_dat_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        uint8_t size() const { return m_size; }
        std::string str() const { return m_str; }
        fallout_dat_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

    class folder_t : public kaitai::kstruct {

    public:

        folder_t(kaitai::kstream* p__io, fallout_dat_t* p__parent = 0, fallout_dat_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~folder_t();

    private:
        uint32_t m_file_count;
        uint32_t m_unknown;
        uint32_t m_flags;
        uint32_t m_timestamp;
        std::vector<file_t*>* m_files;
        fallout_dat_t* m__root;
        fallout_dat_t* m__parent;

    public:
        uint32_t file_count() const { return m_file_count; }
        uint32_t unknown() const { return m_unknown; }
        uint32_t flags() const { return m_flags; }
        uint32_t timestamp() const { return m_timestamp; }
        std::vector<file_t*>* files() const { return m_files; }
        fallout_dat_t* _root() const { return m__root; }
        fallout_dat_t* _parent() const { return m__parent; }
    };

    class file_t : public kaitai::kstruct {

    public:

        file_t(kaitai::kstream* p__io, fallout_dat_t::folder_t* p__parent = 0, fallout_dat_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~file_t();

    private:
        bool f_contents;
        std::string m_contents;

    public:
        std::string contents();

    private:
        pstr_t* m_name;
        compression_t m_flags;
        uint32_t m_offset;
        uint32_t m_size_unpacked;
        uint32_t m_size_packed;
        fallout_dat_t* m__root;
        fallout_dat_t::folder_t* m__parent;

    public:
        pstr_t* name() const { return m_name; }
        compression_t flags() const { return m_flags; }
        uint32_t offset() const { return m_offset; }
        uint32_t size_unpacked() const { return m_size_unpacked; }
        uint32_t size_packed() const { return m_size_packed; }
        fallout_dat_t* _root() const { return m__root; }
        fallout_dat_t::folder_t* _parent() const { return m__parent; }
    };

private:
    uint32_t m_folder_count;
    uint32_t m_unknown1;
    uint32_t m_unknown2;
    uint32_t m_timestamp;
    std::vector<pstr_t*>* m_folder_names;
    std::vector<folder_t*>* m_folders;
    fallout_dat_t* m__root;
    kaitai::kstruct* m__parent;

public:
    uint32_t folder_count() const { return m_folder_count; }
    uint32_t unknown1() const { return m_unknown1; }
    uint32_t unknown2() const { return m_unknown2; }
    uint32_t timestamp() const { return m_timestamp; }
    std::vector<pstr_t*>* folder_names() const { return m_folder_names; }
    std::vector<folder_t*>* folders() const { return m_folders; }
    fallout_dat_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // FALLOUT_DAT_H_

fallout_dat.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "fallout_dat.h"

fallout_dat_t::fallout_dat_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, fallout_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_folder_names = 0;
    m_folders = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void fallout_dat_t::_read() {
    m_folder_count = m__io->read_u4be();
    m_unknown1 = m__io->read_u4be();
    m_unknown2 = m__io->read_u4be();
    m_timestamp = m__io->read_u4be();
    int l_folder_names = folder_count();
    m_folder_names = new std::vector<pstr_t*>();
    m_folder_names->reserve(l_folder_names);
    for (int i = 0; i < l_folder_names; i++) {
        m_folder_names->push_back(new pstr_t(m__io, this, m__root));
    }
    int l_folders = folder_count();
    m_folders = new std::vector<folder_t*>();
    m_folders->reserve(l_folders);
    for (int i = 0; i < l_folders; i++) {
        m_folders->push_back(new folder_t(m__io, this, m__root));
    }
}

fallout_dat_t::~fallout_dat_t() {
    _clean_up();
}

void fallout_dat_t::_clean_up() {
    if (m_folder_names) {
        for (std::vector<pstr_t*>::iterator it = m_folder_names->begin(); it != m_folder_names->end(); ++it) {
            delete *it;
        }
        delete m_folder_names; m_folder_names = 0;
    }
    if (m_folders) {
        for (std::vector<folder_t*>::iterator it = m_folders->begin(); it != m_folders->end(); ++it) {
            delete *it;
        }
        delete m_folders; m_folders = 0;
    }
}

fallout_dat_t::pstr_t::pstr_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, fallout_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void fallout_dat_t::pstr_t::_read() {
    m_size = m__io->read_u1();
    m_str = kaitai::kstream::bytes_to_str(m__io->read_bytes(size()), std::string("ASCII"));
}

fallout_dat_t::pstr_t::~pstr_t() {
    _clean_up();
}

void fallout_dat_t::pstr_t::_clean_up() {
}

fallout_dat_t::folder_t::folder_t(kaitai::kstream* p__io, fallout_dat_t* p__parent, fallout_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_files = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void fallout_dat_t::folder_t::_read() {
    m_file_count = m__io->read_u4be();
    m_unknown = m__io->read_u4be();
    m_flags = m__io->read_u4be();
    m_timestamp = m__io->read_u4be();
    int l_files = file_count();
    m_files = new std::vector<file_t*>();
    m_files->reserve(l_files);
    for (int i = 0; i < l_files; i++) {
        m_files->push_back(new file_t(m__io, this, m__root));
    }
}

fallout_dat_t::folder_t::~folder_t() {
    _clean_up();
}

void fallout_dat_t::folder_t::_clean_up() {
    if (m_files) {
        for (std::vector<file_t*>::iterator it = m_files->begin(); it != m_files->end(); ++it) {
            delete *it;
        }
        delete m_files; m_files = 0;
    }
}

fallout_dat_t::file_t::file_t(kaitai::kstream* p__io, fallout_dat_t::folder_t* p__parent, fallout_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_name = 0;
    f_contents = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void fallout_dat_t::file_t::_read() {
    m_name = new pstr_t(m__io, this, m__root);
    m_flags = static_cast<fallout_dat_t::compression_t>(m__io->read_u4be());
    m_offset = m__io->read_u4be();
    m_size_unpacked = m__io->read_u4be();
    m_size_packed = m__io->read_u4be();
}

fallout_dat_t::file_t::~file_t() {
    _clean_up();
}

void fallout_dat_t::file_t::_clean_up() {
    if (m_name) {
        delete m_name; m_name = 0;
    }
    if (f_contents) {
    }
}

std::string fallout_dat_t::file_t::contents() {
    if (f_contents)
        return m_contents;
    kaitai::kstream *io = _root()->_io();
    std::streampos _pos = io->pos();
    io->seek(offset());
    m_contents = io->read_bytes(((flags() == fallout_dat_t::COMPRESSION_NONE) ? (size_unpacked()) : (size_packed())));
    io->seek(_pos);
    f_contents = true;
    return m_contents;
}