src/StringDataTypeReader.h
branchv_0
changeset 8 c87e9c84f7aa
parent 7 489e52138771
child 9 517888868e55
equal deleted inserted replaced
7:489e52138771 8:c87e9c84f7aa
       
     1 #pragma once
       
     2 
       
     3 #include <string>
       
     4 #include <sstream>
       
     5 #include <iostream>
       
     6 #include <vector>
       
     7 #include <locale>
       
     8 #include <codecvt>
       
     9 
       
    10 #include "../include/typedefs.h"
       
    11 #include "../include/DataTypeReader.h"
       
    12 #include "../include/DataTypeReader.h"
       
    13 #include "format.h"
       
    14 
       
    15 namespace relpipe {
       
    16 namespace reader {
       
    17 
       
    18 /**
       
    19  * The prototype does not recognize any encoding,
       
    20  * it just works with c++ strings in encoding default to given platform. 
       
    21  * In the real implementation of relational pipes, there will be DataTypes for particular encodings.
       
    22  */
       
    23 class StringDataTypeReader : public DataTypeReader<string_t> {
       
    24 private:
       
    25 	IntegerDataTypeReader integerType;
       
    26 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    27 public:
       
    28 
       
    29 	StringDataTypeReader() : DataTypeReader<string_t>(DATA_TYPE_ID_STRING, DATA_TYPE_CODE_STRING) {
       
    30 	}
       
    31 
       
    32 	string_t readValue(std::istream &input) override {
       
    33 		integer_t length = integerType.readValue(input);
       
    34 		// TODO: check maximum length of single field
       
    35 		// if (length > 4000) throw RelpipeException("data too long", EXIT_CODE_DATA_ERROR);
       
    36 		std::vector<char> buf(length);
       
    37 		input.read(buf.data(), length);
       
    38 		if (input.good()) {
       
    39 			return convertor.from_bytes(std::string(buf.data(), length));
       
    40 		} else {
       
    41 			throw RelpipeReaderException(L"Error while reading string from the stream.");
       
    42 		}
       
    43 	}
       
    44 
       
    45 	string_t toString(const string_t &value) override {
       
    46 		return value;
       
    47 	}
       
    48 
       
    49 };
       
    50 
       
    51 }
       
    52 }