src/lib/INIReader.cpp
branchv_0
changeset 0 16c7fa9b7c49
child 1 3876a9c56a66
equal deleted inserted replaced
-1:000000000000 0:16c7fa9b7c49
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include <vector>
       
    19 
       
    20 #include "INIReader.h"
       
    21 
       
    22 class INIReaderImpl : public INIReader {
       
    23 private:
       
    24 	std::istream& input;
       
    25 	std::vector<INIContentHandler*> handlers;
       
    26 public:
       
    27 
       
    28 	INIReaderImpl(std::istream& input) : input(input) {
       
    29 	}
       
    30 
       
    31 	void addHandler(INIContentHandler* handler) override {
       
    32 		handlers.push_back(handler);
       
    33 	}
       
    34 
       
    35 	void process() override {
       
    36 		
       
    37 		// TODO: real parser instead of demo data
       
    38 		for (INIContentHandler* handler : handlers) {
       
    39 			handler->startDocument();
       
    40 			
       
    41 			handler->entry("key-0", "outside sections");
       
    42 			
       
    43 			handler->startSection("section-1");
       
    44 			handler->entry("key-1", "in section 1");
       
    45 			handler->entry("key-2", "in section 1");
       
    46 			handler->entry("key-3", "in section 1");
       
    47 			
       
    48 			handler->startSection("nested-section-1-1");
       
    49 			handler->entry("key-1", "in nested section 1-1");
       
    50 			handler->entry("key-2", "in nested section 1-1");
       
    51 			handler->endSection();
       
    52 			
       
    53 			handler->endSection();
       
    54 			
       
    55 			handler->startSection("section-2");
       
    56 			handler->entry("key-1", "in section 2");
       
    57 			handler->endSection();
       
    58 			
       
    59 			handler->entry("key-666", "outside sections again; this normally would not happen, but should be supported");
       
    60 			
       
    61 			handler->endDocument();
       
    62 		}
       
    63 		
       
    64 	}
       
    65 };
       
    66 
       
    67 INIReader* INIReader::create(std::istream& input) {
       
    68 	return new INIReaderImpl(input);
       
    69 }