src/INIStandardHandler.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 11 Dec 2020 12:34:42 +0100
branchv_0
changeset 2 e753a7f967c8
parent 0 1bb084f84eb9
permissions -rw-r--r--
implement --style standard
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include "Configuration.h"
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "INIWriter.h"
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace out {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace ini {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
class INIStandardHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
private:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	INIWriter& writer;
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
	size_t currentAttributeIndex = 0;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentAttributes;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
	relpipe::common::type::StringX previousSection;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
	relpipe::common::type::StringX currentSection;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
	relpipe::common::type::StringX currentKey;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
	relpipe::common::type::StringX currentSubKey;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
	relpipe::common::type::StringX currentValue;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
	relpipe::common::type::StringX currentComment;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
	relpipe::common::type::StringX currentWhitespace;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	bool hasSections = false;
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	INIStandardHandler(INIWriter& writer) : writer(writer) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	virtual ~INIStandardHandler() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
		currentAttributes = attributes;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
		currentSection.clear();
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
		currentKey.clear();
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
		currentSubKey.clear();
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
		currentValue.clear();
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
		currentComment.clear();
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
		currentWhitespace.clear();
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	void attribute(const relpipe::common::type::StringX& value) override {
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
		auto name = currentAttributes[currentAttributeIndex % currentAttributes.size()].getAttributeName();
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
		if (name == L"section") currentSection = value;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
		else if (name == L"key") currentKey = value;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
		else if (name == L"sub_key") currentSubKey = value;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
		else if (name == L"value") currentValue = value;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
		else if (name == L"comment") currentComment = value;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
		else if (name == L"whitespace") currentWhitespace = value;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
		currentAttributeIndex++;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
		if (currentAttributeIndex == currentAttributes.size()) {
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
			currentAttributeIndex = 0;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
			if (currentSection != previousSection) {
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
				if (previousSection.size()) writer.endSection();
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
				writer.startSection({
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
					.comment = currentKey.empty() ? currentComment : L"",
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
					.name = currentSection,
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
					.tag = L"",
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
				});
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
				previousSection = currentSection;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
				hasSections = true;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    93
			}
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
			if (currentKey.size()) {
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
				writer.entry({
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
					.comment = currentComment,
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    98
					.key = currentKey,
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    99
					.subKey = currentSubKey,
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   100
					.value = currentValue,
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   101
				});
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   102
			} else if (currentComment.size()) {
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   103
				writer.comment({
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
					.comment = currentComment,
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   105
				});
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   106
			} else if (currentWhitespace.size()) {
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
				writer.whitespace({
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
					.whitespace = currentWhitespace,
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
				});
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   110
			}
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   111
		}
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
	void endOfPipe() {
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
		if (hasSections) writer.endSection();
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
}