cid#1640468 Dereference after null check
[LibreOffice.git] / include / sax / tools / converter.hxx
blob6b18cac11bf494464c4ca12d63fa4f3156f5637a
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 measure and unit pair */
82 static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit,
83 std::u16string_view rString);
85 /** convert string to measure and unit pair */
86 static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit,
87 std::string_view rString);
89 /** convert measure and unit pair to string */
90 static void convertMeasureUnit(OUStringBuffer& rBuffer, double dValue,
91 std::optional<sal_Int16> nValueUnit);
93 /** convert string to boolean */
94 static bool convertBool( bool& rBool,
95 std::u16string_view rString );
97 /** convert string to boolean */
98 static bool convertBool( bool& rBool,
99 std::string_view rString );
101 /** convert boolean to string */
102 static void convertBool( OUStringBuffer& rBuffer,
103 bool bValue );
105 /** convert string to percent */
106 static bool convertPercent( sal_Int32& rValue,
107 std::u16string_view rString );
109 /** convert string to percent */
110 static bool convertPercent( sal_Int32& rValue,
111 std::string_view rString );
113 /** convert percent to string */
114 static void convertPercent( OUStringBuffer& rBuffer,
115 sal_Int32 nValue );
117 /** convert string to pixel measure unit */
118 static bool convertMeasurePx( sal_Int32& rValue,
119 std::u16string_view rString );
121 /** convert string to pixel measure unit */
122 static bool convertMeasurePx( sal_Int32& rValue,
123 std::string_view rString );
125 /** convert pixel measure unit to string */
126 static void convertMeasurePx( OUStringBuffer& rBuffer,
127 sal_Int32 nValue );
129 /** convert string to rgb color */
130 static bool convertColor( sal_Int32& rColor,
131 std::u16string_view rValue );
132 static bool convertColor( sal_Int32& rColor,
133 std::string_view rValue );
134 static bool convertColor( ::Color& rColor,
135 std::u16string_view rValue )
137 sal_Int32 n(rColor);
138 bool b = convertColor( n, rValue );
139 if (b) rColor = Color(ColorTransparency, n);
140 return b;
142 static bool convertColor( ::Color& rColor,
143 std::string_view rValue )
145 sal_Int32 n(rColor);
146 bool b = convertColor( n, rValue );
147 if (b) rColor = Color(ColorTransparency, n);
148 return b;
151 /** convert color to string */
152 static void convertColor( OUStringBuffer &rBuffer,
153 sal_Int32 nColor );
154 static void convertColor( OUStringBuffer &rBuffer,
155 ::Color nColor )
156 { convertColor( rBuffer, sal_Int32(nColor) ); }
158 /** convert string to number with optional min and max values */
159 static bool convertNumber( sal_Int32& rValue,
160 std::u16string_view aString,
161 sal_Int32 nMin = SAL_MIN_INT32,
162 sal_Int32 nMax = SAL_MAX_INT32 );
164 /** convert string to number with optional min and max values */
165 static bool convertNumber( sal_Int32& rValue,
166 std::string_view aString,
167 sal_Int32 nMin = SAL_MIN_INT32,
168 sal_Int32 nMax = SAL_MAX_INT32 );
170 /** convert string to number with optional min and max values */
171 static bool convertNumber64(sal_Int64& rValue,
172 std::u16string_view aString,
173 sal_Int64 nMin = SAL_MIN_INT64,
174 sal_Int64 nMax = SAL_MAX_INT64);
176 /** convert string to number with optional min and max values */
177 static bool convertNumber64(sal_Int64& rValue,
178 std::string_view aString,
179 sal_Int64 nMin = SAL_MIN_INT64,
180 sal_Int64 nMax = SAL_MAX_INT64);
182 /** convert double number to string (using ::rtl::math) and
183 DO convert from source unit to target unit */
184 static void convertDouble( OUStringBuffer& rBuffer,
185 double fNumber,
186 bool bWriteUnits,
187 sal_Int16 nSourceUnit,
188 sal_Int16 nTargetUnit );
190 /** convert double number to string (using ::rtl::math) without unit conversion */
191 static void convertDouble( OUStringBuffer& rBuffer, double fNumber);
193 /** convert string to double number (using ::rtl::math) and DO convert from
194 source unit to target unit. */
195 static bool convertDouble( double& rValue,
196 std::u16string_view rString,
197 sal_Int16 nSourceUnit,
198 sal_Int16 nTargetUnit );
200 /** convert string to double number (using ::rtl::math) and DO convert from
201 source unit to target unit. */
202 static bool convertDouble( double& rValue,
203 std::string_view rString,
204 sal_Int16 nSourceUnit,
205 sal_Int16 nTargetUnit );
207 /** convert string to double number (using ::rtl::math) without unit conversion */
208 static bool convertDouble(double& rValue, std::u16string_view rString, std::u16string_view* pRest = nullptr);
210 /** convert string to double number (using ::rtl::math) without unit conversion */
211 static bool convertDouble(double& rValue, std::string_view rString, std::string_view* pRest = nullptr);
213 /** convert number, 10th of degrees with range [0..3600] to SVG angle */
214 static void convert10thDegAngle(OUStringBuffer& rBuffer, sal_Int16 nAngle,
215 const bool isWrongOOo10thDegAngle);
217 /** convert SVG angle to number in 10th of degrees */
218 static bool convert10thDegAngle(sal_Int16& rAngle, std::u16string_view rString,
219 bool isWrongOOo10thDegAngle);
221 /** convert SVG angle to number in 10th of degrees */
222 static bool convert10thDegAngle(sal_Int16& rAngle, std::string_view rString,
223 bool isWrongOOo10thDegAngle);
225 /** convert SVG angle to number, in degrees, range [0..360] */
226 static bool convertAngle(double& rAngle, std::u16string_view rString);
228 /** convert SVG angle to number, in degrees, range [0..360] */
229 static bool convertAngle(double& rAngle, std::string_view rString);
231 /** convert double to XMLSchema-2 "duration" string; negative durations allowed */
232 static void convertDuration(OUStringBuffer& rBuffer,
233 const double fTime);
235 /** convert util::Duration to XMLSchema-2 "duration" string */
236 static void convertDuration(OUStringBuffer& rBuffer,
237 const css::util::Duration& rDuration);
239 /** convert XMLSchema-2 "duration" string to double; negative durations allowed */
240 static bool convertDuration(double & rfTime,
241 std::string_view rString);
243 /** convert XMLSchema-2 "duration" string to util::Duration */
244 static bool convertDuration(css::util::Duration& rDuration,
245 std::u16string_view rString);
247 /** convert XMLSchema-2 "duration" string to util::Duration */
248 static bool convertDuration(css::util::Duration& rDuration,
249 std::string_view rString);
251 /** convert util::Date to XMLSchema-2 "date" string */
252 static void convertDate( OUStringBuffer& rBuffer,
253 const css::util::Date& rDate,
254 sal_Int16 const* pTimeZoneOffset);
256 /** convert util::DateTime to XMLSchema-2 "date" or "dateTime" string */
257 static void convertDateTime( OUStringBuffer& rBuffer,
258 const css::util::DateTime& rDateTime,
259 sal_Int16 const* pTimeZoneOffset,
260 bool bAddTimeIf0AM = false );
262 /** convert util::DateTime to XMLSchema-2 "time" or "dateTime" string */
263 static void convertTimeOrDateTime(OUStringBuffer& rBuffer,
264 const css::util::DateTime& rDateTime);
266 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
267 static bool parseDateTime( css::util::DateTime& rDateTime,
268 std::u16string_view rString );
270 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
271 static bool parseDateTime( css::util::DateTime& rDateTime,
272 std::string_view rString );
274 /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
275 static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
276 std::u16string_view rString);
278 /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
279 static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
280 std::string_view rString);
282 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
283 util::Date */
284 static bool parseDateOrDateTime(
285 css::util::Date * pDate,
286 css::util::DateTime & rDateTime,
287 bool & rbDateTime,
288 std::optional<sal_Int16> * pTimeZoneOffset,
289 std::u16string_view rString );
291 /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
292 util::Date */
293 static bool parseDateOrDateTime(
294 css::util::Date * pDate,
295 css::util::DateTime & rDateTime,
296 bool & rbDateTime,
297 std::optional<sal_Int16> * pTimeZoneOffset,
298 std::string_view rString );
300 /** gets the position of the first comma after npos in the string
301 rStr. Commas inside '"' pairs are not matched */
302 static sal_Int32 indexOfComma( std::u16string_view rStr,
303 sal_Int32 nPos );
305 static double GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
306 static double GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
307 static sal_Int16 GetUnitFromString(std::u16string_view rString, sal_Int16 nDefaultUnit);
308 static sal_Int16 GetUnitFromString(std::string_view rString, sal_Int16 nDefaultUnit);
310 /** convert an Any to string (typesafe) */
311 static bool convertAny(OUStringBuffer& rsValue,
312 OUStringBuffer& rsType ,
313 const css::uno::Any& rValue);
315 /** convert specified byte sequence to xsd:hexBinary string **/
316 static void convertBytesToHexBinary(OUStringBuffer& rBuffer, const void* pBytes,
317 sal_Int32 nBytes);
319 template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
320 static void convertNumberToHexBinary(OUStringBuffer& rBuffer, T n)
322 convertBytesToHexBinary(rBuffer, &n, sizeof(n));
329 #endif // INCLUDED_SAX_TOOLS_CONVERTER_HXX
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */