src/INIStandardHandler.h
branchv_0
changeset 2 e753a7f967c8
parent 0 1bb084f84eb9
equal deleted inserted replaced
1:e04e5bbc147b 2:e753a7f967c8
    37 namespace ini {
    37 namespace ini {
    38 
    38 
    39 class INIStandardHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    39 class INIStandardHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    40 private:
    40 private:
    41 	INIWriter& writer;
    41 	INIWriter& writer;
       
    42 	size_t currentAttributeIndex = 0;
       
    43 	std::vector<relpipe::reader::handlers::AttributeMetadata> currentAttributes;
       
    44 	relpipe::common::type::StringX previousSection;
       
    45 	relpipe::common::type::StringX currentSection;
       
    46 	relpipe::common::type::StringX currentKey;
       
    47 	relpipe::common::type::StringX currentSubKey;
       
    48 	relpipe::common::type::StringX currentValue;
       
    49 	relpipe::common::type::StringX currentComment;
       
    50 	relpipe::common::type::StringX currentWhitespace;
       
    51 	bool hasSections = false;
    42 public:
    52 public:
    43 
    53 
    44 	INIStandardHandler(INIWriter& writer) : writer(writer) {
    54 	INIStandardHandler(INIWriter& writer) : writer(writer) {
    45 	}
    55 	}
    46 
    56 
    47 	virtual ~INIStandardHandler() {
    57 	virtual ~INIStandardHandler() {
    48 
       
    49 	}
    58 	}
    50 
    59 
    51 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
    60 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
       
    61 		currentAttributes = attributes;
       
    62 		currentSection.clear();
       
    63 		currentKey.clear();
       
    64 		currentSubKey.clear();
       
    65 		currentValue.clear();
       
    66 		currentComment.clear();
       
    67 		currentWhitespace.clear();
    52 	}
    68 	}
    53 
    69 
    54 	void attribute(const relpipe::common::type::StringX& value) override {
    70 	void attribute(const relpipe::common::type::StringX& value) override {
       
    71 		auto name = currentAttributes[currentAttributeIndex % currentAttributes.size()].getAttributeName();
       
    72 		if (name == L"section") currentSection = value;
       
    73 		else if (name == L"key") currentKey = value;
       
    74 		else if (name == L"sub_key") currentSubKey = value;
       
    75 		else if (name == L"value") currentValue = value;
       
    76 		else if (name == L"comment") currentComment = value;
       
    77 		else if (name == L"whitespace") currentWhitespace = value;
       
    78 
       
    79 		currentAttributeIndex++;
       
    80 
       
    81 		if (currentAttributeIndex == currentAttributes.size()) {
       
    82 			currentAttributeIndex = 0;
       
    83 
       
    84 			if (currentSection != previousSection) {
       
    85 				if (previousSection.size()) writer.endSection();
       
    86 				writer.startSection({
       
    87 					.comment = currentKey.empty() ? currentComment : L"",
       
    88 					.name = currentSection,
       
    89 					.tag = L"",
       
    90 				});
       
    91 				previousSection = currentSection;
       
    92 				hasSections = true;
       
    93 			}
       
    94 
       
    95 			if (currentKey.size()) {
       
    96 				writer.entry({
       
    97 					.comment = currentComment,
       
    98 					.key = currentKey,
       
    99 					.subKey = currentSubKey,
       
   100 					.value = currentValue,
       
   101 				});
       
   102 			} else if (currentComment.size()) {
       
   103 				writer.comment({
       
   104 					.comment = currentComment,
       
   105 				});
       
   106 			} else if (currentWhitespace.size()) {
       
   107 				writer.whitespace({
       
   108 					.whitespace = currentWhitespace,
       
   109 				});
       
   110 			}
       
   111 		}
    55 	}
   112 	}
    56 
   113 
    57 	void endOfPipe() {
   114 	void endOfPipe() {
       
   115 		if (hasSections) writer.endSection();
    58 	}
   116 	}
    59 
   117 
    60 };
   118 };
    61 
   119 
    62 }
   120 }