src/lib/INIReader.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 21 Nov 2020 18:26:39 +0100
branchv_0
changeset 0 16c7fa9b7c49
child 1 3876a9c56a66
permissions -rw-r--r--
project and parser skeleton + output demo data

/**
 * 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/>.
 */

#include <vector>

#include "INIReader.h"

class INIReaderImpl : public INIReader {
private:
	std::istream& input;
	std::vector<INIContentHandler*> handlers;
public:

	INIReaderImpl(std::istream& input) : input(input) {
	}

	void addHandler(INIContentHandler* handler) override {
		handlers.push_back(handler);
	}

	void process() override {
		
		// TODO: real parser instead of demo data
		for (INIContentHandler* handler : handlers) {
			handler->startDocument();
			
			handler->entry("key-0", "outside sections");
			
			handler->startSection("section-1");
			handler->entry("key-1", "in section 1");
			handler->entry("key-2", "in section 1");
			handler->entry("key-3", "in section 1");
			
			handler->startSection("nested-section-1-1");
			handler->entry("key-1", "in nested section 1-1");
			handler->entry("key-2", "in nested section 1-1");
			handler->endSection();
			
			handler->endSection();
			
			handler->startSection("section-2");
			handler->entry("key-1", "in section 2");
			handler->endSection();
			
			handler->entry("key-666", "outside sections again; this normally would not happen, but should be supported");
			
			handler->endDocument();
		}
		
	}
};

INIReader* INIReader::create(std::istream& input) {
	return new INIReaderImpl(input);
}