author | František Kučera <franta-hg@frantovo.cz> |
Sat, 23 Apr 2022 11:52:41 +0200 | |
branch | v_0 |
changeset 25 | 58de33c1af03 |
parent 24 | d698d34baf9b |
permissions | -rw-r--r-- |
4
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
15
638f581903d0
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
4
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <memory> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <string> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <vector> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <iostream> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sstream> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <locale> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <codecvt> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
#include <relpipe/reader/typedefs.h> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
#include <relpipe/reader/TypeId.h> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
|
25
58de33c1af03
do not stop at null byte 0x00; replace them with configurable string (default '^@')
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
32 |
#include "Configuration.h" |
58de33c1af03
do not stop at null byte 0x00; replace them with configurable string (default '^@')
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
33 |
|
4
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
namespace relpipe { |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
namespace out { |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
namespace nullbyte { |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
using namespace relpipe; |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
using namespace relpipe::reader; |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
using namespace relpipe::reader::handlers; |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
|
11
7c92942ac1e8
fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
42 |
class NullByteHandler : public RelationalReaderStringHandler { |
4
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
private: |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
std::ostream& output; |
25
58de33c1af03
do not stop at null byte 0x00; replace them with configurable string (default '^@')
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
45 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings? |
58de33c1af03
do not stop at null byte 0x00; replace them with configurable string (default '^@')
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
46 |
Configuration configuration; |
58de33c1af03
do not stop at null byte 0x00; replace them with configurable string (default '^@')
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
47 |
std::string nullByteReplacement; |
4
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
public: |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
|
25
58de33c1af03
do not stop at null byte 0x00; replace them with configurable string (default '^@')
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
50 |
NullByteHandler(Configuration configuration, std::ostream& output) : configuration(configuration), output(output), nullByteReplacement(convertor.to_bytes(configuration.nullByteReplacement)) { |
4
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
} |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override { |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
} |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
void attribute(const string_t& value) override { |
23
b717a552e63f
do not stop at null byte 0x00; replace them with '@' I
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
58 |
const std::string octets = convertor.to_bytes(value); |
24
d698d34baf9b
do not stop at null byte 0x00; replace them with '@' II
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
59 |
for (char ch : octets) { |
d698d34baf9b
do not stop at null byte 0x00; replace them with '@' II
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
60 |
if (ch) output.put(ch); |
25
58de33c1af03
do not stop at null byte 0x00; replace them with configurable string (default '^@')
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
61 |
else output << nullByteReplacement; |
24
d698d34baf9b
do not stop at null byte 0x00; replace them with '@' II
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
62 |
} |
4
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
output.put(0); |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
} |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
void endOfPipe() { |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
output.flush(); |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
} |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
}; |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
|
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
} |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
} |
12ffbdbb3574
NullByteHandler: produces \0-separated values
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
} |