src/INIWriter.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
child 3 ae8775e0bc7a
permissions -rw-r--r--
implement --style standard

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <string>
#include <sstream>

#include <relpipe/common/type/typedefs.h>
#include <relpipe/reader/RelpipeReaderException.h>

namespace relpipe {
namespace out {
namespace ini {

class INIWriter {
private:
	std::ostream& output;
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate INI always in UTF-8 like XML?

	std::string keyValueSeparator = " = ";
	std::string commentSeparatorForSections = " ; ";
	std::string commentSeparatorForEntries = " ; ";
	std::string commentSeparatorStandalone = "; ";
	
	bool hasContent = false;

	enum class TokenType {
		SectionName,
		SectionTag,
		SectionComment,
		EntryKey,
		EntrySubKey,
		EntryValue,
		EntryComment,
		StandaloneComment,
	};

	std::string escape(TokenType type, relpipe::common::type::StringX value) {
		std::wstringstream result;

		for (wchar_t ch : value) {
			if (ch == L'\\') result << "\\\\";
			else if (ch == L'\n') result << L"\\n";
			else if (ch == L'\r') result << L"\\r";
			else if (ch == L'\t') result << L"\\t";
			else if (ch == L'"') result << "\\\"";
			else result.put(ch);
		}

		// TODO: modular escaping (like unescaping in relpipe-in-ini)
		return convertor.to_bytes(result.str());
	}

public:

	class SectionStartEvent {
	public:
		relpipe::common::type::StringX comment;
		relpipe::common::type::StringX name;
		relpipe::common::type::StringX tag;
	};

	class EntryEvent {
	public:
		relpipe::common::type::StringX comment;
		relpipe::common::type::StringX key;
		relpipe::common::type::StringX subKey;
		relpipe::common::type::StringX value;
	};

	class CommentEvent {
	public:
		relpipe::common::type::StringX comment;
	};

	class WhitespaceEvent {
	public:
		relpipe::common::type::StringX whitespace;
	};

	INIWriter(std::ostream& output) : output(output) {
	}

	virtual ~INIWriter() {
	};

	void setOption(relpipe::common::type::StringX uri, relpipe::common::type::StringX value) {
		// TODO: setOption()
		if (uri == L"dialect");
		else if (uri == L"comment-separator-for-sections") commentSeparatorForSections = convertor.to_bytes(value);
		else if (uri == L"comment-separator-for-entries") commentSeparatorForEntries = convertor.to_bytes(value);
		else if (uri == L"comment-separator-standalone") commentSeparatorStandalone = convertor.to_bytes(value);
		else if (uri == L"key-value-separator") keyValueSeparator = convertor.to_bytes(value);
		else if (uri == L"escape-backspace");
		else if (uri == L"escape-basic");
		else if (uri == L"escape-java-properties");
		else throw relpipe::reader::RelpipeReaderException(L"Unsupported writer option: " + uri);
	}

	void startDocument() {
	}

	void endDocument() {
		output.flush();
	}

	void startSection(const SectionStartEvent& event) {
		if (hasContent) output << std::endl;
		output << "[" << escape(TokenType::SectionName, event.name) << "]";
		if (event.tag.size()) output << "[" << escape(TokenType::SectionTag, event.tag) << "]";
		if (event.comment.size()) output << commentSeparatorForSections << escape(TokenType::SectionComment, event.comment);
		output << std::endl;
		hasContent = true;
	}

	void endSection() {
		output.flush();
	}

	void entry(const EntryEvent& event) {
		output << escape(TokenType::EntryKey, event.key);
		if (event.subKey.size()) output << "[" << escape(TokenType::EntrySubKey, event.subKey) << "]";
		output << keyValueSeparator << escape(TokenType::EntryValue, event.value);
		if (event.comment.size()) output << commentSeparatorForEntries << escape(TokenType::EntryComment, event.comment);
		output << std::endl;
		hasContent = true;
	}

	void comment(const CommentEvent& event) {
		output << commentSeparatorStandalone << escape(TokenType::StandaloneComment, event.comment);
		output << std::endl;
		hasContent = true;
	}

	void whitespace(const WhitespaceEvent& event) {
		for (wchar_t ch : event.whitespace) {
			if (ch == L' ') output << " ";
			else if (ch == L'\t') output << "\t";
			else if (ch == L'\n') output << "\n";
			else if (ch == L'\r'); // TODO: keep CR?
			else; // TODO: throw exception if whitespace contains unexpected data? (should not happen)
		}
		hasContent = true;
	}



};

}
}
}