.chg file format of MONOMAKH-SAPR: C++11/STL parsing library

CHG is a container format file used by MONOMAKH-SAPR, a software package for analysis & design of reinforced concrete multi-storey buildings with arbitrary configuration in plan.

CHG is a simple container, which bundles several project files together.

Written and tested by Vladimir Shulzhitskiy, 2017

Application

MONOMAKH-SAPR

File extension

chg

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.7

This page hosts a formal specification of .chg file format of MONOMAKH-SAPR 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++11/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.chg", 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:
    monomakh_sapr_chg_t data(&ks);
    

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

data.title() // => get title

C++11/STL source code to parse .chg file format of MONOMAKH-SAPR

monomakh_sapr_chg.h

#pragma once

// 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 <memory>
#include <vector>

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

/**
 * CHG is a container format file used by
 * [MONOMAKH-SAPR](https://www.liraland.com/mono/index.php), a software
 * package for analysis & design of reinforced concrete multi-storey
 * buildings with arbitrary configuration in plan.
 * 
 * CHG is a simple container, which bundles several project files
 * together.
 * 
 * Written and tested by Vladimir Shulzhitskiy, 2017
 */

class monomakh_sapr_chg_t : public kaitai::kstruct {

public:
    class block_t;

    monomakh_sapr_chg_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, monomakh_sapr_chg_t* p__root = nullptr);

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

public:
    ~monomakh_sapr_chg_t();

    class block_t : public kaitai::kstruct {

    public:

        block_t(kaitai::kstream* p__io, monomakh_sapr_chg_t* p__parent = nullptr, monomakh_sapr_chg_t* p__root = nullptr);

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

    public:
        ~block_t();

    private:
        std::string m_header;
        uint64_t m_file_size;
        std::string m_file;
        monomakh_sapr_chg_t* m__root;
        monomakh_sapr_chg_t* m__parent;

    public:
        std::string header() const { return m_header; }
        uint64_t file_size() const { return m_file_size; }
        std::string file() const { return m_file; }
        monomakh_sapr_chg_t* _root() const { return m__root; }
        monomakh_sapr_chg_t* _parent() const { return m__parent; }
    };

private:
    std::string m_title;
    std::unique_ptr<std::vector<std::unique_ptr<block_t>>> m_ent;
    monomakh_sapr_chg_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string title() const { return m_title; }
    std::vector<std::unique_ptr<block_t>>* ent() const { return m_ent.get(); }
    monomakh_sapr_chg_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

monomakh_sapr_chg.cpp

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

#include "monomakh_sapr_chg.h"

monomakh_sapr_chg_t::monomakh_sapr_chg_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, monomakh_sapr_chg_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_ent = nullptr;
    _read();
}

void monomakh_sapr_chg_t::_read() {
    m_title = kaitai::kstream::bytes_to_str(m__io->read_bytes(10), std::string("ascii"));
    m_ent = std::unique_ptr<std::vector<std::unique_ptr<block_t>>>(new std::vector<std::unique_ptr<block_t>>());
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_ent->push_back(std::move(std::unique_ptr<block_t>(new block_t(m__io, this, m__root))));
            i++;
        }
    }
}

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

void monomakh_sapr_chg_t::_clean_up() {
}

monomakh_sapr_chg_t::block_t::block_t(kaitai::kstream* p__io, monomakh_sapr_chg_t* p__parent, monomakh_sapr_chg_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void monomakh_sapr_chg_t::block_t::_read() {
    m_header = kaitai::kstream::bytes_to_str(m__io->read_bytes(13), std::string("ascii"));
    m_file_size = m__io->read_u8le();
    m_file = m__io->read_bytes(file_size());
}

monomakh_sapr_chg_t::block_t::~block_t() {
    _clean_up();
}

void monomakh_sapr_chg_t::block_t::_clean_up() {
}