summaryrefslogtreecommitdiff
path: root/include/sax/tools/converter.hxx
blob: 6b18cac11bf494464c4ca12d63fa4f3156f5637a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_SAX_TOOLS_CONVERTER_HXX
#define INCLUDED_SAX_TOOLS_CONVERTER_HXX

#include <sal/config.h>

#include <optional>
#include <type_traits>

#include <sax/saxdllapi.h>

#include <rtl/strbuf.hxx>
#include <sal/types.h>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/util/MeasureUnit.hpp>
#include <tools/color.hxx>
#include <unotools/saveopt.hxx>

namespace com::sun::star {
    namespace uno {
        class Any;
    }
    namespace util {
        struct Date;
        struct DateTime;
        struct Duration;
    }
}

namespace sax {

/** the Converter converts values of various types from
    their internal representation to the textual form used in xml
    and back.

    All unit types are expressed as css::util::MeasureUnit
*/


class SAX_DLLPUBLIC Converter
{
public:
    /** convert string to measure using optional min and max values*/
    static bool convertMeasure( sal_Int32& rValue,
                                std::u16string_view rString,
                                sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH,
                                sal_Int32 nMin = SAL_MIN_INT32,
                                sal_Int32 nMax = SAL_MAX_INT32 );

    /** convert string to measure using optional min and max values*/
    static bool convertMeasure( sal_Int32& rValue,
                                std::string_view rString,
                                sal_Int16 nTargetUnit = css::util::MeasureUnit::MM_100TH,
                                sal_Int32 nMin = SAL_MIN_INT32,
                                sal_Int32 nMax = SAL_MAX_INT32 );

    /** convert measure to string */
    static void convertMeasure( OUStringBuffer& rBuffer,
                                sal_Int32 nMeasure,
                                sal_Int16 SourceUnit,
                                sal_Int16 nTargetUnit  );

    /** convert string to measure and unit pair */
    static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit,
                                   std::u16string_view rString);

    /** convert string to measure and unit pair */
    static bool convertMeasureUnit(double& rValue, std::optional<sal_Int16>& rValueUnit,
                                   std::string_view rString);

    /** convert measure and unit pair to string */
    static void convertMeasureUnit(OUStringBuffer& rBuffer, double dValue,
                                   std::optional<sal_Int16> nValueUnit);

    /** convert string to boolean */
    static bool convertBool( bool& rBool,
                             std::u16string_view rString );

    /** convert string to boolean */
    static bool convertBool( bool& rBool,
                             std::string_view rString );

    /** convert boolean to string */
    static void convertBool( OUStringBuffer& rBuffer,
                             bool bValue );

    /** convert string to percent */
    static bool convertPercent( sal_Int32& rValue,
                                std::u16string_view rString );

    /** convert string to percent */
    static bool convertPercent( sal_Int32& rValue,
                                std::string_view rString );

    /** convert percent to string */
    static void convertPercent( OUStringBuffer& rBuffer,
                                sal_Int32 nValue );

    /** convert string to pixel measure unit */
    static bool convertMeasurePx( sal_Int32& rValue,
                                  std::u16string_view rString );

    /** convert string to pixel measure unit */
    static bool convertMeasurePx( sal_Int32& rValue,
                                  std::string_view rString );

    /** convert pixel measure unit to string */
    static void convertMeasurePx( OUStringBuffer& rBuffer,
                                  sal_Int32 nValue );

    /** convert string to rgb color */
    static bool convertColor( sal_Int32& rColor,
                              std::u16string_view rValue );
    static bool convertColor( sal_Int32& rColor,
                              std::string_view rValue );
    static bool convertColor( ::Color& rColor,
                              std::u16string_view rValue )
    {
        sal_Int32 n(rColor);
        bool b = convertColor( n, rValue );
        if (b) rColor = Color(ColorTransparency, n);
        return b;
    }
    static bool convertColor( ::Color& rColor,
                              std::string_view rValue )
    {
        sal_Int32 n(rColor);
        bool b = convertColor( n, rValue );
        if (b) rColor = Color(ColorTransparency, n);
        return b;
    }

    /** convert color to string */
    static void convertColor( OUStringBuffer &rBuffer,
                              sal_Int32 nColor );
    static void convertColor( OUStringBuffer &rBuffer,
                              ::Color nColor )
    { convertColor( rBuffer, sal_Int32(nColor) ); }

    /** convert string to number with optional min and max values */
    static bool convertNumber( sal_Int32& rValue,
                               std::u16string_view aString,
                               sal_Int32 nMin = SAL_MIN_INT32,
                               sal_Int32 nMax = SAL_MAX_INT32 );

    /** convert string to number with optional min and max values */
    static bool convertNumber( sal_Int32& rValue,
                               std::string_view aString,
                               sal_Int32 nMin = SAL_MIN_INT32,
                               sal_Int32 nMax = SAL_MAX_INT32 );

    /** convert string to number with optional min and max values */
    static bool convertNumber64(sal_Int64& rValue,
                                std::u16string_view aString,
                                sal_Int64 nMin = SAL_MIN_INT64,
                                sal_Int64 nMax = SAL_MAX_INT64);

    /** convert string to number with optional min and max values */
    static bool convertNumber64(sal_Int64& rValue,
                                std::string_view aString,
                                sal_Int64 nMin = SAL_MIN_INT64,
                                sal_Int64 nMax = SAL_MAX_INT64);

    /** convert double number to string (using ::rtl::math) and
        DO convert from source unit to target unit */
    static void convertDouble( OUStringBuffer& rBuffer,
                               double fNumber,
                               bool bWriteUnits,
                               sal_Int16 nSourceUnit,
                               sal_Int16 nTargetUnit );

    /** convert double number to string (using ::rtl::math) without unit conversion */
    static void convertDouble( OUStringBuffer& rBuffer, double fNumber);

    /** convert string to double number (using ::rtl::math) and DO convert from
        source unit to target unit. */
    static bool convertDouble(  double& rValue,
                                std::u16string_view rString,
                                sal_Int16 nSourceUnit,
                                sal_Int16 nTargetUnit );

    /** convert string to double number (using ::rtl::math) and DO convert from
        source unit to target unit. */
    static bool convertDouble(  double& rValue,
                                std::string_view rString,
                                sal_Int16 nSourceUnit,
                                sal_Int16 nTargetUnit );

    /** convert string to double number (using ::rtl::math) without unit conversion */
    static bool convertDouble(double& rValue, std::u16string_view rString, std::u16string_view* pRest = nullptr);

    /** convert string to double number (using ::rtl::math) without unit conversion */
    static bool convertDouble(double& rValue, std::string_view rString, std::string_view* pRest = nullptr);

    /** convert number, 10th of degrees with range [0..3600] to SVG angle */
    static void convert10thDegAngle(OUStringBuffer& rBuffer, sal_Int16 nAngle,
                                    const bool isWrongOOo10thDegAngle);

    /** convert SVG angle to number in 10th of degrees */
    static bool convert10thDegAngle(sal_Int16& rAngle, std::u16string_view rString,
                                    bool isWrongOOo10thDegAngle);

    /** convert SVG angle to number in 10th of degrees */
    static bool convert10thDegAngle(sal_Int16& rAngle, std::string_view rString,
                                    bool isWrongOOo10thDegAngle);

    /** convert SVG angle to number, in degrees, range [0..360] */
    static bool convertAngle(double& rAngle, std::u16string_view rString);

    /** convert SVG angle to number, in degrees, range [0..360] */
    static bool convertAngle(double& rAngle, std::string_view rString);

    /** convert double to XMLSchema-2 "duration" string; negative durations allowed */
    static void convertDuration(OUStringBuffer& rBuffer,
                                const double fTime);

    /** convert util::Duration to XMLSchema-2 "duration" string */
    static void convertDuration(OUStringBuffer& rBuffer,
                        const css::util::Duration& rDuration);

    /** convert XMLSchema-2 "duration" string to double; negative durations allowed */
    static bool convertDuration(double & rfTime,
                                std::string_view rString);

    /** convert XMLSchema-2 "duration" string to util::Duration */
    static bool convertDuration(css::util::Duration& rDuration,
                        std::u16string_view rString);

    /** convert XMLSchema-2 "duration" string to util::Duration */
    static bool convertDuration(css::util::Duration& rDuration,
                        std::string_view rString);

    /** convert util::Date to XMLSchema-2 "date" string */
    static void convertDate( OUStringBuffer& rBuffer,
                    const css::util::Date& rDate,
                    sal_Int16 const* pTimeZoneOffset);

    /** convert util::DateTime to XMLSchema-2 "date" or "dateTime" string */
    static void convertDateTime( OUStringBuffer& rBuffer,
                                const css::util::DateTime& rDateTime,
                                 sal_Int16 const* pTimeZoneOffset,
                                   bool bAddTimeIf0AM = false );

    /** convert util::DateTime to XMLSchema-2 "time" or "dateTime" string */
    static void convertTimeOrDateTime(OUStringBuffer& rBuffer,
                            const css::util::DateTime& rDateTime);

    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
    static bool parseDateTime( css::util::DateTime& rDateTime,
                               std::u16string_view rString );

    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime */
    static bool parseDateTime( css::util::DateTime& rDateTime,
                               std::string_view rString );

    /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
    static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
                                 std::u16string_view rString);

    /** convert XMLSchema-2 "time" or "dateTime" string to util::DateTime */
    static bool parseTimeOrDateTime(css::util::DateTime& rDateTime,
                                 std::string_view rString);

    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
        util::Date */
    static bool parseDateOrDateTime(
                    css::util::Date * pDate,
                    css::util::DateTime & rDateTime,
                    bool & rbDateTime,
                    std::optional<sal_Int16> * pTimeZoneOffset,
                    std::u16string_view rString );

    /** convert XMLSchema-2 "date" or "dateTime" string to util::DateTime or
        util::Date */
    static bool parseDateOrDateTime(
                    css::util::Date * pDate,
                    css::util::DateTime & rDateTime,
                    bool & rbDateTime,
                    std::optional<sal_Int16> * pTimeZoneOffset,
                    std::string_view rString );

    /** gets the position of the first comma after npos in the string
        rStr. Commas inside '"' pairs are not matched */
    static sal_Int32 indexOfComma( std::u16string_view rStr,
                                   sal_Int32 nPos );

    static double GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
    static double GetConversionFactor(OStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
    static sal_Int16 GetUnitFromString(std::u16string_view rString, sal_Int16 nDefaultUnit);
    static sal_Int16 GetUnitFromString(std::string_view rString, sal_Int16 nDefaultUnit);

    /** convert an Any to string (typesafe) */
    static bool convertAny(OUStringBuffer&          rsValue,
                           OUStringBuffer&          rsType ,
                           const css::uno::Any& rValue);

    /** convert specified byte sequence to xsd:hexBinary string **/
    static void convertBytesToHexBinary(OUStringBuffer& rBuffer, const void* pBytes,
                                        sal_Int32 nBytes);

    template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
    static void convertNumberToHexBinary(OUStringBuffer& rBuffer, T n)
    {
        convertBytesToHexBinary(rBuffer, &n, sizeof(n));
    }

};

}

#endif // INCLUDED_SAX_TOOLS_CONVERTER_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */