src/types/StringDataTypeReader.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 19:45:24 +0200
branchv_0
changeset 42 3442e0d6caec
parent 30 ef99f5a6c9f6
permissions -rw-r--r--
fix license version: GNU LGPLv3 or GPLv2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     1
/**
30
ef99f5a6c9f6 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     2
 * Relational pipes (library)
29
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     4
 *
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
30
ef99f5a6c9f6 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     6
 * it under the terms of the:
ef99f5a6c9f6 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     7
 *  - GNU Lesser General Public License as published by the Free Software Foundation;
42
3442e0d6caec fix license version: GNU LGPLv3 or GPLv2
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
     8
 *    version 3 of the License or (at your option)
30
ef99f5a6c9f6 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     9
 *  - GNU General Public License as published by the Free Software Foundation;
42
3442e0d6caec fix license version: GNU LGPLv3 or GPLv2
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    10
 *    version 2 of the License.
29
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    11
 *
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    12
 * This program is distributed in the hope that it will be useful,
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    15
 * GNU General Public License for more details.
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    16
 *
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    17
 * You should have received a copy of the GNU General Public License
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    19
 */
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#pragma once
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <string>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <iostream>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <vector>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <locale>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <codecvt>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
12
2d7109286408 remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
#include <relpipe/protocol/constants.h>
2d7109286408 remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    30
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    31
#include "../../include/relpipe/reader/RelpipeReaderException.h"
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    32
#include "../../include/relpipe/reader/typedefs.h"
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    33
#include "../DataTypeReader.h"
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    35
namespace relpipe {
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    36
namespace reader {
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    37
namespace types {
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    38
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    39
using namespace relpipe::protocol;
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    40
using namespace relpipe::reader;
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
/**
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
 * The prototype does not recognize any encoding,
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
 * it just works with c++ strings in encoding default to given platform. 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
 * In the real implementation of relational pipes, there will be DataTypes for particular encodings.
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
 */
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    47
class StringDataTypeReader : public DataTypeReader<string_t> {
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
private:
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    49
	IntegerDataTypeReader integerType;
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    50
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
public:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    53
	StringDataTypeReader() : DataTypeReader<string_t>(TypeId::STRING, DATA_TYPE_CODE_STRING) {
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    56
	string_t readValue(std::istream &input) override {
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		integer_t length = integerType.readValue(input);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		// TODO: check maximum length of single field
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		// if (length > 4000) throw RelpipeException("data too long", EXIT_CODE_DATA_ERROR);
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    60
		std::vector<char> buf(length);
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		input.read(buf.data(), length);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		if (input.good()) {
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    63
			return convertor.from_bytes(std::string(buf.data(), length));
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		} else {
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    65
			throw RelpipeReaderException(L"Error while reading string from the stream.");
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    69
	string_t toString(const string_t &value) override {
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		return value;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
};
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    75
}
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    76
}
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
}