src/INICommand.cpp
branchv_0
changeset 6 fb717cfbfea1
parent 5 d70ea23682aa
child 7 95b21edc9519
equal deleted inserted replaced
5:d70ea23682aa 6:fb717cfbfea1
    71 		if (configuration.enableSections) metadata.push_back({L"section", TypeId::STRING});
    71 		if (configuration.enableSections) metadata.push_back({L"section", TypeId::STRING});
    72 		metadata.push_back({L"key", TypeId::STRING});
    72 		metadata.push_back({L"key", TypeId::STRING});
    73 		if (configuration.enableSubKeys) metadata.push_back({L"sub_key", TypeId::STRING});
    73 		if (configuration.enableSubKeys) metadata.push_back({L"sub_key", TypeId::STRING});
    74 		metadata.push_back({L"value", TypeId::STRING});
    74 		metadata.push_back({L"value", TypeId::STRING});
    75 		if (configuration.enableComments) metadata.push_back({L"comment", TypeId::STRING});
    75 		if (configuration.enableComments) metadata.push_back({L"comment", TypeId::STRING});
       
    76 		if (configuration.enableWhitespace) metadata.push_back({L"whitespace", TypeId::STRING});
    76 		writer->startRelation(configuration.relation, metadata, true);
    77 		writer->startRelation(configuration.relation, metadata, true);
    77 	};
    78 	};
    78 
    79 
    79 	void endDocument() override {
    80 	void endDocument() override {
    80 		currentSection.clear();
    81 		currentSection.clear();
   100 		writer->writeAttribute(convertor.from_bytes(key));
   101 		writer->writeAttribute(convertor.from_bytes(key));
   101 		if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(event.subKey));
   102 		if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(event.subKey));
   102 
   103 
   103 		writer->writeAttribute(convertor.from_bytes(event.value));
   104 		writer->writeAttribute(convertor.from_bytes(event.value));
   104 		if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(event.comment));
   105 		if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(event.comment));
       
   106 		if (configuration.enableWhitespace) writer->writeAttribute(L"");
   105 	};
   107 	};
   106 
   108 
   107 	// TODO: handle also comments and whitespace (to allow lossless transformation from INI and back to INI)
   109 	void comment(const CommentEvent& event) override {
       
   110 		if (configuration.enableComments) {
       
   111 			if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
       
   112 			if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));
       
   113 
       
   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 		}
       
   126 	}
       
   127 
       
   128 	void whitespace(const WhitespaceEvent& event) override {
       
   129 		if (configuration.enableWhitespace) {
       
   130 			if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
       
   131 			if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));
       
   132 
       
   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 	}
       
   146 	// TODO: unify methods, DRY
   108 
   147 
   109 };
   148 };
   110 
   149 
   111 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) {
   112 	FlatINIContentHandler handler(writer, configuration);
   151 	FlatINIContentHandler handler(writer, configuration);