/src/poco/Foundation/src/TextConverter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // TextConverter.cpp |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Text |
6 | | // Module: TextConverter |
7 | | // |
8 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | | // and Contributors. |
10 | | // |
11 | | // SPDX-License-Identifier: BSL-1.0 |
12 | | // |
13 | | |
14 | | |
15 | | #include "Poco/TextConverter.h" |
16 | | #include "Poco/TextIterator.h" |
17 | | #include "Poco/TextEncoding.h" |
18 | | |
19 | | |
20 | | namespace { |
21 | | int nullTransform(int ch) |
22 | 0 | { |
23 | 0 | return ch; |
24 | 0 | } |
25 | | } |
26 | | |
27 | | |
28 | | namespace Poco { |
29 | | |
30 | | |
31 | | TextConverter::TextConverter(const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): |
32 | | _inEncoding(inEncoding), |
33 | | _outEncoding(outEncoding), |
34 | | _defaultChar(defaultChar) |
35 | 0 | { |
36 | 0 | } |
37 | | |
38 | | |
39 | | TextConverter::~TextConverter() |
40 | 0 | { |
41 | 0 | } |
42 | | |
43 | | |
44 | | int TextConverter::convert(const std::string& source, std::string& destination, Transform trans) |
45 | 0 | { |
46 | 0 | int errors = 0; |
47 | 0 | TextIterator it(source, _inEncoding); |
48 | 0 | TextIterator end(source); |
49 | 0 | unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH]; |
50 | |
|
51 | 0 | while (it != end) |
52 | 0 | { |
53 | 0 | int c = *it; |
54 | 0 | if (c == -1) { ++errors; c = _defaultChar; } |
55 | 0 | c = trans(c); |
56 | 0 | int n = _outEncoding.convert(c, buffer, sizeof(buffer)); |
57 | 0 | if (n == 0) n = _outEncoding.convert(_defaultChar, buffer, sizeof(buffer)); |
58 | 0 | poco_assert (n <= sizeof(buffer)); |
59 | 0 | destination.append((const char*) buffer, n); |
60 | 0 | ++it; |
61 | 0 | } |
62 | 0 | return errors; |
63 | 0 | } |
64 | | |
65 | | |
66 | | int TextConverter::convert(const void* source, int length, std::string& destination, Transform trans) |
67 | 0 | { |
68 | 0 | poco_check_ptr (source); |
69 | |
|
70 | 0 | int errors = 0; |
71 | 0 | const unsigned char* it = (const unsigned char*) source; |
72 | 0 | const unsigned char* end = (const unsigned char*) source + length; |
73 | 0 | unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH]; |
74 | |
|
75 | 0 | while (it < end) |
76 | 0 | { |
77 | 0 | int n = _inEncoding.queryConvert(it, 1); |
78 | 0 | int uc; |
79 | 0 | int read = 1; |
80 | |
|
81 | 0 | while (-1 > n && (end - it) >= -n) |
82 | 0 | { |
83 | 0 | read = -n; |
84 | 0 | n = _inEncoding.queryConvert(it, read); |
85 | 0 | } |
86 | |
|
87 | 0 | if (-1 > n) |
88 | 0 | { |
89 | 0 | it = end; |
90 | 0 | } |
91 | 0 | else |
92 | 0 | { |
93 | 0 | it += read; |
94 | 0 | } |
95 | |
|
96 | 0 | if (-1 >= n) |
97 | 0 | { |
98 | 0 | uc = _defaultChar; |
99 | 0 | ++errors; |
100 | 0 | } |
101 | 0 | else |
102 | 0 | { |
103 | 0 | uc = n; |
104 | 0 | } |
105 | |
|
106 | 0 | uc = trans(uc); |
107 | 0 | n = _outEncoding.convert(uc, buffer, sizeof(buffer)); |
108 | 0 | if (n == 0) n = _outEncoding.convert(_defaultChar, buffer, sizeof(buffer)); |
109 | 0 | poco_assert (n <= sizeof(buffer)); |
110 | 0 | destination.append((const char*) buffer, n); |
111 | 0 | } |
112 | 0 | return errors; |
113 | 0 | } |
114 | | |
115 | | |
116 | | int TextConverter::convert(const std::string& source, std::string& destination) |
117 | 0 | { |
118 | 0 | return convert(source, destination, nullTransform); |
119 | 0 | } |
120 | | |
121 | | |
122 | | int TextConverter::convert(const void* source, int length, std::string& destination) |
123 | 0 | { |
124 | 0 | return convert(source, length, destination, nullTransform); |
125 | 0 | } |
126 | | |
127 | | |
128 | | } // namespace Poco |