src/types/StringDataTypeReader.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 25 Aug 2018 18:16:53 +0200
branchv_0
changeset 14 e8de089f95dd
parent 12 src/StringDataTypeReader.h@2d7109286408
child 29 755978b0935c
permissions -rw-r--r--
refactoring, move files, use TypeId instead of integer_t
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#pragma once
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <string>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
#include <sstream>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#include <iostream>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
#include <vector>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
#include <locale>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
#include <codecvt>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
12
2d7109286408 remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    10
#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
    11
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    12
#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
    13
#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
    14
#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
    15
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    16
namespace relpipe {
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    17
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
    18
namespace types {
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    19
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    20
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
    21
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
    22
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
/**
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
 * 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
    25
 * 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
    26
 * 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
    27
 */
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    28
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
    29
private:
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    30
	IntegerDataTypeReader integerType;
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    31
	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
    32
public:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    34
	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
    35
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    37
	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
    38
		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
    39
		// 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
    40
		// 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
    41
		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
    42
		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
    43
		if (input.good()) {
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    44
			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
    45
		} else {
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    46
			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
    47
		}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    50
	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
    51
		return value;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
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
}
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    57
}
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
}