Gran Turismo File System (GTFS): C++98/STL parsing library

File extension

vol

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of Gran Turismo File System (GTFS) 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.vol", 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:
    gran_turismo_vol_t data(&ks);
    

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

data.magic() // => get magic

C++98/STL source code to parse Gran Turismo File System (GTFS)

gran_turismo_vol.h

#ifndef GRAN_TURISMO_VOL_H_
#define GRAN_TURISMO_VOL_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 gran_turismo_vol_t : public kaitai::kstruct {

public:
    class file_info_t;

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

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

public:
    ~gran_turismo_vol_t();

    class file_info_t : public kaitai::kstruct {

    public:

        file_info_t(kaitai::kstream* p__io, gran_turismo_vol_t* p__parent = 0, gran_turismo_vol_t* p__root = 0);

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

    public:
        ~file_info_t();

    private:
        bool f_size;
        int32_t m_size;

    public:
        int32_t size();

    private:
        bool f_body;
        std::string m_body;
        bool n_body;

    public:
        bool _is_null_body() { body(); return n_body; };

    private:

    public:
        std::string body();

    private:
        bool f_is_dir;
        bool m_is_dir;

    public:
        bool is_dir();

    private:
        bool f_is_last_entry;
        bool m_is_last_entry;

    public:
        bool is_last_entry();

    private:
        uint32_t m_timestamp;
        uint16_t m_offset_idx;
        uint8_t m_flags;
        std::string m_name;
        gran_turismo_vol_t* m__root;
        gran_turismo_vol_t* m__parent;

    public:
        uint32_t timestamp() const { return m_timestamp; }
        uint16_t offset_idx() const { return m_offset_idx; }
        uint8_t flags() const { return m_flags; }
        std::string name() const { return m_name; }
        gran_turismo_vol_t* _root() const { return m__root; }
        gran_turismo_vol_t* _parent() const { return m__parent; }
    };

private:
    bool f_ofs_dir;
    uint32_t m_ofs_dir;

public:
    uint32_t ofs_dir();

private:
    bool f_files;
    std::vector<file_info_t*>* m_files;

public:
    std::vector<file_info_t*>* files();

private:
    std::string m_magic;
    uint16_t m_num_files;
    uint16_t m_num_entries;
    std::string m_reserved;
    std::vector<uint32_t>* m_offsets;
    gran_turismo_vol_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }
    uint16_t num_files() const { return m_num_files; }
    uint16_t num_entries() const { return m_num_entries; }
    std::string reserved() const { return m_reserved; }
    std::vector<uint32_t>* offsets() const { return m_offsets; }
    gran_turismo_vol_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // GRAN_TURISMO_VOL_H_

gran_turismo_vol.cpp

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

#include "gran_turismo_vol.h"
#include "kaitai/exceptions.h"

gran_turismo_vol_t::gran_turismo_vol_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, gran_turismo_vol_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_offsets = 0;
    m_files = 0;
    f_ofs_dir = false;
    f_files = false;

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

void gran_turismo_vol_t::_read() {
    m_magic = m__io->read_bytes(8);
    if (!(magic() == std::string("\x47\x54\x46\x53\x00\x00\x00\x00", 8))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x47\x54\x46\x53\x00\x00\x00\x00", 8), magic(), _io(), std::string("/seq/0"));
    }
    m_num_files = m__io->read_u2le();
    m_num_entries = m__io->read_u2le();
    m_reserved = m__io->read_bytes(4);
    if (!(reserved() == std::string("\x00\x00\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x00\x00", 4), reserved(), _io(), std::string("/seq/3"));
    }
    m_offsets = new std::vector<uint32_t>();
    const int l_offsets = num_files();
    for (int i = 0; i < l_offsets; i++) {
        m_offsets->push_back(m__io->read_u4le());
    }
}

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

void gran_turismo_vol_t::_clean_up() {
    if (m_offsets) {
        delete m_offsets; m_offsets = 0;
    }
    if (f_files) {
        if (m_files) {
            for (std::vector<file_info_t*>::iterator it = m_files->begin(); it != m_files->end(); ++it) {
                delete *it;
            }
            delete m_files; m_files = 0;
        }
    }
}

gran_turismo_vol_t::file_info_t::file_info_t(kaitai::kstream* p__io, gran_turismo_vol_t* p__parent, gran_turismo_vol_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_size = false;
    f_body = false;
    f_is_dir = false;
    f_is_last_entry = false;

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

void gran_turismo_vol_t::file_info_t::_read() {
    m_timestamp = m__io->read_u4le();
    m_offset_idx = m__io->read_u2le();
    m_flags = m__io->read_u1();
    m_name = kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(kaitai::kstream::bytes_strip_right(m__io->read_bytes(25), 0), 0, false), std::string("ASCII"));
}

gran_turismo_vol_t::file_info_t::~file_info_t() {
    _clean_up();
}

void gran_turismo_vol_t::file_info_t::_clean_up() {
    if (f_body && !n_body) {
    }
}

int32_t gran_turismo_vol_t::file_info_t::size() {
    if (f_size)
        return m_size;
    m_size = ((_root()->offsets()->at((offset_idx() + 1)) & 4294965248UL) - _root()->offsets()->at(offset_idx()));
    f_size = true;
    return m_size;
}

std::string gran_turismo_vol_t::file_info_t::body() {
    if (f_body)
        return m_body;
    n_body = true;
    if (!(is_dir())) {
        n_body = false;
        std::streampos _pos = m__io->pos();
        m__io->seek((_root()->offsets()->at(offset_idx()) & 4294965248UL));
        m_body = m__io->read_bytes(size());
        m__io->seek(_pos);
        f_body = true;
    }
    return m_body;
}

bool gran_turismo_vol_t::file_info_t::is_dir() {
    if (f_is_dir)
        return m_is_dir;
    m_is_dir = (flags() & 1) != 0;
    f_is_dir = true;
    return m_is_dir;
}

bool gran_turismo_vol_t::file_info_t::is_last_entry() {
    if (f_is_last_entry)
        return m_is_last_entry;
    m_is_last_entry = (flags() & 128) != 0;
    f_is_last_entry = true;
    return m_is_last_entry;
}

uint32_t gran_turismo_vol_t::ofs_dir() {
    if (f_ofs_dir)
        return m_ofs_dir;
    m_ofs_dir = offsets()->at(1);
    f_ofs_dir = true;
    return m_ofs_dir;
}

std::vector<gran_turismo_vol_t::file_info_t*>* gran_turismo_vol_t::files() {
    if (f_files)
        return m_files;
    std::streampos _pos = m__io->pos();
    m__io->seek((ofs_dir() & 4294965248UL));
    m_files = new std::vector<file_info_t*>();
    const int l_files = _root()->num_entries();
    for (int i = 0; i < l_files; i++) {
        m_files->push_back(new file_info_t(m__io, this, m__root));
    }
    m__io->seek(_pos);
    f_files = true;
    return m_files;
}