src/XMLDocumentConstructor.h
branchv_0
changeset 38 ba850b20fe0d
parent 37 cae9eedf4180
child 39 77d585a1b98c
equal deleted inserted replaced
37:cae9eedf4180 38:ba850b20fe0d
    58 	std::vector<ssize_t> remainingItems;
    58 	std::vector<ssize_t> remainingItems;
    59 #define INDEFINITE -1
    59 #define INDEFINITE -1
    60 
    60 
    61 	std::stringstream currentIndefiniteString;
    61 	std::stringstream currentIndefiniteString;
    62 
    62 
       
    63 	uint64_t currentTag;
       
    64 	bool currentTagPresent;
       
    65 
    63 	cbor_callbacks callbacks = cbor_empty_callbacks;
    66 	cbor_callbacks callbacks = cbor_empty_callbacks;
    64 
    67 
    65 	/**
    68 	/**
    66 	 * Both CBOR and XML strings are in UTF-8.
    69 	 * Both CBOR and XML strings are in UTF-8.
    67 	 */
    70 	 */
    80 				if (remainingItems.back() < 1) containerEnd();
    83 				if (remainingItems.back() < 1) containerEnd();
    81 			}
    84 			}
    82 		}
    85 		}
    83 	}
    86 	}
    84 
    87 
       
    88 	void writeCurrentTag(xmlpp::Element* element) {
       
    89 		if (currentTagPresent) {
       
    90 			element->set_attribute("tag", std::to_string(currentTag));
       
    91 			currentTagPresent = false;
       
    92 		}
       
    93 	}
       
    94 
    85 	void appendScalarValue(Glib::ustring value, Glib::ustring cborType) {
    95 	void appendScalarValue(Glib::ustring value, Glib::ustring cborType) {
    86 		if (mode.back() == Mode::ARRAY) {
    96 		if (mode.back() == Mode::ARRAY) {
    87 			xmlpp::Element* element = current->add_child(nameCodec.encode(itemName));
    97 			xmlpp::Element* element = current->add_child(nameCodec.encode(itemName));
    88 			element->add_child_text(value);
    98 			element->add_child_text(value);
    89 			element->set_attribute("value-type", cborType);
    99 			element->set_attribute("value-type", cborType);
       
   100 			writeCurrentTag(element);
    90 		} else if (mode.back() == Mode::MAP_KEY) {
   101 		} else if (mode.back() == Mode::MAP_KEY) {
    91 			current = current->add_child(nameCodec.encode(value));
   102 			current = current->add_child(nameCodec.encode(value));
    92 			current->set_attribute("key-type", cborType);
   103 			current->set_attribute("key-type", cborType);
       
   104 			writeCurrentTag(current);
    93 			mode.push_back(Mode::MAP_VALUE);
   105 			mode.push_back(Mode::MAP_VALUE);
    94 		} else if (mode.back() == Mode::MAP_VALUE) {
   106 		} else if (mode.back() == Mode::MAP_VALUE) {
    95 			current->add_child_text(value);
   107 			current->add_child_text(value);
    96 			current->set_attribute("value-type", cborType);
   108 			current->set_attribute("value-type", cborType);
       
   109 			writeCurrentTag(current);
    97 			current = parentOrSelf(current);
   110 			current = parentOrSelf(current);
    98 			mode.pop_back();
   111 			mode.pop_back();
    99 		} else if (mode.back() == Mode::ROOT) {
   112 		} else if (mode.back() == Mode::ROOT) {
   100 			current->add_child_text(value);
   113 			current->add_child_text(value);
       
   114 			writeCurrentTag(current);
   101 		} else if (mode.back() == Mode::BYTE_STRING || mode.back() == Mode::CHAR_STRING) {
   115 		} else if (mode.back() == Mode::BYTE_STRING || mode.back() == Mode::CHAR_STRING) {
   102 			currentIndefiniteString << value;
   116 			currentIndefiniteString << value;
   103 		} else {
   117 		} else {
   104 			// TODO: process YAML_SCALAR_EVENT
   118 			// TODO: throw exception?
   105 		}
   119 		}
   106 
   120 
   107 		checkRemainingItems();
   121 		checkRemainingItems();
   108 	}
   122 	}
   109 
   123 
   118 		}
   132 		}
   119 
   133 
   120 		if (mode.back() == Mode::MAP_KEY) mode.pop_back();
   134 		if (mode.back() == Mode::MAP_KEY) mode.pop_back();
   121 		mode.push_back(Mode::ARRAY);
   135 		mode.push_back(Mode::ARRAY);
   122 		remainingItems.push_back(size);
   136 		remainingItems.push_back(size);
       
   137 		writeCurrentTag(current);
   123 	}
   138 	}
   124 
   139 
   125 	void mapStart(ssize_t size) {
   140 	void mapStart(ssize_t size) {
   126 		if (mode.back() == Mode::ROOT) {
   141 		if (mode.back() == Mode::ROOT) {
   127 		} else if (mode.back() == Mode::ARRAY) {
   142 		} else if (mode.back() == Mode::ARRAY) {
   135 			// …probably not
   150 			// …probably not
   136 		}
   151 		}
   137 
   152 
   138 		mode.push_back(Mode::MAP_KEY);
   153 		mode.push_back(Mode::MAP_KEY);
   139 		remainingItems.push_back(size);
   154 		remainingItems.push_back(size);
       
   155 		writeCurrentTag(current);
   140 	}
   156 	}
   141 
   157 
   142 	void flushCurrentIndefiniteString(Glib::ustring cborType) {
   158 	void flushCurrentIndefiniteString(Glib::ustring cborType) {
   143 		mode.pop_back();
   159 		mode.pop_back();
   144 		appendScalarValue(currentIndefiniteString.str(), cborType);
   160 		appendScalarValue(currentIndefiniteString.str(), cborType);
   241 			CBOR_CALLBACK_END
   257 			CBOR_CALLBACK_END
   242 		};
   258 		};
   243 
   259 
   244 		callbacks.tag = [](void* context, uint64_t value) {
   260 		callbacks.tag = [](void* context, uint64_t value) {
   245 			CBOR_CALLBACK_START
   261 			CBOR_CALLBACK_START
   246 			// TODO: implement
   262 			instance->currentTag = value;
   247 			xmlpp::Element* element = instance->current->add_child("tag");
   263 			instance->currentTagPresent = true;
   248 			element->add_child_text(std::to_string(value));
       
   249 			CBOR_CALLBACK_END
   264 			CBOR_CALLBACK_END
   250 		};
   265 		};
   251 
   266 
   252 		callbacks.uint8 = NUMERIC_CALLBACK(uint8_t, "uint8");
   267 		callbacks.uint8 = NUMERIC_CALLBACK(uint8_t, "uint8");
   253 		callbacks.uint16 = NUMERIC_CALLBACK(uint16_t, "uint16");
   268 		callbacks.uint16 = NUMERIC_CALLBACK(uint16_t, "uint16");