src/INIWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 12 Dec 2020 14:42:36 +0100
branchv_0
changeset 4 372b161669e4
parent 3 ae8775e0bc7a
child 5 bee7acb57330
permissions -rw-r--r--
configurable quoting
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 <string>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <sstream>
4
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    21
#include <regex>
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/common/type/typedefs.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/reader/RelpipeReaderException.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    26
#include "uri.h"
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    27
#include "Dialect.h"
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    28
#include "EscapingProcessor.h"
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    29
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace relpipe {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace out {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace ini {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
class INIWriter {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
private:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	std::ostream& output;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate INI always in UTF-8 like XML?
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
	std::string keyValueSeparator = " = ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	std::string commentSeparatorForSections = " ; ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	std::string commentSeparatorForEntries = " ; ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	std::string commentSeparatorStandalone = "; ";
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    43
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
	bool hasContent = false;
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    46
	/**
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    47
	 * TODO: use a common method
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    48
	 */
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    49
	bool parseBoolean(const relpipe::common::type::StringX& value) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    50
		if (value == L"true") return true;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
		else if (value == L"false") return false;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    52
		else throw relpipe::reader::RelpipeReaderException(L"Unable to parse boolean value: " + value + L" (expecting true or false)");
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    53
	}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    54
4
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
	EscapingProcessor::QuotingType parseQuotingType(const relpipe::common::type::StringX& value) {
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
		if (value == quoting::None) return EscapingProcessor::QuotingType::None;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
		else if (value == quoting::Apostrophes) return EscapingProcessor::QuotingType::Apostrophes;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
		else if (value == quoting::Quotes) return EscapingProcessor::QuotingType::Quotes;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
		else throw relpipe::reader::RelpipeReaderException(L"Unsupported quoting type:" + value);
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
	}
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
	class QuotingRule {
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
	public:
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
		EscapingProcessor::QuotingType type = EscapingProcessor::QuotingType::None;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
		relpipe::common::type::StringX pattern;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    67
		EscapingProcessor::QuotingType findEffectiveQuotingType(const relpipe::common::type::StringX& value) {
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    68
			if (type == EscapingProcessor::QuotingType::None) return type;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    69
			else if (pattern.empty()) return type;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    70
			else return std::regex_match(value, std::wregex(pattern)) ? type : EscapingProcessor::QuotingType::None;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    71
		}
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    72
	};
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    74
	QuotingRule quotingForSections;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
	QuotingRule quotingForKeys;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
	QuotingRule quotingForValues;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    77
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    78
	std::string escape(const relpipe::common::type::StringX& value, EscapingProcessor::TextType type) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    79
		relpipe::common::type::StringX result = value;
4
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
		EscapingProcessor::QuotingType quotingType;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    82
		if (type == EscapingProcessor::TextType::SectionName || type == EscapingProcessor::TextType::SectionTag) quotingType = quotingForSections.findEffectiveQuotingType(value);
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
		else if (type == EscapingProcessor::TextType::EntryKey || type == EscapingProcessor::TextType::EntrySubKey) quotingType = quotingForKeys.findEffectiveQuotingType(value);
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    84
		else if (type == EscapingProcessor::TextType::EntryValue) quotingType = quotingForValues.findEffectiveQuotingType(value);
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    85
		else quotingType = EscapingProcessor::QuotingType::None;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    87
		for (ConfiguredEscapingProcessor p : escapingProcessors) if (p.enbaled) result = p.processor->escape(result, type, quotingType);
4
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    88
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    89
		if (quotingType == EscapingProcessor::QuotingType::Quotes) result = L"\"" + result + L"\"";
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    90
		else if (quotingType == EscapingProcessor::QuotingType::Apostrophes) result = L"'" + result + L"'";
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    91
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    92
		return convertor.to_bytes(result);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    93
	}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    94
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    95
	class ConfiguredEscapingProcessor {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    96
	public:
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    97
		std::shared_ptr<EscapingProcessor> processor;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    98
		const relpipe::common::type::StringX uri;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    99
		bool enbaled;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   100
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   101
		ConfiguredEscapingProcessor(std::shared_ptr<EscapingProcessor> processor, const relpipe::common::type::StringX uri, bool enbaled) : processor(processor), uri(uri), enbaled(enbaled) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   102
		}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   103
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   106
	std::vector<ConfiguredEscapingProcessor> escapingProcessors;
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   108
	bool setEscaping(const relpipe::common::type::StringX& uri, const relpipe::common::type::StringX& value) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   109
		for (ConfiguredEscapingProcessor& p : escapingProcessors) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   110
			if (p.uri == uri) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   111
				p.enbaled = parseBoolean(value);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   112
				return true;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   113
			}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   114
		}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   115
		return false;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   116
	}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   117
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   118
	class ConfiguredDialect {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   119
	public:
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   120
		std::shared_ptr<Dialect> dialect;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   121
		const relpipe::common::type::StringX uri;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   122
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   123
		ConfiguredDialect(std::shared_ptr<Dialect> dialect, const relpipe::common::type::StringX uri) : dialect(dialect), uri(uri) {
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
		}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   126
	};
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   127
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   128
	std::vector<ConfiguredDialect> dialects;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   129
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   130
	void setDialect(const relpipe::common::type::StringX& uri) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   131
		for (ConfiguredDialect& d : dialects) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   132
			if (d.uri == uri) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   133
				d.dialect->apply(*this);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   134
				return;
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   135
			}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   136
		}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   137
		throw relpipe::reader::RelpipeReaderException(L"Unsupported INI dialect: " + uri);
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   139
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   141
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   142
	class SectionStartEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   143
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
		relpipe::common::type::StringX comment;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
		relpipe::common::type::StringX name;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   146
		relpipe::common::type::StringX tag;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
	class EntryEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
		relpipe::common::type::StringX comment;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
		relpipe::common::type::StringX key;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
		relpipe::common::type::StringX subKey;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
		relpipe::common::type::StringX value;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   155
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
	class CommentEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
		relpipe::common::type::StringX comment;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
	class WhitespaceEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
		relpipe::common::type::StringX whitespace;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	INIWriter(std::ostream& output) : output(output) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
	virtual ~INIWriter() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   172
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
	void setOption(relpipe::common::type::StringX uri, relpipe::common::type::StringX value) {
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   174
		if (uri == option::Dialect) setDialect(value);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   175
		else if (uri == option::CommentSeparatorForSections) commentSeparatorForSections = convertor.to_bytes(value);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   176
		else if (uri == option::CommentSeparatorForEntries) commentSeparatorForEntries = convertor.to_bytes(value);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   177
		else if (uri == option::CommentSeparatorStandalone) commentSeparatorStandalone = convertor.to_bytes(value);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   178
		else if (uri == option::KeyValueSeparator) keyValueSeparator = convertor.to_bytes(value);
4
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   179
		else if (uri == option::AllowSections); // TODO: allow sections
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   180
		else if (uri == option::QuotesTypeForSections) quotingForSections.type = parseQuotingType(value);
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   181
		else if (uri == option::QuotesTypeForKeys) quotingForKeys.type = parseQuotingType(value);
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   182
		else if (uri == option::QuotesTypeForValues) quotingForValues.type = parseQuotingType(value);
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   183
		else if (uri == option::QuotesPatternForSections) quotingForSections.pattern = value;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   184
		else if (uri == option::QuotesPatternForKeys) quotingForKeys.pattern = value;
372b161669e4 configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   185
		else if (uri == option::QuotesPatternForValues) quotingForValues.pattern = value;
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   186
		else if (setEscaping(uri, value));
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
		else throw relpipe::reader::RelpipeReaderException(L"Unsupported writer option: " + uri);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   188
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   190
	void addDialect(std::shared_ptr<Dialect> dialect, const relpipe::common::type::StringX uri, bool enabledByDefault) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   191
		dialects.push_back({dialect, uri});
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   192
		if (enabledByDefault) dialect->apply(*this);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   193
	}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   194
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   195
	void addEscapingProcessor(std::shared_ptr<EscapingProcessor> processor, const relpipe::common::type::StringX uri, bool enabledByDefault) {
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   196
		escapingProcessors.push_back({processor, uri, enabledByDefault});
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   197
	}
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   198
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
	void startDocument() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   201
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
	void endDocument() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
		output.flush();
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
	void startSection(const SectionStartEvent& event) {
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   207
		if (hasContent) output << std::endl;
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   208
		output << "[" << escape(event.name, EscapingProcessor::TextType::SectionName) << "]";
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   209
		if (event.tag.size()) output << "[" << escape(event.tag, EscapingProcessor::TextType::SectionTag) << "]";
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   210
		if (event.comment.size()) output << commentSeparatorForSections << escape(event.comment, EscapingProcessor::TextType::SectionComment);
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   211
		output << std::endl;
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   212
		hasContent = true;
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   213
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   214
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   215
	void endSection() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   216
		output.flush();
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   219
	void entry(const EntryEvent& event) {
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   220
		output << escape(event.key, EscapingProcessor::TextType::EntryKey);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   221
		if (event.subKey.size()) output << "[" << escape(event.subKey, EscapingProcessor::TextType::EntrySubKey) << "]";
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   222
		output << keyValueSeparator << escape(event.value, EscapingProcessor::TextType::EntryValue);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   223
		if (event.comment.size()) output << commentSeparatorForEntries << escape(event.comment, EscapingProcessor::TextType::EntryComment);
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   224
		output << std::endl;
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   225
		hasContent = true;
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   226
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
	void comment(const CommentEvent& event) {
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   229
		output << commentSeparatorStandalone << escape(event.comment, EscapingProcessor::TextType::StandaloneComment);
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   230
		output << std::endl;
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   231
		hasContent = true;
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   232
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   233
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   234
	void whitespace(const WhitespaceEvent& event) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   235
		for (wchar_t ch : event.whitespace) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   236
			if (ch == L' ') output << " ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   237
			else if (ch == L'\t') output << "\t";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   238
			else if (ch == L'\n') output << "\n";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   239
			else if (ch == L'\r'); // TODO: keep CR?
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
			else; // TODO: throw exception if whitespace contains unexpected data? (should not happen)
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   241
		}
2
e753a7f967c8 implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   242
		hasContent = true;
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   243
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   244
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   245
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   246
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   247
};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   248
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   249
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   250
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   251
}