Skip to content

Commit

Permalink
Rename kstream to kistream, add compat typedef.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchv committed Sep 7, 2019
1 parent c89db17 commit 8b83281
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
6 changes: 3 additions & 3 deletions kaitai/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class undecided_endianness_error: public kstruct_error {
*/
class validation_failed_error: public kstruct_error {
public:
validation_failed_error(const std::string what, const kstream* io, const std::string src_path):
validation_failed_error(const std::string what, const kistream* io, const std::string src_path):
kstruct_error(std::string("at pos ") + /*std::to_string(io->pos())*/ + ": validation failed:" + what, src_path),
m_io(io)
{
Expand All @@ -52,7 +52,7 @@ class validation_failed_error: public kstruct_error {
// "at pos #{io.pos}: validation failed: #{msg}"

protected:
const kstream* m_io;
const kistream* m_io;
};

/**
Expand All @@ -62,7 +62,7 @@ class validation_failed_error: public kstruct_error {
template<typename T>
class validation_not_equal_error: public validation_failed_error {
public:
validation_not_equal_error<T>(const T& expected, const T& actual, const kstream* io, const std::string src_path):
validation_not_equal_error<T>(const T& expected, const T& actual, const kistream* io, const std::string src_path):
validation_failed_error("not equal", io, src_path),
m_expected(expected),
m_actual(actual)
Expand Down
8 changes: 8 additions & 0 deletions kaitai/kaitaistream.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
// Kaitai Struct runtime API version: x.y.z = 'xxxyyyzzz' decimal
#define KAITAI_STRUCT_VERSION 9000L

namespace kaitai {

// This typedef exists for API compatibility with previous versions of the
// KaitaiStruct C++ runtime.
typedef kstream kistream;

}

#endif
6 changes: 3 additions & 3 deletions kaitai/kaitaistruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace kaitai {

class kstruct {
public:
kstruct(kstream *_io) { m__io = _io; }
kstruct(kistream *_io) { m__io = _io; }
virtual ~kstruct() {}
protected:
kstream *m__io;
kistream *m__io;
public:
kstream *_io() { return m__io; }
kistream *_io() { return m__io; }
};

}
Expand Down
64 changes: 32 additions & 32 deletions kaitai/kistream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
#include <vector>
#include <stdexcept>

kaitai::kstream::kstream(std::istream* io): kio(io) {
kaitai::kistream::kistream(std::istream* io): kio(io) {
m_io = io;
init();
}

kaitai::kstream::kstream(std::string& data): m_io_str(data) {
kaitai::kistream::kistream(std::string& data): m_io_str(data) {
kio::init(m_io);
m_io = &m_io_str;
init();
}

void kaitai::kstream::init() {
void kaitai::kistream::init() {
align_to_byte();
}

// ========================================================================
// Stream positioning
// ========================================================================

bool kaitai::kstream::is_eof() const {
bool kaitai::kistream::is_eof() const {
if (m_bits_left > 0) {
return false;
}
Expand All @@ -44,15 +44,15 @@ bool kaitai::kstream::is_eof() const {
}
}

void kaitai::kstream::seek(uint64_t pos) {
void kaitai::kistream::seek(uint64_t pos) {
m_io->seekg(pos);
}

uint64_t kaitai::kstream::pos() {
uint64_t kaitai::kistream::pos() {
return m_io->tellg();
}

uint64_t kaitai::kstream::size() {
uint64_t kaitai::kistream::size() {
std::iostream::pos_type cur_pos = m_io->tellg();
m_io->seekg(0, std::ios::end);
std::iostream::pos_type len = m_io->tellg();
Expand All @@ -68,7 +68,7 @@ uint64_t kaitai::kstream::size() {
// Signed
// ------------------------------------------------------------------------

int8_t kaitai::kstream::read_s1() {
int8_t kaitai::kistream::read_s1() {
char t;
m_io->get(t);
return t;
Expand All @@ -78,7 +78,7 @@ int8_t kaitai::kstream::read_s1() {
// Big-endian
// ........................................................................

int16_t kaitai::kstream::read_s2be() {
int16_t kaitai::kistream::read_s2be() {
int16_t t;
m_io->read(reinterpret_cast<char *>(&t), 2);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -87,7 +87,7 @@ int16_t kaitai::kstream::read_s2be() {
return t;
}

int32_t kaitai::kstream::read_s4be() {
int32_t kaitai::kistream::read_s4be() {
int32_t t;
m_io->read(reinterpret_cast<char *>(&t), 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -96,7 +96,7 @@ int32_t kaitai::kstream::read_s4be() {
return t;
}

int64_t kaitai::kstream::read_s8be() {
int64_t kaitai::kistream::read_s8be() {
int64_t t;
m_io->read(reinterpret_cast<char *>(&t), 8);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -109,7 +109,7 @@ int64_t kaitai::kstream::read_s8be() {
// Little-endian
// ........................................................................

int16_t kaitai::kstream::read_s2le() {
int16_t kaitai::kistream::read_s2le() {
int16_t t;
m_io->read(reinterpret_cast<char *>(&t), 2);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -118,7 +118,7 @@ int16_t kaitai::kstream::read_s2le() {
return t;
}

int32_t kaitai::kstream::read_s4le() {
int32_t kaitai::kistream::read_s4le() {
int32_t t;
m_io->read(reinterpret_cast<char *>(&t), 4);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -127,7 +127,7 @@ int32_t kaitai::kstream::read_s4le() {
return t;
}

int64_t kaitai::kstream::read_s8le() {
int64_t kaitai::kistream::read_s8le() {
int64_t t;
m_io->read(reinterpret_cast<char *>(&t), 8);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -140,7 +140,7 @@ int64_t kaitai::kstream::read_s8le() {
// Unsigned
// ------------------------------------------------------------------------

uint8_t kaitai::kstream::read_u1() {
uint8_t kaitai::kistream::read_u1() {
char t;
m_io->get(t);
return t;
Expand All @@ -150,7 +150,7 @@ uint8_t kaitai::kstream::read_u1() {
// Big-endian
// ........................................................................

uint16_t kaitai::kstream::read_u2be() {
uint16_t kaitai::kistream::read_u2be() {
uint16_t t;
m_io->read(reinterpret_cast<char *>(&t), 2);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -159,7 +159,7 @@ uint16_t kaitai::kstream::read_u2be() {
return t;
}

uint32_t kaitai::kstream::read_u4be() {
uint32_t kaitai::kistream::read_u4be() {
uint32_t t;
m_io->read(reinterpret_cast<char *>(&t), 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -168,7 +168,7 @@ uint32_t kaitai::kstream::read_u4be() {
return t;
}

uint64_t kaitai::kstream::read_u8be() {
uint64_t kaitai::kistream::read_u8be() {
uint64_t t;
m_io->read(reinterpret_cast<char *>(&t), 8);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -181,7 +181,7 @@ uint64_t kaitai::kstream::read_u8be() {
// Little-endian
// ........................................................................

uint16_t kaitai::kstream::read_u2le() {
uint16_t kaitai::kistream::read_u2le() {
uint16_t t;
m_io->read(reinterpret_cast<char *>(&t), 2);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -190,7 +190,7 @@ uint16_t kaitai::kstream::read_u2le() {
return t;
}

uint32_t kaitai::kstream::read_u4le() {
uint32_t kaitai::kistream::read_u4le() {
uint32_t t;
m_io->read(reinterpret_cast<char *>(&t), 4);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -199,7 +199,7 @@ uint32_t kaitai::kstream::read_u4le() {
return t;
}

uint64_t kaitai::kstream::read_u8le() {
uint64_t kaitai::kistream::read_u8le() {
uint64_t t;
m_io->read(reinterpret_cast<char *>(&t), 8);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -216,7 +216,7 @@ uint64_t kaitai::kstream::read_u8le() {
// Big-endian
// ........................................................................

float kaitai::kstream::read_f4be() {
float kaitai::kistream::read_f4be() {
uint32_t t;
m_io->read(reinterpret_cast<char *>(&t), 4);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -225,7 +225,7 @@ float kaitai::kstream::read_f4be() {
return reinterpret_cast<float&>(t);
}

double kaitai::kstream::read_f8be() {
double kaitai::kistream::read_f8be() {
uint64_t t;
m_io->read(reinterpret_cast<char *>(&t), 8);
#if __BYTE_ORDER == __LITTLE_ENDIAN
Expand All @@ -238,7 +238,7 @@ double kaitai::kstream::read_f8be() {
// Little-endian
// ........................................................................

float kaitai::kstream::read_f4le() {
float kaitai::kistream::read_f4le() {
uint32_t t;
m_io->read(reinterpret_cast<char *>(&t), 4);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -247,7 +247,7 @@ float kaitai::kstream::read_f4le() {
return reinterpret_cast<float&>(t);
}

double kaitai::kstream::read_f8le() {
double kaitai::kistream::read_f8le() {
uint64_t t;
m_io->read(reinterpret_cast<char *>(&t), 8);
#if __BYTE_ORDER == __BIG_ENDIAN
Expand All @@ -260,12 +260,12 @@ double kaitai::kstream::read_f8le() {
// Unaligned bit values
// ========================================================================

void kaitai::kstream::align_to_byte() {
void kaitai::kistream::align_to_byte() {
m_bits_left = 0;
m_bits = 0;
}

uint64_t kaitai::kstream::read_bits_int(int n) {
uint64_t kaitai::kistream::read_bits_int(int n) {
int bits_needed = n - m_bits_left;
if (bits_needed > 0) {
// 1 bit => 1 byte
Expand Down Expand Up @@ -303,7 +303,7 @@ uint64_t kaitai::kstream::read_bits_int(int n) {
// Byte arrays
// ========================================================================

std::string kaitai::kstream::read_bytes(std::streamsize len) {
std::string kaitai::kistream::read_bytes(std::streamsize len) {
std::vector<char> result(len);

// NOTE: streamsize type is signed, negative values are only *supposed* to not be used.
Expand All @@ -319,7 +319,7 @@ std::string kaitai::kstream::read_bytes(std::streamsize len) {
return std::string(result.begin(), result.end());
}

std::string kaitai::kstream::read_bytes_full() {
std::string kaitai::kistream::read_bytes_full() {
std::iostream::pos_type p1 = m_io->tellg();
m_io->seekg(0, std::ios::end);
std::iostream::pos_type p2 = m_io->tellg();
Expand All @@ -336,7 +336,7 @@ std::string kaitai::kstream::read_bytes_full() {
return result;
}

std::string kaitai::kstream::read_bytes_term(char term, bool include, bool consume, bool eos_error) {
std::string kaitai::kistream::read_bytes_term(char term, bool include, bool consume, bool eos_error) {
std::string result;
std::getline(*m_io, result, term);
if (m_io->eof()) {
Expand All @@ -354,7 +354,7 @@ std::string kaitai::kstream::read_bytes_term(char term, bool include, bool consu
return result;
}

std::string kaitai::kstream::ensure_fixed_contents(std::string expected) {
std::string kaitai::kistream::ensure_fixed_contents(std::string expected) {
std::string actual = read_bytes(expected.length());

if (actual != expected) {
Expand All @@ -372,7 +372,7 @@ std::string kaitai::kstream::ensure_fixed_contents(std::string expected) {
#ifdef KS_ZLIB
#include <zlib.h>

std::string kaitai::kstream::process_zlib(std::string data) {
std::string kaitai::kistream::process_zlib(std::string data) {
int ret;

unsigned char *src_ptr = reinterpret_cast<unsigned char*>(&data[0]);
Expand Down
6 changes: 3 additions & 3 deletions kaitai/kistream.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace kaitai {

/**
* Kaitai Stream class (kaitai::kstream) is an implementation of
* Kaitai Stream class (kaitai::kistream) is an implementation of
* <a href="https://github.com/kaitai-io/kaitai_struct/wiki/Kaitai-Struct-stream-API">Kaitai Struct stream API</a>
* for C++/STL. It's implemented as a wrapper over generic STL std::istream.
*
Expand All @@ -33,14 +33,14 @@ class kistream : public virtual kio {
* Constructs new Kaitai Stream object, wrapping a given std::istream.
* \param io istream object to use for this Kaitai Stream
*/
kstream(std::istream* io);
kistream(std::istream* io);

/**
* Constructs new Kaitai Stream object, wrapping a given in-memory data
* buffer.
* \param data data buffer to use for this Kaitai Stream
*/
kstream(std::string& data);
kistream(std::string& data);

/** @name Stream positioning */
//@{
Expand Down

0 comments on commit 8b83281

Please sign in to comment.