Line |
Branch |
Exec |
Source |
1 |
|
|
/* GATE PROJECT LICENSE: |
2 |
|
|
+----------------------------------------------------------------------------+ |
3 |
|
|
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | |
4 |
|
|
| All rights reserved. | |
5 |
|
|
| | |
6 |
|
|
| Redistribution and use in source and binary forms, with or without | |
7 |
|
|
| modification, are permitted provided that the following conditions are met:| |
8 |
|
|
| | |
9 |
|
|
| 1. Redistributions of source code must retain the above copyright notice, | |
10 |
|
|
| this list of conditions and the following disclaimer. | |
11 |
|
|
| 2. Redistributions in binary form must reproduce the above copyright | |
12 |
|
|
| notice, this list of conditions and the following disclaimer in the | |
13 |
|
|
| documentation and/or other materials provided with the distribution. | |
14 |
|
|
| | |
15 |
|
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| |
16 |
|
|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 |
|
|
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 |
|
|
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
19 |
|
|
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 |
|
|
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 |
|
|
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 |
|
|
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 |
|
|
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 |
|
|
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
25 |
|
|
| THE POSSIBILITY OF SUCH DAMAGE. | |
26 |
|
|
+----------------------------------------------------------------------------+ |
27 |
|
|
*/ |
28 |
|
|
#include "gate/encode/texts.hpp" |
29 |
|
|
#include "gate/exceptions.hpp" |
30 |
|
|
#include "gate/encode/texts.h" |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
namespace gate |
34 |
|
|
{ |
35 |
|
|
namespace enc |
36 |
|
|
{ |
37 |
|
|
|
38 |
|
✗ |
size_t Text::detectBomType(char const* data, size_t datalen, BomTypeEnum& bomtype) |
39 |
|
|
{ |
40 |
|
✗ |
unsigned int tp = 0; |
41 |
|
✗ |
gate_size_t ret = gate_text_detect_bom(data, datalen, &tp); |
42 |
|
✗ |
bomtype = (BomTypeEnum)tp; |
43 |
|
✗ |
return ret; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
✗ |
void Text::loadUtf8(Stream& src, StringBuilder& dst) |
47 |
|
|
{ |
48 |
|
✗ |
result_t res = gate_text_load_utf8(src.c_impl(), dst.c_impl()); |
49 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
50 |
|
✗ |
} |
51 |
|
✗ |
void Text::loadAnsi(Stream& src, StringBuilder& dst) |
52 |
|
|
{ |
53 |
|
✗ |
result_t res = gate_text_load_ansi(src.c_impl(), dst.c_impl()); |
54 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
55 |
|
✗ |
} |
56 |
|
✗ |
void Text::loadUtf16LE(Stream& src, StringBuilder& dst) |
57 |
|
|
{ |
58 |
|
✗ |
result_t res = gate_text_load_utf16le(src.c_impl(), dst.c_impl()); |
59 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
60 |
|
✗ |
} |
61 |
|
✗ |
void Text::loadUtf16BE(Stream& src, StringBuilder& dst) |
62 |
|
|
{ |
63 |
|
✗ |
result_t res = gate_text_load_utf16be(src.c_impl(), dst.c_impl()); |
64 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
65 |
|
✗ |
} |
66 |
|
✗ |
void Text::loadUtf32LE(Stream& src, StringBuilder& dst) |
67 |
|
|
{ |
68 |
|
✗ |
result_t res = gate_text_load_utf32le(src.c_impl(), dst.c_impl()); |
69 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
70 |
|
✗ |
} |
71 |
|
✗ |
void Text::loadUtf32BE(Stream& src, StringBuilder& dst) |
72 |
|
|
{ |
73 |
|
✗ |
result_t res = gate_text_load_utf32be(src.c_impl(), dst.c_impl()); |
74 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
75 |
|
✗ |
} |
76 |
|
|
|
77 |
|
|
|
78 |
|
✗ |
void Text::saveUtf8(String const& src, Stream& dst, bool addbom) |
79 |
|
|
{ |
80 |
|
✗ |
result_t res = gate_text_save_utf8(src.c_impl(), dst.c_impl(), addbom); |
81 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
82 |
|
✗ |
} |
83 |
|
✗ |
void Text::saveAnsi(String const& src, Stream& dst) |
84 |
|
|
{ |
85 |
|
✗ |
result_t res = gate_text_save_ansi(src.c_impl(), dst.c_impl()); |
86 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
87 |
|
✗ |
} |
88 |
|
✗ |
void Text::saveUtf16LE(String const& src, Stream& dst, bool addbom) |
89 |
|
|
{ |
90 |
|
✗ |
result_t res = gate_text_save_utf16le(src.c_impl(), dst.c_impl(), addbom); |
91 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
92 |
|
✗ |
} |
93 |
|
✗ |
void Text::saveUtf16BE(String const& src, Stream& dst, bool addbom) |
94 |
|
|
{ |
95 |
|
✗ |
result_t res = gate_text_save_utf16be(src.c_impl(), dst.c_impl(), addbom); |
96 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
97 |
|
✗ |
} |
98 |
|
✗ |
void Text::saveUtf32LE(String const& src, Stream& dst, bool addbom) |
99 |
|
|
{ |
100 |
|
✗ |
result_t res = gate_text_save_utf32le(src.c_impl(), dst.c_impl(), addbom); |
101 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
102 |
|
✗ |
} |
103 |
|
✗ |
void Text::saveUtf32BE(String const& src, Stream& dst, bool addbom) |
104 |
|
|
{ |
105 |
|
✗ |
result_t res = gate_text_save_utf32be(src.c_impl(), dst.c_impl(), addbom); |
106 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
107 |
|
✗ |
} |
108 |
|
|
|
109 |
|
✗ |
void Text::escapeC(String const& src, StringBuilder& dst) |
110 |
|
|
{ |
111 |
|
✗ |
result_t res = gate_text_c_escape(src.c_impl(), dst.c_impl()); |
112 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
113 |
|
✗ |
} |
114 |
|
✗ |
String Text::escapeC(String const& src) |
115 |
|
|
{ |
116 |
|
✗ |
StringBuilder builder; |
117 |
|
✗ |
escapeC(src, builder); |
118 |
|
✗ |
return builder.toString(); |
119 |
|
|
} |
120 |
|
✗ |
void Text::unescapeC(String const& src, StringBuilder& dst) |
121 |
|
|
{ |
122 |
|
✗ |
result_t res = gate_text_c_unescape(src.c_impl(), dst.c_impl()); |
123 |
|
✗ |
GATEXX_CHECK_EXCEPTION(res); |
124 |
|
✗ |
} |
125 |
|
✗ |
String Text::unescapeC(String const& src) |
126 |
|
|
{ |
127 |
|
✗ |
StringBuilder builder; |
128 |
|
✗ |
unescapeC(src, builder); |
129 |
|
✗ |
return builder.toString(); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
✗ |
TextFormat::TextFormat(String const& format) |
138 |
|
|
{ |
139 |
|
✗ |
result_t res = gate_text_format_create(&this->impl, format.c_impl()); |
140 |
|
✗ |
GATEXX_CHECK_ERROR(res); |
141 |
|
✗ |
} |
142 |
|
✗ |
TextFormat::~TextFormat() |
143 |
|
|
{ |
144 |
|
✗ |
gate_text_format_destroy(&this->impl); |
145 |
|
✗ |
} |
146 |
|
|
|
147 |
|
✗ |
void TextFormat::addText(String const& text) |
148 |
|
|
{ |
149 |
|
✗ |
result_t res = gate_text_format_text(&this->impl, text.c_impl()); |
150 |
|
✗ |
GATEXX_CHECK_ERROR(res); |
151 |
|
✗ |
} |
152 |
|
✗ |
void TextFormat::addInt(gate_int64_t num) |
153 |
|
|
{ |
154 |
|
✗ |
result_t res = gate_text_format_int(&this->impl, num); |
155 |
|
✗ |
GATEXX_CHECK_ERROR(res); |
156 |
|
✗ |
} |
157 |
|
✗ |
void TextFormat::addReal(gate_real64_t num) |
158 |
|
|
{ |
159 |
|
✗ |
result_t res = gate_text_format_real(&this->impl, num); |
160 |
|
✗ |
GATEXX_CHECK_ERROR(res); |
161 |
|
✗ |
} |
162 |
|
|
|
163 |
|
✗ |
String TextFormat::toString() |
164 |
|
|
{ |
165 |
|
✗ |
gate_string_t tmp = GATE_STRING_INIT_EMPTY; |
166 |
|
✗ |
result_t res = gate_text_format_print(&this->impl, &tmp); |
167 |
|
✗ |
GATEXX_CHECK_ERROR(res); |
168 |
|
✗ |
return String::createFrom(tmp); |
169 |
|
|
} |
170 |
|
✗ |
String TextFormat::toString() const |
171 |
|
|
{ |
172 |
|
✗ |
gate_string_t tmp = GATE_STRING_INIT_EMPTY; |
173 |
|
✗ |
result_t res = gate_text_format_extract(&this->impl, &tmp); |
174 |
|
✗ |
GATEXX_CHECK_ERROR(res); |
175 |
|
✗ |
return String::createFrom(tmp); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
✗ |
TextFormat::operator String() |
179 |
|
|
{ |
180 |
|
✗ |
return this->toString(); |
181 |
|
|
} |
182 |
|
✗ |
TextFormat::operator String() const |
183 |
|
|
{ |
184 |
|
✗ |
return this->toString(); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
|
188 |
|
✗ |
TextFormat& TextFormat::operator%(String const& text) |
189 |
|
|
{ |
190 |
|
✗ |
this->addText(text); |
191 |
|
✗ |
return *this; |
192 |
|
|
} |
193 |
|
✗ |
TextFormat& TextFormat::operator%(int8_t const& num) |
194 |
|
|
{ |
195 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
196 |
|
✗ |
return *this; |
197 |
|
|
} |
198 |
|
✗ |
TextFormat& TextFormat::operator%(int16_t const& num) |
199 |
|
|
{ |
200 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
201 |
|
✗ |
return *this; |
202 |
|
|
} |
203 |
|
✗ |
TextFormat& TextFormat::operator%(int32_t const& num) |
204 |
|
|
{ |
205 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
206 |
|
✗ |
return *this; |
207 |
|
|
} |
208 |
|
✗ |
TextFormat& TextFormat::operator%(int64_t const& num) |
209 |
|
|
{ |
210 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
211 |
|
✗ |
return *this; |
212 |
|
|
} |
213 |
|
✗ |
TextFormat& TextFormat::operator%(uint8_t const& num) |
214 |
|
|
{ |
215 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
216 |
|
✗ |
return *this; |
217 |
|
|
} |
218 |
|
✗ |
TextFormat& TextFormat::operator%(uint16_t const& num) |
219 |
|
|
{ |
220 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
221 |
|
✗ |
return *this; |
222 |
|
|
} |
223 |
|
✗ |
TextFormat& TextFormat::operator%(uint32_t const& num) |
224 |
|
|
{ |
225 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
226 |
|
✗ |
return *this; |
227 |
|
|
} |
228 |
|
✗ |
TextFormat& TextFormat::operator%(uint64_t const& num) |
229 |
|
|
{ |
230 |
|
✗ |
this->addInt(static_cast<int64_t>(num)); |
231 |
|
✗ |
return *this; |
232 |
|
|
} |
233 |
|
✗ |
TextFormat& TextFormat::operator%(real32_t const& real) |
234 |
|
|
{ |
235 |
|
✗ |
this->addReal(static_cast<real64_t>(real)); |
236 |
|
✗ |
return *this; |
237 |
|
|
} |
238 |
|
✗ |
TextFormat& TextFormat::operator%(real64_t const& real) |
239 |
|
|
{ |
240 |
|
✗ |
this->addReal(static_cast<real64_t>(real)); |
241 |
|
✗ |
return *this; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
✗ |
gate_text_formater_t* TextFormat::c_impl() |
245 |
|
|
{ |
246 |
|
✗ |
return &this->impl; |
247 |
|
|
} |
248 |
|
✗ |
gate_text_formater_t const* TextFormat::c_impl() const |
249 |
|
|
{ |
250 |
|
✗ |
return &this->impl; |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
|
254 |
|
|
} // end of namespace enc |
255 |
|
|
} // end of namespace gate |
256 |
|
|
|