author | František Kučera <franta-hg@frantovo.cz> |
Tue, 20 Oct 2020 23:48:51 +0200 | |
branch | v_0 |
changeset 49 | 361696fb6434 |
parent 46 | 12c329f5524f |
child 52 | d22e9274fcad |
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 |
/** |
46
12c329f5524f
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
45
diff
changeset
|
41 |
* Signed variable-length integer. |
12c329f5524f
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
45
diff
changeset
|
42 |
* LEB128 |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
*/ |
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
44 |
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
|
45 |
private: |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
|
46
12c329f5524f
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
45
diff
changeset
|
47 |
octet_t readNextOctet(std::istream &input) { |
12c329f5524f
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
45
diff
changeset
|
48 |
octet_t value = input.get(); |
45
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
49 |
if (input.good()) return value; |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
50 |
else throw RelpipeReaderException(L"Unable to read next octet of the integer."); |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
53 |
public: |
7
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
|
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
55 |
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
|
56 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
58 |
integer_t readValue(std::istream &input) override { |
45
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
59 |
integer_t value = 0; |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
60 |
integer_t shift = 0; |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
61 |
octet_t octet; |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
62 |
do { |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
63 |
octet = readNextOctet(input); |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
64 |
value += integer_t(octet & 0x7F) << shift; |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
65 |
shift += 7; |
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
66 |
} while (octet >= 128); |
46
12c329f5524f
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
45
diff
changeset
|
67 |
|
12c329f5524f
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
45
diff
changeset
|
68 |
if ((shift < (sizeof (integer_t) * 8)) && (octet & 0x40)) |
12c329f5524f
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
45
diff
changeset
|
69 |
value |= (~0 << shift); |
45
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
70 |
return value; |
7
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 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
} |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
|
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
}; |
489e52138771
add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
|
8
c87e9c84f7aa
reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
79 |
} |
14
e8de089f95dd
refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
80 |
} |
45
24a506eb97b5
binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format
František Kučera <franta-hg@frantovo.cz>
parents:
42
diff
changeset
|
81 |
} |