src/INICommand.cpp
branchv_0
changeset 7 95b21edc9519
parent 6 fb717cfbfea1
child 8 83b23480d41f
equal deleted inserted replaced
6:fb717cfbfea1 7:95b21edc9519
    20 #include <memory>
    20 #include <memory>
    21 
    21 
    22 #include <relpipe/writer/RelationalWriter.h>
    22 #include <relpipe/writer/RelationalWriter.h>
    23 #include <relpipe/writer/RelpipeWriterException.h>
    23 #include <relpipe/writer/RelpipeWriterException.h>
    24 #include <relpipe/writer/AttributeMetadata.h>
    24 #include <relpipe/writer/AttributeMetadata.h>
    25 #include <relpipe/writer/TypeId.h>
    25 #include <relpipe/common/type/typedefs.h>
    26 
    26 
    27 #include <relpipe/cli/CLI.h>
    27 #include <relpipe/cli/CLI.h>
    28 
    28 
    29 #include "INICommand.h"
    29 #include "INICommand.h"
    30 #include "lib/INIReader.h"
    30 #include "lib/INIReader.h"
    52 			if (i > 0)result << "/";
    52 			if (i > 0)result << "/";
    53 			result << currentSection[i];
    53 			result << currentSection[i];
    54 		}
    54 		}
    55 
    55 
    56 		return result.str();
    56 		return result.str();
       
    57 	}
       
    58 
       
    59 	class Record {
       
    60 	public:
       
    61 		relpipe::common::type::Integer lineNumber = -1;
       
    62 		relpipe::common::type::Integer eventNumber = -1;
       
    63 		std::string key;
       
    64 		std::string subKey;
       
    65 		std::string value;
       
    66 		std::string comment;
       
    67 		std::string whitespace;
       
    68 
       
    69 		Record(const Event * const event) : lineNumber(event->lineNumber), eventNumber(event->eventNumber) {
       
    70 		}
       
    71 
       
    72 	};
       
    73 
       
    74 	void write(const Record& record) {
       
    75 		if (configuration.enableLineNumbers) writer->writeAttribute(&record.lineNumber, typeid (record.lineNumber));
       
    76 		if (configuration.enableEventNumbers) writer->writeAttribute(&record.eventNumber, typeid (record.eventNumber));
       
    77 
       
    78 		std::string section = getCurrentSectionFullName();
       
    79 		std::string key = record.key;
       
    80 		if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
       
    81 		else if (section.size()) key = section + "/" + record.key;
       
    82 		writer->writeAttribute(convertor.from_bytes(key));
       
    83 		if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(record.subKey));
       
    84 
       
    85 		writer->writeAttribute(convertor.from_bytes(record.value));
       
    86 		if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(record.comment));
       
    87 		if (configuration.enableWhitespace) writer->writeAttribute(convertor.from_bytes(record.whitespace));
    57 	}
    88 	}
    58 
    89 
    59 public:
    90 public:
    60 
    91 
    61 	FlatINIContentHandler(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : writer(writer), configuration(configuration) {
    92 	FlatINIContentHandler(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : writer(writer), configuration(configuration) {
    88 	void endSection() override {
   119 	void endSection() override {
    89 		currentSection.pop_back();
   120 		currentSection.pop_back();
    90 	};
   121 	};
    91 
   122 
    92 	void entry(const EntryEvent& event) override {
   123 	void entry(const EntryEvent& event) override {
    93 		if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
   124 		Record record(&event);
    94 		if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));
   125 		record.comment = event.comment;
    95 
   126 		record.key = configuration.enableSubKeys ? event.key : event.fullKey;
    96 		std::string section = getCurrentSectionFullName();
   127 		record.subKey = event.subKey;
    97 		std::string key = configuration.enableSubKeys ? event.key : event.fullKey;
   128 		record.value = event.value;
    98 
   129 		write(record);
    99 		if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
       
   100 		else if (section.size()) key = section + "/" + key;
       
   101 		writer->writeAttribute(convertor.from_bytes(key));
       
   102 		if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(event.subKey));
       
   103 
       
   104 		writer->writeAttribute(convertor.from_bytes(event.value));
       
   105 		if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(event.comment));
       
   106 		if (configuration.enableWhitespace) writer->writeAttribute(L"");
       
   107 	};
   130 	};
   108 
   131 
   109 	void comment(const CommentEvent& event) override {
   132 	void comment(const CommentEvent& event) override {
   110 		if (configuration.enableComments) {
   133 		if (configuration.enableComments) {
   111 			if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
   134 			Record record(&event);
   112 			if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));
   135 			record.comment = event.comment;
   113 
   136 			write(record);
   114 			std::string section = getCurrentSectionFullName();
       
   115 			std::string key;
       
   116 
       
   117 			if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
       
   118 			else if (section.size()) key = section + "/";
       
   119 			writer->writeAttribute(convertor.from_bytes(key));
       
   120 
       
   121 			if (configuration.enableSubKeys) writer->writeAttribute(L"");
       
   122 			writer->writeAttribute(L""); // value
       
   123 			writer->writeAttribute(convertor.from_bytes(event.comment));
       
   124 			if (configuration.enableWhitespace) writer->writeAttribute(L"");
       
   125 		}
   137 		}
   126 	}
   138 	}
   127 
   139 
   128 	void whitespace(const WhitespaceEvent& event) override {
   140 	void whitespace(const WhitespaceEvent& event) override {
   129 		if (configuration.enableWhitespace) {
   141 		if (configuration.enableWhitespace) {
   130 			if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
   142 			Record record(&event);
   131 			if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));
   143 			record.whitespace = event.whitespace;
   132 
   144 			write(record);
   133 			std::string section = getCurrentSectionFullName();
       
   134 			std::string key;
       
   135 
       
   136 			if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
       
   137 			else if (section.size()) key = section + "/";
       
   138 			writer->writeAttribute(convertor.from_bytes(key));
       
   139 
       
   140 			if (configuration.enableSubKeys) writer->writeAttribute(L"");
       
   141 			writer->writeAttribute(L""); // value
       
   142 			if (configuration.enableComments) writer->writeAttribute(L"");
       
   143 			writer->writeAttribute(convertor.from_bytes(event.whitespace));
       
   144 		}
   145 		}
   145 	}
   146 	}
   146 	// TODO: unify methods, DRY
       
   147 
   147 
   148 };
   148 };
   149 
   149 
   150 void INICommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
   150 void INICommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
   151 	FlatINIContentHandler handler(writer, configuration);
   151 	FlatINIContentHandler handler(writer, configuration);