src/INIWriter.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 09 Dec 2020 17:55:03 +0100
branchv_0
changeset 0 1bb084f84eb9
child 2 e753a7f967c8
permissions -rw-r--r--
project skeleton
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>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/common/type/typedefs.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/reader/RelpipeReaderException.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace relpipe {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace out {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace ini {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
class INIWriter {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
private:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	std::ostream& output;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	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
    33
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	std::string keyValueSeparator = " = ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	std::string commentSeparatorForSections = " ; ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	std::string commentSeparatorForEntries = " ; ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	std::string commentSeparatorStandalone = "; ";
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
	enum class TokenType {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		SectionName,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		SectionTag,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		SectionComment,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		EntryKey,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		EntrySubKey,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		EntryValue,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		EntryComment,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		StandaloneComment,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	std::string escape(TokenType type, relpipe::common::type::StringX value) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		std::wstringstream result;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		for (wchar_t ch : value) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			if (ch == L'\\') result << "\\\\";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
			else if (ch == L'\n') result << L"\\n";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			else if (ch == L'\r') result << L"\\r";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
			else if (ch == L'\t') result << L"\\t";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
			else if (ch == L'"') result << "\\\"";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			else result.put(ch);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		// TODO: modular escaping (like unescaping in relpipe-in-ini)
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		return convertor.to_bytes(result.str());
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	class SectionStartEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		relpipe::common::type::StringX comment;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		relpipe::common::type::StringX name;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		relpipe::common::type::StringX tag;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	class EntryEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		relpipe::common::type::StringX comment;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		relpipe::common::type::StringX key;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		relpipe::common::type::StringX subKey;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		relpipe::common::type::StringX value;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	class CommentEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		relpipe::common::type::StringX comment;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	class WhitespaceEvent {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		relpipe::common::type::StringX whitespace;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	INIWriter(std::ostream& output) : output(output) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	virtual ~INIWriter() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
	void setOption(relpipe::common::type::StringX uri, relpipe::common::type::StringX value) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		// TODO: setOption()
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		if (uri == L"dialect");
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
		else if (uri == L"comment-separator-for-sections") commentSeparatorForSections = convertor.to_bytes(value);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		else if (uri == L"comment-separator-for-entries") commentSeparatorForEntries = convertor.to_bytes(value);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
		else if (uri == L"comment-separator-standalone") commentSeparatorStandalone = convertor.to_bytes(value);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
		else if (uri == L"key-value-separator") keyValueSeparator = convertor.to_bytes(value);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		else if (uri == L"escape-backspace");
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
		else if (uri == L"escape-basic");
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		else if (uri == L"escape-java-properties");
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
		else throw relpipe::reader::RelpipeReaderException(L"Unsupported writer option: " + uri);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	void startDocument() {
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
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
	void endDocument() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
		output.flush();
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
	void startSection(const SectionStartEvent& event) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
		output << "[" << escape(TokenType::SectionName, event.name) << "]";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
		if (event.tag.size()) output << "[" << escape(TokenType::SectionTag, event.tag) << "]";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
		if (event.comment.size()) output << commentSeparatorForSections << escape(TokenType::SectionComment, event.comment);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
		output << std::endl;
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
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
	void endSection() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
		output << std::endl;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
		output.flush();
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
	void entry(const EntryEvent& event) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
		output << escape(TokenType::EntryKey, event.key);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
		if (event.subKey.size()) output << "[" << escape(TokenType::EntrySubKey, event.subKey) << "]";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
		output << keyValueSeparator << escape(TokenType::EntryValue, event.value);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
		if (event.comment.size()) output << commentSeparatorForEntries << escape(TokenType::EntryComment, event.comment);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
		output << std::endl;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
	}
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
	void comment(const CommentEvent& event) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
		output << commentSeparatorStandalone << escape(TokenType::StandaloneComment, event.comment);
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
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   143
	void whitespace(const WhitespaceEvent& event) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
		for (wchar_t ch : event.whitespace) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
			if (ch == L' ') output << " ";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   146
			else if (ch == L'\t') output << "\t";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
			else if (ch == L'\n') output << "\n";
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
			else if (ch == L'\r'); // TODO: keep CR?
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
			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
   150
		}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
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
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
}