Add support for DBBitArray to reader struct + parsers
MODIFY
src/lcf/reader_lcf.h
src/lcf/reader_lcf.h
#include "lcf/config.h"
#include "lcf/dbstring.h"
+#include "lcf/dbbitarray.h"
#include <string>
#include <vector>
@@ -125,6 +126,14 @@ public:
template <class T>
void Read(std::vector<T> &buffer, size_t size);
+ /**
+ * Reads a DBBitsArray of primitive type.
+ *
+ * @param buffer DBBitArray to fill.
+ * @param size how many bytes to read as bits.
+ */
+ void ReadBits(DBBitArray &buffer, size_t size);
+
/**
* Reads a compressed integer from the stream.
*

MODIFY
src/lcf/writer_lcf.h
src/lcf/writer_lcf.h
#define LCF_WRITER_LCF_H
#include "lcf/config.h"
+#include "lcf/dbbitarray.h"
#include "lcf/dbstring.h"
#include <string>
@@ -70,6 +71,13 @@ public:
void Write(const std::string& str);
void Write(const DBString& str);
+ /**
+ * Writes a bit array to bytes in the stream.
+ *
+ * @param bits the bit array.
+ */
+ void Write(const DBBitArray& bits);
+
/**
* Writes a compressed integer to the stream.
*

MODIFY
src/reader_lcf.cpp
src/reader_lcf.cpp
@@ -184,6 +184,15 @@ void LcfReader::Read<uint32_t>(std::vector<uint32_t> &buffer, size_t size) {
}
}
+void LcfReader::ReadBits(DBBitArray &buffer, size_t size) {
+ buffer = DBBitArray(size);
+ for (size_t i = 0; i < size; ++i) {
+ uint8_t val;
+ Read(&val, sizeof(val), 1);
+ buffer[i] = static_cast<bool>(val);
+ }
+}
+
void LcfReader::ReadString(std::string& ref, size_t size) {
ref.resize(size);
Read((size > 0 ? &ref.front(): nullptr), 1, size);
@@ -196,6 +205,8 @@ void LcfReader::ReadString(DBString& ref, size_t size) {
ref = DBString(tmp);
}
+
+
bool LcfReader::IsOk() const {
return stream.good() && encoder.IsOk();
}

MODIFY
src/reader_struct.h
src/reader_struct.h
@@ -83,6 +83,7 @@ template <> struct TypeCategory<bool> { static const Category::Index value
template <> struct TypeCategory<double> { static const Category::Index value = Category::Primitive; };
template <> struct TypeCategory<std::string> { static const Category::Index value = Category::Primitive; };
template <> struct TypeCategory<DBString> { static const Category::Index value = Category::Primitive; };
+template <> struct TypeCategory<DBBitArray> { static const Category::Index value = Category::Primitive; };
template <class T>
struct TypeCategory<std::vector<T>> {
@@ -337,6 +338,37 @@ struct Primitive<DBString> {
}
};
+/**
+ * DBBitArray specialization.
+ */
+template <>
+struct Primitive<DBBitArray> {
+ static void ReadLcf(DBBitArray& ref, LcfReader& stream, uint32_t length) {
+ stream.ReadBits(ref, length);
+#ifdef LCF_DEBUG_TRACE
+ printf(" ");
+ for (auto& b: ref) {
+ print("%d", static_cast<int>(b));
+ }
+ printf("\n");
+#endif
+ }
+ static void WriteLcf(const DBBitArray& ref, LcfWriter& stream) {
+ stream.Write(ref);
+ }
+ static int LcfSize(const DBBitArray& ref, LcfWriter& stream) {
+ return ref.size();
+ }
+ static void WriteXml(const DBBitArray& ref, XmlWriter& stream) {
+ stream.Write(ref);
+ }
+ static void ParseXml(DBBitArray& ref, const std::string& data) {
+ XmlReader::Read(ref, data);
+ }
+};
+
+
+
/**
* Primitive Reader.
*/

MODIFY
src/reader_xml.cpp
src/reader_xml.cpp
@@ -188,6 +188,14 @@ void XmlReader::Read<DBString>(DBString& val, const std::string& data) {
val = DBString(sval);
}
+template <>
+void XmlReader::Read<DBBitArray>(DBBitArray& val, const std::string& data) {
+ // FIXME: Adds copies
+ std::vector<bool> tmp;
+ ReadVector(tmp, data);
+ val = DBBitArray(tmp.begin(), tmp.end());
+}
+
template <class T>
void XmlReader::ReadVector(std::vector<T>& val, const std::string& data) {
val.clear();

MODIFY
src/writer_lcf.cpp
src/writer_lcf.cpp
@@ -127,6 +127,12 @@ void LcfWriter::Write(const DBString& _str) {
}
}
+void LcfWriter::Write(const DBBitArray& bits) {
+ for (auto& b: bits) {
+ Write(static_cast<uint8_t>(b));
+ }
+}
+
uint32_t LcfWriter::Tell() {
return (uint32_t)stream.tellp();
}

MODIFY
src/writer_xml.cpp
src/writer_xml.cpp
#include "lcf/writer_xml.h"
#include "lcf/dbstring.h"
#include "lcf/dbarray.h"
+#include "lcf/dbbitarray.h"
namespace lcf {
@@ -176,6 +177,11 @@ void XmlWriter::WriteInt(int val) {
Write<int32_t>(val);
}
+template <>
+void XmlWriter::Write(const DBBitArray& val) {
+ WriteVector(val);
+}
+
template <typename ArrayType>
void XmlWriter::WriteVector(const ArrayType& val) {
Indent();

Add support for DBBitArray to reader struct + parsers