Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
#ifndef INCLUDED_SAX_TOOLS_CONVERTER_HXX
21
#define INCLUDED_SAX_TOOLS_CONVERTER_HXX
22
23
#include <sal/config.h>
24
25
#include <optional>
26
#include <type_traits>
27
28
#include <sax/saxdllapi.h>
29
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>
36
37
namespace com::sun::star {
38
    namespace uno {
39
        class Any;
40
    }
41
    namespace util {
42
        struct Date;
43
        struct DateTime;
44
        struct Duration;
45
    }
46
}
47
48
namespace sax {
49
50
/** the Converter converts values of various types from
51
    their internal representation to the textual form used in xml
52
    and back.
53
54
    All unit types are expressed as css::util::MeasureUnit
55
*/
56
57
58
class SAX_DLLPUBLIC Converter
59
{
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 );
67
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 );
74
75
    /** convert measure to string */
76
    static void convertMeasure( OUStringBuffer& rBuffer,
77
                                sal_Int32 nMeasure,
78
                                sal_Int16 SourceUnit,
79
                                sal_Int16 nTargetUnit  );
80
81
    /** convert string to measure and unit pair */
82
    static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit,
83
                                   std::u16string_view rString);
84
85
    /** convert string to measure and unit pair */
86
    static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit,
87
                                   std::string_view rString);
88
89
    /** convert measure and unit pair to string */
90
    static void convertMeasureUnit(OUStringBuffer& rBuffer, double dValue,
91
                                   std::optional<sal_Int16> nValueUnit);
92
93
    /** convert string to boolean */
94
    static bool convertBool( bool& rBool,
95
                             std::u16string_view rString );
96
97
    /** convert string to boolean */
98
    static bool convertBool( bool& rBool,
99
                             std::string_view rString );
100
101
    /** convert boolean to string */
102
    static void convertBool( OUStringBuffer& rBuffer,
103
                             bool bValue );
104
105
    /** convert string to percent */
106
    static bool convertPercent( sal_Int32& rValue,
107
                                std::u16string_view rString );
108
109
    /** convert string to percent */
110
    static bool convertPercent( sal_Int32& rValue,
111
                                std::string_view rString );
112
113
    /** convert percent to string */
114
    static void convertPercent( OUStringBuffer& rBuffer,
115
                                sal_Int32 nValue );
116
117
    /** convert string to pixel measure unit */
118
    static bool convertMeasurePx( sal_Int32& rValue,
119
                                  std::u16string_view rString );
120
121
    /** convert string to pixel measure unit */
122
    static bool convertMeasurePx( sal_Int32& rValue,
123
                                  std::string_view rString );
124
125
    /** convert pixel measure unit to string */
126
    static void convertMeasurePx( OUStringBuffer& rBuffer,
127
                                  sal_Int32 nValue );
128
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 )
136
5.90k
    {
137
5.90k
        sal_Int32 n(rColor);
138
5.90k
        bool b = convertColor( n, rValue );
139
5.90k
        if (b) rColor = Color(ColorTransparency, n);
140
5.90k
        return b;
141
5.90k
    }
142
    static bool convertColor( ::Color& rColor,
143
                              std::string_view rValue )
144
3.01k
    {
145
3.01k
        sal_Int32 n(rColor);
146
3.01k
        bool b = convertColor( n, rValue );
147
3.01k
        if (b) rColor = Color(ColorTransparency, n);
148
3.01k
        return b;
149
3.01k
    }
150
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
0
    { convertColor( rBuffer, sal_Int32(nColor) ); }
157
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 );
163
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 );
169
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);
175
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);
181
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 );
189
190
    /** convert double number to string (using ::rtl::math) without unit conversion */
191
    static void convertDouble( OUStringBuffer& rBuffer, double fNumber);
192
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 );
199
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 );
206
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);
209
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);
212
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);
216
217
    /** convert SVG angle to number in 10th of degrees */
218
    static bool convert10thDegAngle(sal_Int16& rAngle, std::u16string_view rString,
219
                                    bool isWrongOOo10thDegAngle);
220
221
    /** convert SVG angle to number in 10th of degrees */
222
    static bool convert10thDegAngle(sal_Int16& rAngle, std::string_view rString,
223
                                    bool isWrongOOo10thDegAngle);
224
225
    /** convert SVG angle to number, in degrees, range [0..360] */
226
    static bool convertAngle(double& rAngle, std::u16string_view rString);
227
228
    /** convert SVG angle to number, in degrees, range [0..360] */
229
    static bool convertAngle(double& rAngle, std::string_view rString);
230
231
    /** convert double to XMLSchema-2 "duration" string; negative durations allowed */
232
    static void convertDuration(OUStringBuffer& rBuffer,
233
                                const double fTime);
234
235
    /** convert util::Duration to XMLSchema-2 "duration" string */
236
    static void convertDuration(OUStringBuffer& rBuffer,
237
                        const css::util::Duration& rDuration);
238
239
    /** convert XMLSchema-2 "duration" string to double; negative durations allowed */
240
    static bool convertDuration(double & rfTime,
241
                                std::string_view rString);
242
243
    /** convert XMLSchema-2 "duration" string to util::Duration */
244
    static bool convertDuration(css::util::Duration& rDuration,
245
                        std::u16string_view rString);
246
247
    /** convert XMLSchema-2 "duration" string to util::Duration */
248
    static bool convertDuration(css::util::Duration& rDuration,
249
                        std::string_view rString);
250
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);
255
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 );
261
262
    /** convert util::DateTime to XMLSchema-2 "date" or "dateTime" string */
263
    static void convertDateTime( OStringBuffer& rBuffer,
264
                                const css::util::DateTime& rDateTime,
265
                                 sal_Int16 const* pTimeZoneOffset,
266
                                   bool bAddTimeIf0AM = false );
267
268
    /** convert util::DateTime to XMLSchema-2 "time" or "dateTime" string */
269
    static void convertTimeOrDateTime(OUStringBuffer& rBuffer,
270
                            const css::util::DateTime& rDateTime);
271
272
    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
273
    static bool parseDateTime( css::util::DateTime& rDateTime,
274
                               std::u16string_view rString );
275
276
    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
277
    static bool parseDateTime( css::util::DateTime& rDateTime,
278
                               std::string_view rString );
279
280
    /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
281
    static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
282
                                 std::u16string_view rString);
283
284
    /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
285
    static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
286
                                 std::string_view rString);
287
288
    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
289
        util::Date */
290
    static bool parseDateOrDateTime(
291
                    css::util::Date * pDate,
292
                    css::util::DateTime & rDateTime,
293
                    bool & rbDateTime,
294
                    std::optional<sal_Int16> * pTimeZoneOffset,
295
                    std::u16string_view rString );
296
297
    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
298
        util::Date */
299
    static bool parseDateOrDateTime(
300
                    css::util::Date * pDate,
301
                    css::util::DateTime & rDateTime,
302
                    bool & rbDateTime,
303
                    std::optional<sal_Int16> * pTimeZoneOffset,
304
                    std::string_view rString );
305
306
    /** gets the position of the first comma after npos in the string
307
        rStr. Commas inside '"' pairs are not matched */
308
    static sal_Int32 indexOfComma( std::u16string_view rStr,
309
                                   sal_Int32 nPos );
310
311
    static double GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
312
    static double GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
313
    static sal_Int16 GetUnitFromString(std::u16string_view rString, sal_Int16 nDefaultUnit);
314
    static sal_Int16 GetUnitFromString(std::string_view rString, sal_Int16 nDefaultUnit);
315
316
    /** convert an Any to string (typesafe) */
317
    static bool convertAny(OUStringBuffer&          rsValue,
318
                           OUStringBuffer&          rsType ,
319
                           const css::uno::Any& rValue);
320
321
    /** convert specified byte sequence to xsd:hexBinary string **/
322
    static void convertBytesToHexBinary(OUStringBuffer& rBuffer, const void* pBytes,
323
                                        sal_Int32 nBytes);
324
325
    template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
326
    static void convertNumberToHexBinary(OUStringBuffer& rBuffer, T n)
327
0
    {
328
0
        convertBytesToHexBinary(rBuffer, &n, sizeof(n));
329
0
    }
330
331
};
332
333
}
334
335
#endif // INCLUDED_SAX_TOOLS_CONVERTER_HXX
336
337
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */