src/RecfileWriter.h
branchv_0
changeset 0 9005fdd81bca
equal deleted inserted replaced
-1:000000000000 0:9005fdd81bca
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2018 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, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <iostream>
       
    21 #include <sstream>
       
    22 #include <string>
       
    23 #include <vector>
       
    24 #include <codecvt>
       
    25 #include <locale>
       
    26 
       
    27 #include <relpipe/reader/typedefs.h>
       
    28 #include <relpipe/cli/RelpipeCLIException.h>
       
    29 
       
    30 using namespace std;
       
    31 
       
    32 namespace relpipe {
       
    33 namespace out {
       
    34 namespace recfile {
       
    35 
       
    36 using namespace relpipe::reader;
       
    37 
       
    38 /**
       
    39  * A simple library for writing ASN.1 BER encoded files.
       
    40  */
       
    41 class RecfileWriter {
       
    42 private:
       
    43 	ostream& output;
       
    44 	wstring_convert<codecvt_utf8<wchar_t>> convertor; // only UTF8String is supported
       
    45 	int sequenceLevel = 0;
       
    46 
       
    47 	void xxx_indent() {
       
    48 		// FIXME: remove
       
    49 		for (int i = 0; i < sequenceLevel; i++) output << "  ";
       
    50 	}
       
    51 
       
    52 public:
       
    53 
       
    54 	RecfileWriter(std::ostream& output) : output(output) {
       
    55 	}
       
    56 
       
    57 	virtual ~RecfileWriter() {
       
    58 		if (sequenceLevel) output << "Unable to close RecfileWriter because there are not ended sequences." << endl; // FIXME: better error handling
       
    59 		output.flush();
       
    60 	}
       
    61 
       
    62 	void writeStartSequence() {
       
    63 		xxx_indent();
       
    64 		output << "sequence start" << endl;
       
    65 
       
    66 		sequenceLevel++;
       
    67 	}
       
    68 
       
    69 	void writeEndSequence() {
       
    70 		if (sequenceLevel == 0) throw cli::RelpipeCLIException(L"Unable to writeEndSequence() if not sequence is currently started.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // should not happen, error in the program
       
    71 
       
    72 		sequenceLevel--;
       
    73 
       
    74 		xxx_indent();
       
    75 		output << "sequence end" << endl;
       
    76 	}
       
    77 
       
    78 	void writeBoolean(const boolean_t& value) {
       
    79 		xxx_indent();
       
    80 		output << "boolean" << (value ? "true" : "false") << endl;
       
    81 	}
       
    82 
       
    83 	void writeInteger(const integer_t& value) {
       
    84 		xxx_indent();
       
    85 		output << "integer: " << value << endl;
       
    86 	}
       
    87 
       
    88 	void writeString(const string_t& value) {
       
    89 		xxx_indent();
       
    90 		output << "string: " << convertor.to_bytes(value) << endl;
       
    91 	}
       
    92 
       
    93 };
       
    94 
       
    95 }
       
    96 }
       
    97 }