.chg file format of MONOMAKH-SAPR: C++/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

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 a stream for reading from a local file:
      #include <fstream>
      
      std::ifstream is("path/to/local/file.chg", std::ifstream::binary);
    • Or one can prepare a stream for reading from existing std::string str:
      #include <sstream>
      
      std::istringstream is(str);
    • Or one can parse arbitrary char* buffer in memory, given that we know its size:
      #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++/STL source code to parse .chg file format of MONOMAKH-SAPR

monomakh_sapr_chg.h

#ifndef MONOMAKH_SAPR_CHG_H_
#define MONOMAKH_SAPR_CHG_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 < 7000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.7 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 = 0, monomakh_sapr_chg_t* p__root = 0);

private:
    void _read();

public:
    ~monomakh_sapr_chg_t();

    class block_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    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::vector<block_t*>* m_ent;
    monomakh_sapr_chg_t* m__root;
    kaitai::kstruct* m__parent;

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

#endif  // MONOMAKH_SAPR_CHG_H_

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;
    _read();
}

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

monomakh_sapr_chg_t::~monomakh_sapr_chg_t() {
    for (std::vector<block_t*>::iterator it = m_ent->begin(); it != m_ent->end(); ++it) {
        delete *it;
    }
    delete m_ent;
}

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() {
}