Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / sax / tools / converter.hxx
blobd797eace304224d3051e4782ac3b6c77501d2aaf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SAX_TOOLS_CONVERTER_HXX
21 #define INCLUDED_SAX_TOOLS_CONVERTER_HXX
23 #include <sal/config.h>
25 #include <optional>
26 #include <type_traits>
28 #include <sax/saxdllapi.h>
30 #include <rtl/strbuf.hxx>
31 #include <sal/types.h>
32 #include <rtl/ustrbuf.hxx>
33 #include <com/sun/star/util/MeasureUnit.hpp>
34 #include <tools/color.hxx>
35 #include <unotools/saveopt.hxx>
37 namespace com::sun::star {
38 namespace uno {
39 class Any;
41 namespace util {
42 struct Date;
43 struct DateTime;
44 struct Duration;
48 namespace sax {
50 /** the Converter converts values of various types from
51 their internal representation to the textual form used in xml
52 and back.
54 All unit types are expressed as css::util::MeasureUnit
58 class SAX_DLLPUBLIC Converter
60 public:
61 /** convert string to measure using optional min and max values*/
62 static bool convertMeasure( sal_Int32& rValue,
63 std::u16string_view rString,
64 sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH,
65 sal_Int32 nMin = SAL_MIN_INT32,
66 sal_Int32 nMax = SAL_MAX_INT32 );
68 /** convert string to measure using optional min and max values*/
69 static bool convertMeasure( sal_Int32& rValue,
70 std::string_view rString,
71 sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH,
72 sal_Int32 nMin = SAL_MIN_INT32,
73 sal_Int32 nMax = SAL_MAX_INT32 );
75 /** convert measure to string */
76 static void convertMeasure( OUStringBuffer& rBuffer,
77 sal_Int32 nMeasure,
78 sal_Int16 SourceUnit,
79 sal_Int16 nTargetUnit );
81 /** convert string to boolean */
82 static bool convertBool( bool& rBool,
83 std::u16string_view rString );
85 /** convert string to boolean */
86 static bool convertBool( bool& rBool,
87 std::string_view rString );
89 /** convert boolean to string */
90 static void convertBool( OUStringBuffer& rBuffer,
91 bool bValue );
93 /** convert string to percent */
94 static bool convertPercent( sal_Int32& rValue,
95 std::u16string_view rString );
97 /** convert string to percent */
98 static bool convertPercent( sal_Int32& rValue,
99 std::string_view rString );
101 /** convert percent to string */
102 static void convertPercent( OUStringBuffer& rBuffer,
103 sal_Int32 nValue );
105 /** convert string to pixel measure unit */
106 static bool convertMeasurePx( sal_Int32& rValue,
107 std::u16string_view rString );
109 /** convert string to pixel measure unit */
110 static bool convertMeasurePx( sal_Int32& rValue,
111 std::string_view rString );
113 /** convert pixel measure unit to string */
114 static void convertMeasurePx( OUStringBuffer& rBuffer,
115 sal_Int32 nValue );
117 /** convert string to rgb color */
118 static bool convertColor( sal_Int32& rColor,
119 std::u16string_view rValue );
120 static bool convertColor( sal_Int32& rColor,
121 std::string_view rValue );
122 static bool convertColor( ::Color& rColor,
123 std::u16string_view rValue )
125 sal_Int32 n(rColor);
126 bool b = convertColor( n, rValue );
127 if (b) rColor = Color(ColorTransparency, n);
128 return b;
130 static bool convertColor( ::Color& rColor,
131 std::string_view rValue )
133 sal_Int32 n(rColor);
134 bool b = convertColor( n, rValue );
135 if (b) rColor = Color(ColorTransparency, n);
136 return b;
139 /** convert color to string */
140 static void convertColor( OUStringBuffer &rBuffer,
141 sal_Int32 nColor );
142 static void convertColor( OUStringBuffer &rBuffer,
143 ::Color nColor )
144 { convertColor( rBuffer, sal_Int32(nColor) ); }
146 /** convert string to number with optional min and max values */
147 static bool convertNumber( sal_Int32& rValue,
148 std::u16string_view aString,
149 sal_Int32 nMin = SAL_MIN_INT32,
150 sal_Int32 nMax = SAL_MAX_INT32 );
152 /** convert string to number with optional min and max values */
153 static bool convertNumber( sal_Int32& rValue,
154 std::string_view aString,
155 sal_Int32 nMin = SAL_MIN_INT32,
156 sal_Int32 nMax = SAL_MAX_INT32 );
158 /** convert string to number with optional min and max values */
159 static bool convertNumber64(sal_Int64& rValue,
160 std::u16string_view aString,
161 sal_Int64 nMin = SAL_MIN_INT64,
162 sal_Int64 nMax = SAL_MAX_INT64);
164 /** convert string to number with optional min and max values */
165 static bool convertNumber64(sal_Int64& rValue,
166 std::string_view aString,
167 sal_Int64 nMin = SAL_MIN_INT64,
168 sal_Int64 nMax = SAL_MAX_INT64);
170 /** convert double number to string (using ::rtl::math) and
171 DO convert from source unit to target unit */
172 static void convertDouble( OUStringBuffer& rBuffer,
173 double fNumber,
174 bool bWriteUnits,
175 sal_Int16 nSourceUnit,
176 sal_Int16 nTargetUnit );
178 /** convert double number to string (using ::rtl::math) without unit conversion */
179 static void convertDouble( OUStringBuffer& rBuffer, double fNumber);
181 /** convert string to double number (using ::rtl::math) and DO convert from
182 source unit to target unit. */
183 static bool convertDouble( double& rValue,
184 std::u16string_view rString,
185 sal_Int16 nSourceUnit,
186 sal_Int16 nTargetUnit );
188 /** convert string to double number (using ::rtl::math) and DO convert from
189 source unit to target unit. */
190 static bool convertDouble( double& rValue,
191 std::string_view rString,
192 sal_Int16 nSourceUnit,
193 sal_Int16 nTargetUnit );
195 /** convert string to double number (using ::rtl::math) without unit conversion */
196 static bool convertDouble(double& rValue, std::u16string_view rString);
198 /** convert string to double number (using ::rtl::math) without unit conversion */
199 static bool convertDouble(double& rValue, std::string_view rString);
201 /** convert number, 10th of degrees with range [0..3600] to SVG angle */
202 static void convertAngle(OUStringBuffer& rBuffer, sal_Int16 nAngle,
203 SvtSaveOptions::ODFSaneDefaultVersion nVersion);
205 /** convert SVG angle to number, 10th of degrees with range [0..3600] */
206 static bool convertAngle(sal_Int16& rAngle, std::u16string_view rString,
207 bool isWrongOOo10thDegAngle);
209 /** convert SVG angle to number, 10th of degrees with range [0..3600] */
210 static bool convertAngle(sal_Int16& rAngle, std::string_view rString,
211 bool isWrongOOo10thDegAngle);
213 /** convert double to XMLSchema-2 "duration" string; negative durations allowed */
214 static void convertDuration(OUStringBuffer& rBuffer,
215 const double fTime);
217 /** convert util::Duration to XMLSchema-2 "duration" string */
218 static void convertDuration(OUStringBuffer& rBuffer,
219 const css::util::Duration& rDuration);
221 /** convert XMLSchema-2 "duration" string to double; negative durations allowed */
222 static bool convertDuration(double & rfTime,
223 std::string_view rString);
225 /** convert XMLSchema-2 "duration" string to util::Duration */
226 static bool convertDuration(css::util::Duration& rDuration,
227 std::u16string_view rString);
229 /** convert XMLSchema-2 "duration" string to util::Duration */
230 static bool convertDuration(css::util::Duration& rDuration,
231 std::string_view rString);
233 /** convert util::Date to XMLSchema-2 "date" string */
234 static void convertDate( OUStringBuffer& rBuffer,
235 const css::util::Date& rDate,
236 sal_Int16 const* pTimeZoneOffset);
238 /** convert util::DateTime to XMLSchema-2 "date" or "dateTime" string */
239 static void convertDateTime( OUStringBuffer& rBuffer,
240 const css::util::DateTime& rDateTime,
241 sal_Int16 const* pTimeZoneOffset,
242 bool bAddTimeIf0AM = false );
244 /** convert util::DateTime to XMLSchema-2 "time" or "dateTime" string */
245 static void convertTimeOrDateTime(OUStringBuffer& rBuffer,
246 const css::util::DateTime& rDateTime);
248 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
249 static bool parseDateTime( css::util::DateTime& rDateTime,
250 std::u16string_view rString );
252 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
253 static bool parseDateTime( css::util::DateTime& rDateTime,
254 std::string_view rString );
256 /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
257 static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
258 std::u16string_view rString);
260 /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
261 static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
262 std::string_view rString);
264 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
265 util::Date */
266 static bool parseDateOrDateTime(
267 css::util::Date * pDate,
268 css::util::DateTime & rDateTime,
269 bool & rbDateTime,
270 std::optional<sal_Int16> * pTimeZoneOffset,
271 std::u16string_view rString );
273 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
274 util::Date */
275 static bool parseDateOrDateTime(
276 css::util::Date * pDate,
277 css::util::DateTime & rDateTime,
278 bool & rbDateTime,
279 std::optional<sal_Int16> * pTimeZoneOffset,
280 std::string_view rString );
282 /** gets the position of the first comma after npos in the string
283 rStr. Commas inside '"' pairs are not matched */
284 static sal_Int32 indexOfComma( std::u16string_view rStr,
285 sal_Int32 nPos );
287 static double GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
288 static double GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
289 static sal_Int16 GetUnitFromString(std::u16string_view rString, sal_Int16 nDefaultUnit);
290 static sal_Int16 GetUnitFromString(std::string_view rString, sal_Int16 nDefaultUnit);
292 /** convert an Any to string (typesafe) */
293 static bool convertAny(OUStringBuffer& rsValue,
294 OUStringBuffer& rsType ,
295 const css::uno::Any& rValue);
297 /** convert specified byte sequence to xsd:hexBinary string **/
298 static void convertBytesToHexBinary(OUStringBuffer& rBuffer, const void* pBytes,
299 sal_Int32 nBytes);
301 template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
302 static void convertNumberToHexBinary(OUStringBuffer& rBuffer, T n)
304 convertBytesToHexBinary(rBuffer, &n, sizeof(n));
311 #endif // INCLUDED_SAX_TOOLS_CONVERTER_HXX
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */