/src/libreoffice/include/sax/tools/converter.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #pragma once |
21 | | |
22 | | #include <sal/config.h> |
23 | | |
24 | | #include <optional> |
25 | | #include <type_traits> |
26 | | |
27 | | #include <sax/saxdllapi.h> |
28 | | |
29 | | #include <rtl/strbuf.hxx> |
30 | | #include <sal/types.h> |
31 | | #include <rtl/ustrbuf.hxx> |
32 | | #include <com/sun/star/util/MeasureUnit.hpp> |
33 | | #include <tools/color.hxx> |
34 | | |
35 | | namespace com::sun::star { |
36 | | namespace uno { |
37 | | class Any; |
38 | | } |
39 | | namespace util { |
40 | | struct Date; |
41 | | struct DateTime; |
42 | | struct Duration; |
43 | | } |
44 | | } |
45 | | |
46 | | namespace sax { |
47 | | |
48 | | /** the Converter converts values of various types from |
49 | | their internal representation to the textual form used in xml |
50 | | and back. |
51 | | |
52 | | All unit types are expressed as css::util::MeasureUnit |
53 | | */ |
54 | | |
55 | | |
56 | | class SAX_DLLPUBLIC Converter |
57 | | { |
58 | | public: |
59 | | /** convert string to measure using optional min and max values*/ |
60 | | static bool convertMeasure( sal_Int32& rValue, |
61 | | std::u16string_view rString, |
62 | | sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH, |
63 | | sal_Int32 nMin = SAL_MIN_INT32, |
64 | | sal_Int32 nMax = SAL_MAX_INT32 ); |
65 | | |
66 | | /** convert string to measure using optional min and max values*/ |
67 | | static bool convertMeasure( sal_Int32& rValue, |
68 | | std::string_view rString, |
69 | | sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH, |
70 | | sal_Int32 nMin = SAL_MIN_INT32, |
71 | | sal_Int32 nMax = SAL_MAX_INT32 ); |
72 | | |
73 | | /** convert measure to string */ |
74 | | static void convertMeasure( OUStringBuffer& rBuffer, |
75 | | sal_Int32 nMeasure, |
76 | | sal_Int16 SourceUnit, |
77 | | sal_Int16 nTargetUnit ); |
78 | | |
79 | | /** convert string to measure and unit pair */ |
80 | | static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit, |
81 | | std::u16string_view rString); |
82 | | |
83 | | /** convert string to measure and unit pair */ |
84 | | static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit, |
85 | | std::string_view rString); |
86 | | |
87 | | /** convert measure and unit pair to string */ |
88 | | static void convertMeasureUnit(OUStringBuffer& rBuffer, double dValue, |
89 | | std::optional<sal_Int16> nValueUnit); |
90 | | |
91 | | /** convert string to boolean */ |
92 | | static bool convertBool( bool& rBool, |
93 | | std::u16string_view rString ); |
94 | | |
95 | | /** convert string to boolean */ |
96 | | static bool convertBool( bool& rBool, |
97 | | std::string_view rString ); |
98 | | |
99 | | /** convert boolean to string */ |
100 | | static void convertBool( OUStringBuffer& rBuffer, |
101 | | bool bValue ); |
102 | | |
103 | | /** convert string to percent */ |
104 | | static bool convertPercent( sal_Int32& rValue, |
105 | | std::u16string_view rString ); |
106 | | |
107 | | /** convert string to percent */ |
108 | | static bool convertPercent( sal_Int32& rValue, |
109 | | std::string_view rString ); |
110 | | |
111 | | /** convert percent to string */ |
112 | | static void convertPercent( OUStringBuffer& rBuffer, |
113 | | sal_Int32 nValue ); |
114 | | |
115 | | /** convert string to pixel measure unit */ |
116 | | static bool convertMeasurePx( sal_Int32& rValue, |
117 | | std::u16string_view rString ); |
118 | | |
119 | | /** convert string to pixel measure unit */ |
120 | | static bool convertMeasurePx( sal_Int32& rValue, |
121 | | std::string_view rString ); |
122 | | |
123 | | /** convert pixel measure unit to string */ |
124 | | static void convertMeasurePx( OUStringBuffer& rBuffer, |
125 | | sal_Int32 nValue ); |
126 | | |
127 | | /** convert string to rgb color */ |
128 | | static bool convertColor( sal_Int32& rColor, |
129 | | std::u16string_view rValue ); |
130 | | static bool convertColor( sal_Int32& rColor, |
131 | | std::string_view rValue ); |
132 | | static bool convertColor( ::Color& rColor, |
133 | | std::u16string_view rValue ) |
134 | 16.2k | { |
135 | 16.2k | sal_Int32 n(rColor); |
136 | 16.2k | bool b = convertColor( n, rValue ); |
137 | 16.2k | if (b) rColor = Color(ColorTransparency, n); |
138 | 16.2k | return b; |
139 | 16.2k | } |
140 | | static bool convertColor( ::Color& rColor, |
141 | | std::string_view rValue ) |
142 | 4.18k | { |
143 | 4.18k | sal_Int32 n(rColor); |
144 | 4.18k | bool b = convertColor( n, rValue ); |
145 | 4.18k | if (b) rColor = Color(ColorTransparency, n); |
146 | 4.18k | return b; |
147 | 4.18k | } |
148 | | |
149 | | /** convert color to string */ |
150 | | static void convertColor( OUStringBuffer &rBuffer, |
151 | | sal_Int32 nColor ); |
152 | | static void convertColor( OUStringBuffer &rBuffer, |
153 | | ::Color nColor ) |
154 | 1.08k | { convertColor( rBuffer, sal_Int32(nColor) ); } |
155 | | |
156 | | /** convert string to number with optional min and max values */ |
157 | | static bool convertNumber( sal_Int32& rValue, |
158 | | std::u16string_view aString, |
159 | | sal_Int32 nMin = SAL_MIN_INT32, |
160 | | sal_Int32 nMax = SAL_MAX_INT32 ); |
161 | | |
162 | | /** convert string to number with optional min and max values */ |
163 | | static bool convertNumber( sal_Int32& rValue, |
164 | | std::string_view aString, |
165 | | sal_Int32 nMin = SAL_MIN_INT32, |
166 | | sal_Int32 nMax = SAL_MAX_INT32 ); |
167 | | |
168 | | /** convert string to number with optional min and max values */ |
169 | | static bool convertNumber64(sal_Int64& rValue, |
170 | | std::u16string_view aString, |
171 | | sal_Int64 nMin = SAL_MIN_INT64, |
172 | | sal_Int64 nMax = SAL_MAX_INT64); |
173 | | |
174 | | /** convert string to number with optional min and max values */ |
175 | | static bool convertNumber64(sal_Int64& rValue, |
176 | | std::string_view aString, |
177 | | sal_Int64 nMin = SAL_MIN_INT64, |
178 | | sal_Int64 nMax = SAL_MAX_INT64); |
179 | | |
180 | | /** convert double number to string (using ::rtl::math) and |
181 | | DO convert from source unit to target unit */ |
182 | | static void convertDouble( OUStringBuffer& rBuffer, |
183 | | double fNumber, |
184 | | bool bWriteUnits, |
185 | | sal_Int16 nSourceUnit, |
186 | | sal_Int16 nTargetUnit ); |
187 | | |
188 | | /** convert double number to string (using ::rtl::math) without unit conversion */ |
189 | | static void convertDouble( OUStringBuffer& rBuffer, double fNumber); |
190 | | |
191 | | /** convert string to double number (using ::rtl::math) and DO convert from |
192 | | source unit to target unit. */ |
193 | | static bool convertDouble( double& rValue, |
194 | | std::u16string_view rString, |
195 | | sal_Int16 nSourceUnit, |
196 | | sal_Int16 nTargetUnit ); |
197 | | |
198 | | /** convert string to double number (using ::rtl::math) and DO convert from |
199 | | source unit to target unit. */ |
200 | | static bool convertDouble( double& rValue, |
201 | | std::string_view rString, |
202 | | sal_Int16 nSourceUnit, |
203 | | sal_Int16 nTargetUnit ); |
204 | | |
205 | | /** convert string to double number (using ::rtl::math) without unit conversion */ |
206 | | static bool convertDouble(double& rValue, std::u16string_view rString, std::u16string_view* pRest = nullptr); |
207 | | |
208 | | /** convert string to double number (using ::rtl::math) without unit conversion */ |
209 | | static bool convertDouble(double& rValue, std::string_view rString, std::string_view* pRest = nullptr); |
210 | | |
211 | | /** convert number, 10th of degrees with range [0..3600] to SVG angle */ |
212 | | static void convert10thDegAngle(OUStringBuffer& rBuffer, sal_Int16 nAngle, |
213 | | const bool isWrongOOo10thDegAngle); |
214 | | |
215 | | /** convert SVG angle to number in 10th of degrees */ |
216 | | static bool convert10thDegAngle(sal_Int16& rAngle, std::u16string_view rString, |
217 | | bool isWrongOOo10thDegAngle); |
218 | | |
219 | | /** convert SVG angle to number in 10th of degrees */ |
220 | | static bool convert10thDegAngle(sal_Int16& rAngle, std::string_view rString, |
221 | | bool isWrongOOo10thDegAngle); |
222 | | |
223 | | /** convert SVG angle to number, in degrees, range [0..360] */ |
224 | | static bool convertAngle(double& rAngle, std::u16string_view rString); |
225 | | |
226 | | /** convert SVG angle to number, in degrees, range [0..360] */ |
227 | | static bool convertAngle(double& rAngle, std::string_view rString); |
228 | | |
229 | | /** convert double to XMLSchema-2 "duration" string; negative durations allowed */ |
230 | | static void convertDuration(OUStringBuffer& rBuffer, |
231 | | const double fTime); |
232 | | |
233 | | /** convert util::Duration to XMLSchema-2 "duration" string */ |
234 | | static void convertDuration(OUStringBuffer& rBuffer, |
235 | | const css::util::Duration& rDuration); |
236 | | |
237 | | /** convert XMLSchema-2 "duration" string to double; negative durations allowed */ |
238 | | static bool convertDuration(double & rfTime, |
239 | | std::string_view rString); |
240 | | |
241 | | /** convert XMLSchema-2 "duration" string to util::Duration */ |
242 | | static bool convertDuration(css::util::Duration& rDuration, |
243 | | std::u16string_view rString); |
244 | | |
245 | | /** convert XMLSchema-2 "duration" string to util::Duration */ |
246 | | static bool convertDuration(css::util::Duration& rDuration, |
247 | | std::string_view rString); |
248 | | |
249 | | /** convert util::Date to XMLSchema-2 "date" string */ |
250 | | static void convertDate( OUStringBuffer& rBuffer, |
251 | | const css::util::Date& rDate, |
252 | | sal_Int16 const* pTimeZoneOffset); |
253 | | |
254 | | /** convert util::DateTime to XMLSchema-2 "date" or "dateTime" string */ |
255 | | static void convertDateTime( OUStringBuffer& rBuffer, |
256 | | const css::util::DateTime& rDateTime, |
257 | | sal_Int16 const* pTimeZoneOffset, |
258 | | bool bAddTimeIf0AM = false ); |
259 | | |
260 | | /** convert util::DateTime to XMLSchema-2 "date" or "dateTime" string */ |
261 | | static void convertDateTime( OStringBuffer& rBuffer, |
262 | | const css::util::DateTime& rDateTime, |
263 | | sal_Int16 const* pTimeZoneOffset, |
264 | | bool bAddTimeIf0AM = false ); |
265 | | |
266 | | /** convert util::DateTime to XMLSchema-2 "time" or "dateTime" string */ |
267 | | static void convertTimeOrDateTime(OUStringBuffer& rBuffer, |
268 | | const css::util::DateTime& rDateTime); |
269 | | |
270 | | /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */ |
271 | | static bool parseDateTime( css::util::DateTime& rDateTime, |
272 | | std::u16string_view rString ); |
273 | | |
274 | | /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */ |
275 | | static bool parseDateTime( css::util::DateTime& rDateTime, |
276 | | std::string_view rString ); |
277 | | |
278 | | /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */ |
279 | | static bool parseTimeOrDateTime(css::util::DateTime& rDateTime, |
280 | | std::u16string_view rString); |
281 | | |
282 | | /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */ |
283 | | static bool parseTimeOrDateTime(css::util::DateTime& rDateTime, |
284 | | std::string_view rString); |
285 | | |
286 | | /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or |
287 | | util::Date */ |
288 | | static bool parseDateOrDateTime( |
289 | | css::util::Date * pDate, |
290 | | css::util::DateTime & rDateTime, |
291 | | bool & rbDateTime, |
292 | | std::optional<sal_Int16> * pTimeZoneOffset, |
293 | | std::u16string_view rString ); |
294 | | |
295 | | /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or |
296 | | util::Date */ |
297 | | static bool parseDateOrDateTime( |
298 | | css::util::Date * pDate, |
299 | | css::util::DateTime & rDateTime, |
300 | | bool & rbDateTime, |
301 | | std::optional<sal_Int16> * pTimeZoneOffset, |
302 | | std::string_view rString ); |
303 | | |
304 | | /** gets the position of the first comma after npos in the string |
305 | | rStr. Commas inside '"' pairs are not matched */ |
306 | | static sal_Int32 indexOfComma( std::u16string_view rStr, |
307 | | sal_Int32 nPos ); |
308 | | |
309 | | static double GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit); |
310 | | static double GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit); |
311 | | static sal_Int16 GetUnitFromString(std::u16string_view rString, sal_Int16 nDefaultUnit); |
312 | | static sal_Int16 GetUnitFromString(std::string_view rString, sal_Int16 nDefaultUnit); |
313 | | |
314 | | /** convert an Any to string (typesafe) */ |
315 | | static bool convertAny(OUStringBuffer& rsValue, |
316 | | OUStringBuffer& rsType , |
317 | | const css::uno::Any& rValue); |
318 | | |
319 | | /** convert specified byte sequence to xsd:hexBinary string **/ |
320 | | static void convertBytesToHexBinary(OUStringBuffer& rBuffer, const void* pBytes, |
321 | | sal_Int32 nBytes); |
322 | | |
323 | | template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0> |
324 | | static void convertNumberToHexBinary(OUStringBuffer& rBuffer, T n) |
325 | 0 | { |
326 | 0 | convertBytesToHexBinary(rBuffer, &n, sizeof(n)); |
327 | 0 | } |
328 | | |
329 | | }; |
330 | | |
331 | | } |
332 | | |
333 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |