Helper type to work around Kaitai Struct not providing an _io
member for plain byte arrays.
This page hosts a formal specification of Byte array with an `_io` member using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing 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.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
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.bin", std::ifstream::binary);
#include <sstream>
std::istringstream is(str);
#include <sstream>
const char buf[] = { ... };
std::string str(buf, sizeof buf);
std::istringstream is(str);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
bytes_with_io_t data(&ks);
After that, one can get various attributes from the structure by invoking getter methods like:
data.data() // => The actual data.
#ifndef BYTES_WITH_IO_H_
#define BYTES_WITH_IO_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>
#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif
/**
* Helper type to work around Kaitai Struct not providing an `_io` member for plain byte arrays.
*/
class bytes_with_io_t : public kaitai::kstruct {
public:
bytes_with_io_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, bytes_with_io_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~bytes_with_io_t();
private:
std::string m_data;
bytes_with_io_t* m__root;
kaitai::kstruct* m__parent;
public:
/**
* The actual data.
*/
std::string data() const { return m_data; }
bytes_with_io_t* _root() const { return m__root; }
kaitai::kstruct* _parent() const { return m__parent; }
};
#endif // BYTES_WITH_IO_H_
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "bytes_with_io.h"
bytes_with_io_t::bytes_with_io_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, bytes_with_io_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = this;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void bytes_with_io_t::_read() {
m_data = m__io->read_bytes_full();
}
bytes_with_io_t::~bytes_with_io_t() {
_clean_up();
}
void bytes_with_io_t::_clean_up() {
}