read CBOR and convert it to a flat XML DOM structure v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 30 May 2021 20:12:05 +0200
branchv_0
changeset 30 f5ac2d29eeb4
parent 29 9254988f9382
child 31 273faff8b848
read CBOR and convert it to a flat XML DOM structure
src/XMLDocumentConstructor.h
--- a/src/XMLDocumentConstructor.h	Sat May 29 22:12:19 2021 +0200
+++ b/src/XMLDocumentConstructor.h	Sun May 30 20:12:05 2021 +0200
@@ -18,10 +18,14 @@
 
 #include <codecvt>
 #include <vector>
+#include <sstream>
+#include <iomanip>
 
 #include <libxml++-2.6/libxml++/libxml++.h>
 #include <cbor.h>
 
+#include <relpipe/writer/RelpipeWriterException.h>
+
 #include "XMLNameCodec.h"
 
 namespace relpipe {
@@ -37,15 +41,17 @@
 	std::string rootName = "cbor";
 	xmlpp::Element* current;
 
+	cbor_callbacks callbacks = cbor_empty_callbacks;
+
 	/**
 	 * Both CBOR and XML strings are in UTF-8.
 	 */
-	const char* c2x(char* value) { // FIXME: cbor type
-		return value ? (const char*) value : "";
+	static std::string c2x(cbor_data value, uint64_t length) {
+		return value && length > 0 ? std::string((const char*) value, length) : "";
 	}
 
-	const Glib::ustring c2xname(char* value) { // FIXME: cbor type
-		return nameCodec.encode(c2x(value));
+	const Glib::ustring c2xname(cbor_data value, uint64_t length) {
+		return nameCodec.encode(c2x(value, length));
 	}
 
 	xmlpp::Element* parentOrSelf(xmlpp::Element* current) {
@@ -55,12 +61,178 @@
 public:
 
 	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
-		// cbor_item_t * root = cbor_new_definite_map(2);
-		// FIXME: initialize CBOR parser
+
+#define CBOR_CALLBACK_START if (XMLDocumentConstructor* instance = static_cast<XMLDocumentConstructor*> (context)) {
+#define CBOR_CALLBACK_END } else throw relpipe::writer::RelpipeWriterException(L"Invalid context in the CBOR callback.");
+
+		callbacks.array_start = [](void* context, size_t size) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("array-start");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.boolean = [](void* context, bool value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("boolean");
+			element->add_child_text(value ? "true" : "false");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.byte_string = [](void* context, cbor_data value, uint64_t size) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("byte-string");
+			element->set_attribute("size", std::to_string(size));
+			std::stringstream hex;
+			hex << std::hex << std::setfill('0') << std::setw(2);
+			for (uint64_t i = 0; i < size; i++) hex << (int) value[i];
+			element->add_child_text(hex.str());
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.byte_string_start = [](void* context) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("byte-string-start");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.float2 = [](void* context, float value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("float2");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.float4 = [](void* context, float value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("float4");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.float8 = [](void* context, double value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("float8");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.indef_array_start = [](void* context) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("array-start");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.indef_map_start = [](void* context) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("map-start");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.indef_break = [](void* context) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("indef-break");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.map_start = [](void* context, size_t size) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("map-start");
+			element->set_attribute("size", std::to_string(size));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.negint8 = [](void* context, uint8_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("negative-int8");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.negint16 = [](void* context, uint16_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("negative-int16");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.negint32 = [](void* context, uint32_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("negative-int32");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.negint64 = [](void* context, uint64_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("negative-int64");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.null = [](void* context) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("null");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.string = [](void* context, cbor_data value, uint64_t size) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("string");
+			element->set_attribute("size", std::to_string(size));
+			element->add_child_text(c2x(value, size));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.string_start = [](void* context) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("string-start");
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.tag = [](void* context, uint64_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("tag");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.uint8 = [](void* context, uint8_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("uint8");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.uint16 = [](void* context, uint16_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("uint16");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.uint32 = [](void* context, uint32_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("uint32");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.uint64 = [](void* context, uint64_t value) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("uint64");
+			element->add_child_text(std::to_string(value));
+			CBOR_CALLBACK_END
+		};
+
+		callbacks.undefined = [](void* context) {
+			CBOR_CALLBACK_START
+			xmlpp::Element* element = instance->current->add_child("undefined");
+			CBOR_CALLBACK_END
+		};
+
 	}
 
 	virtual ~XMLDocumentConstructor() {
-		// FIXME: destroy CBOR parser
 	}
 
 	void setOption(const std::string& uri, const std::string& value) {
@@ -70,6 +242,19 @@
 
 	void process() {
 		current = parser->get_document()->create_root_node(rootName);
+
+		std::stringstream bufferStream;
+		for (char ch = input->get(); input->good(); ch = input->get()) bufferStream.put(ch);
+		std::string buffer = bufferStream.str();
+
+		size_t bytesRead = 0;
+		size_t length = buffer.size();
+		for (cbor_decoder_result result; bytesRead < length; bytesRead += result.read) {
+			result = cbor_stream_decode((cbor_data) buffer.c_str() + bytesRead, length - bytesRead, &callbacks, this);
+			if (result.status != cbor_decoder_status::CBOR_DECODER_FINISHED) throw relpipe::writer::RelpipeWriterException(L"CBOR parsing failed: status = " + std::to_wstring(result.status));
+		}
+
+		parser->get_document()->get_root_node()->set_attribute("bytes-read", std::to_string(bytesRead));
 	}
 };