author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Oct 2019 19:45:24 +0200 | |
branch | v_0 |
changeset 42 | 3442e0d6caec |
parent 30 | ef99f5a6c9f6 |
child 45 | 24a506eb97b5 |
permissions | -rw-r--r-- |
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 <iostream> |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <cassert> |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <limits> |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
12
2d7109286408
remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
27 |
#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
|
28 |
|
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
29 |
#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
|
30 |
#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
|
31 |
#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
|
32 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
33 |
namespace relpipe { |
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
34 |
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
|
35 |
namespace types { |
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
36 |
|
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
|
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
/** |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
* The prototype does not have various integer and other numeric data types, |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
* it just works with one type of integer. |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
* But this integer has variable length -- smaller values occupy only one byte, bigger ones, more bytes 1,2,4,8 + first byte (contains length signalization). |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
* In the real implementation of relational pipes, there will be DataTypes for particular numeric types. |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
* |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
* TODO: support also big endian architectures. |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
* TODO: throw exception if a value was stored in bigger type than needed (while reading – there should be only one supported way how to encode a single value) |
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 |
* Example of encoded values: |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
* ------------------------------------------------------------------------------------------------- |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
* $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do printf '%20s = ' $n; dist/Debug/GNU-Linux/rp-prototype write integer $n | hd | head -n 1; done |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
* 0 = 00000000 00 |.| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
* 1 = 00000000 01 |.| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
* 10 = 00000000 0a |.| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
* 250 = 00000000 fa |.| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
* 251 = 00000000 fb fb |..| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
* 252 = 00000000 fb fc |..| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
* 65535 = 00000000 fc ff ff |...| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
* 65536 = 00000000 fd 00 00 01 00 |.....| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
* 4294967295 = 00000000 fd ff ff ff ff |.....| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
* 4294967296 = 00000000 fe 00 00 00 00 01 00 00 00 |.........| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
* 18446744073709551615 = 00000000 fe ff ff ff ff ff ff ff ff |.........| |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
* ------------------------------------------------------------------------------------------------- |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
* |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
* Example of decoded values: |
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 |
* $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do dist/Debug/GNU-Linux/rp-prototype write integer $n | dist/Debug/GNU-Linux/rp-prototype read integer; done; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
* 0 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
* 1 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
* 10 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
* 250 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
* 251 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
* 252 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
* 65535 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
* 65536 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
* 4294967295 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
* 4294967296 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
* 18446744073709551615 |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
* ------------------------------------------------------------------------------------------------- |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
* |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
* Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
* |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
*/ |
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
84 |
class IntegerDataTypeReader : public DataTypeReader<integer_t> { |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
private: |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
static const uint8_t INTEGER_TYPE_UINT8 = 251; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
static const uint8_t INTEGER_TYPE_UINT16 = 252; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
static const uint8_t INTEGER_TYPE_UINT32 = 253; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
static const uint8_t INTEGER_TYPE_UINT64 = 254; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
static const uint8_t INTEGER_TYPE_RESERVED = 255; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
92 |
template<typename T> integer_t read(std::istream &input) { |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
T value = 0; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
input.read(reinterpret_cast<char *> (&value), sizeof (value)); |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
return value; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
98 |
public: |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
|
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
100 |
IntegerDataTypeReader() : DataTypeReader<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) { |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
103 |
integer_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
|
104 |
uint8_t first; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
input.read(reinterpret_cast<char *> (&first), sizeof (first)); |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
if (input.good()) { |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
|
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
if (first < INTEGER_TYPE_UINT8) return first; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
else if (first == INTEGER_TYPE_UINT8) return read<uint8_t>(input); |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
110 |
else if (first == INTEGER_TYPE_UINT16) return read<uint16_t>(input); |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
111 |
else if (first == INTEGER_TYPE_UINT32) return read<uint32_t>(input); |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
112 |
else if (first == INTEGER_TYPE_UINT64) return read<uint64_t>(input); |
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
113 |
else throw RelpipeReaderException(L"Error while parsing integer type: unsupported type"); |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
114 |
} else { |
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
115 |
throw RelpipeReaderException(L"Error while reading integer 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
|
116 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
117 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
119 |
string_t toString(const integer_t &value) override { |
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
120 |
return std::to_wstring(value); |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
|
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
123 |
}; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
124 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
125 |
} |
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
126 |
} |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
127 |
} |