Skip to content

Commit 9e9b76f

Browse files
committed
Add support for DBBitArray to reader struct + parsers
1 parent e0f7f9a commit 9e9b76f

7 files changed

Lines changed: 79 additions & 0 deletions

File tree

src/lcf/reader_lcf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lcf/config.h"
1414
#include "lcf/dbstring.h"
1515
#include "lcf/dbarray.h"
16+
#include "lcf/dbbitarray.h"
1617

1718
#include <string>
1819
#include <vector>
@@ -135,6 +136,14 @@ class LcfReader {
135136
template <class T>
136137
void Read(DBArray<T> &buffer, size_t size);
137138

139+
/**
140+
* Reads a DBBitsArray of primitive type.
141+
*
142+
* @param buffer DBBitArray to fill.
143+
* @param size how many bytes to read as bits.
144+
*/
145+
void ReadBits(DBBitArray &buffer, size_t size);
146+
138147
/**
139148
* Reads a compressed integer from the stream.
140149
*

src/lcf/writer_lcf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "lcf/config.h"
1414
#include "lcf/dbarray.h"
15+
#include "lcf/dbbitarray.h"
1516
#include "lcf/dbstring.h"
1617

1718
#include <string>
@@ -71,6 +72,13 @@ class LcfWriter {
7172
void Write(const std::string& str);
7273
void Write(const DBString& str);
7374

75+
/**
76+
* Writes a bit array to bytes in the stream.
77+
*
78+
* @param bits the bit array.
79+
*/
80+
void Write(const DBBitArray& bits);
81+
7482
/**
7583
* Writes a compressed integer to the stream.
7684
*

src/reader_lcf.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ void LcfReader::ReadVector(ArrayType &buffer, size_t size) {
183183
}
184184
}
185185

186+
void LcfReader::ReadBits(DBBitArray &buffer, size_t size) {
187+
buffer = DBBitArray(size);
188+
for (size_t i = 0; i < size; ++i) {
189+
uint8_t val;
190+
Read(&val, sizeof(val), 1);
191+
buffer[i] = static_cast<bool>(val);
192+
}
193+
}
186194

187195
void LcfReader::ReadString(std::string& ref, size_t size) {
188196
ref.resize(size);
@@ -196,6 +204,8 @@ void LcfReader::ReadString(DBString& ref, size_t size) {
196204
ref = DBString(tmp);
197205
}
198206

207+
208+
199209
bool LcfReader::IsOk() const {
200210
return stream.good() && encoder.IsOk();
201211
}

src/reader_struct.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ template <> struct TypeCategory<bool> { static const Category::Index value
8383
template <> struct TypeCategory<double> { static const Category::Index value = Category::Primitive; };
8484
template <> struct TypeCategory<std::string> { static const Category::Index value = Category::Primitive; };
8585
template <> struct TypeCategory<DBString> { static const Category::Index value = Category::Primitive; };
86+
template <> struct TypeCategory<DBBitArray> { static const Category::Index value = Category::Primitive; };
8687

8788
template <class T>
8889
struct TypeCategory<std::vector<T>> {
@@ -348,6 +349,37 @@ struct Primitive<DBString> {
348349
}
349350
};
350351

352+
/**
353+
* DBBitArray specialization.
354+
*/
355+
template <>
356+
struct Primitive<DBBitArray> {
357+
static void ReadLcf(DBBitArray& ref, LcfReader& stream, uint32_t length) {
358+
stream.ReadBits(ref, length);
359+
#ifdef LCF_DEBUG_TRACE
360+
printf(" ");
361+
for (auto& b: ref) {
362+
print("%d", static_cast<int>(b));
363+
}
364+
printf("\n");
365+
#endif
366+
}
367+
static void WriteLcf(const DBBitArray& ref, LcfWriter& stream) {
368+
stream.Write(ref);
369+
}
370+
static int LcfSize(const DBBitArray& ref, LcfWriter& stream) {
371+
return ref.size();
372+
}
373+
static void WriteXml(const DBBitArray& ref, XmlWriter& stream) {
374+
stream.Write(ref);
375+
}
376+
static void ParseXml(DBBitArray& ref, const std::string& data) {
377+
XmlReader::Read(ref, data);
378+
}
379+
};
380+
381+
382+
351383
/**
352384
* Primitive Reader.
353385
*/

src/reader_xml.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ void XmlReader::Read<DBString>(DBString& val, const std::string& data) {
188188
val = DBString(sval);
189189
}
190190

191+
template <>
192+
void XmlReader::Read<DBBitArray>(DBBitArray& val, const std::string& data) {
193+
// FIXME: Adds copies
194+
std::vector<bool> tmp;
195+
ReadVector(tmp, data);
196+
val = DBBitArray(tmp.begin(), tmp.end());
197+
}
198+
191199
template <class T>
192200
void XmlReader::ReadVector(std::vector<T>& val, const std::string& data) {
193201
val.clear();

src/writer_lcf.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ void LcfWriter::Write(const DBString& _str) {
158158
}
159159
}
160160

161+
void LcfWriter::Write(const DBBitArray& bits) {
162+
for (auto& b: bits) {
163+
Write(static_cast<uint8_t>(b));
164+
}
165+
}
166+
161167
uint32_t LcfWriter::Tell() {
162168
return (uint32_t)stream.tellp();
163169
}

src/writer_xml.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lcf/writer_xml.h"
1414
#include "lcf/dbstring.h"
1515
#include "lcf/dbarray.h"
16+
#include "lcf/dbbitarray.h"
1617

1718
namespace lcf {
1819

@@ -176,6 +177,11 @@ void XmlWriter::WriteInt(int val) {
176177
Write<int32_t>(val);
177178
}
178179

180+
template <>
181+
void XmlWriter::Write(const DBBitArray& val) {
182+
WriteVector(val);
183+
}
184+
179185
template <typename ArrayType>
180186
void XmlWriter::WriteVector(const ArrayType& val) {
181187
Indent();

0 commit comments

Comments
 (0)