|
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 "DataType.h" |
|
11 #include "RelpipeException.h" |
|
12 #include "IntegerDataType.h" |
|
13 |
|
14 using namespace std; |
|
15 |
|
16 namespace rp_prototype { |
|
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 StringDataType : public DataType<wstring> { |
|
24 private: |
|
25 IntegerDataType integerType; |
|
26 wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
27 public: |
|
28 |
|
29 StringDataType() : DataType<wstring>(DATA_TYPE_ID_STRING, DATA_TYPE_CODE_STRING) { |
|
30 } |
|
31 |
|
32 wstring readValue(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 vector<char> buf(length); |
|
37 input.read(buf.data(), length); |
|
38 if (input.good()) { |
|
39 return convertor.from_bytes(string(buf.data(), length)); |
|
40 } else { |
|
41 throw RelpipeException(L"Error while reading string from the stream.", EXIT_CODE_DATA_ERROR); |
|
42 } |
|
43 } |
|
44 |
|
45 void writeValue(ostream &output, const wstring &value) override { |
|
46 string s = convertor.to_bytes(value); |
|
47 integerType.writeValue(output, s.length()); |
|
48 output << s.c_str(); |
|
49 } |
|
50 |
|
51 wstring toValue(const wstring &stringValue) override { |
|
52 return stringValue; |
|
53 } |
|
54 |
|
55 wstring toString(const wstring &value) override { |
|
56 return value; |
|
57 } |
|
58 |
|
59 }; |
|
60 |
|
61 } |