3 #ifndef SPECTMORPH_BINBUFFER_HH
4 #define SPECTMORPH_BINBUFFER_HH
6 #include "smhexstring.hh"
16 std::vector<unsigned char> data;
19 bool m_read_error =
false;
22 write_start (
const char *
id)
32 size_t len = data.size();
34 data[0] = (len >> 24);
35 data[1] = (len >> 16);
58 write_string (
const char *str)
60 const unsigned char *raw_str =
reinterpret_cast<const unsigned char *
> (str);
61 size_t len = strlen (str) + 1;
64 data.insert (data.end(), raw_str, raw_str + len);
69 data.push_back (b ? 1 : 0);
72 write_byte (
unsigned char byte)
74 data.push_back (
byte);
77 write_int_seq (
const int* ints,
size_t length)
81 for (
size_t i = 0; i < length; i++)
85 write_float_seq (
const float* floats,
size_t length)
89 for (
size_t i = 0; i < length; i++)
90 write_float (floats[i]);
96 char hex[17] =
"0123456789abcdef";
98 for (
unsigned char byte : data)
100 result += hex[(
byte >> 4) & 0xf];
101 result += hex[
byte & 0xf];
106 from_string (
const std::string& str)
109 return HexString::decode (str, data);
114 return data.size() - rpos;
124 if (remaining() >= 4)
126 int result = (data[rpos] << 24)
127 + (data[rpos + 1] << 16)
128 + (data[rpos + 2] << 8)
143 union {
float f;
int i; } u;
152 read_string_inplace()
154 int len = read_int();
155 char *data = (
char *)read (len);
166 return read_string_inplace();
171 if (remaining() >= l)
173 void *result = &data[rpos];
184 read_int_seq (std::vector<int>& result)
187 size_t seqlen = read_int();
189 if (remaining() >= seqlen * 4)
191 result.resize (seqlen);
193 for (
size_t i = 0; i < seqlen; i++)
194 result[i] = read_int();
203 read_float_seq (std::vector<float>& result)
206 size_t seqlen = read_int();
208 if (remaining() >= seqlen * 4)
210 result.resize (seqlen);
212 for (
size_t i = 0; i < seqlen; i++)
213 result[i] = read_float();
226 return data[rpos++] > 0;
Definition: smbinbuffer.hh:15