]> vaikene.ee Git - evaf/blob - src/libs/Common/util.h
Added functions to convert unicode strings to/from escaped 7-bit character arrays.
[evaf] / src / libs / Common / util.h
1 /**
2 * @file Common/util.h
3 * @brief Global utility functions for eVaf
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #ifndef __COMMON_UTIL_H
21 # define __COMMON_UTIL_H
22
23 #include "libcommon.h"
24
25 #include <QString>
26 #include <QVariant>
27
28 namespace eVaf {
29 namespace Common {
30
31 /**
32 * Tests if the string means 'true'
33 * @param str The string
34 * @return True if the string is 'yes', 'true', 'on' or '1'.
35 *
36 * This function tests if the string means 'true'. Use this function when the default value
37 * should be 'false'.
38 */
39 inline bool isTrue(QString const & str)
40 {
41 return str == "1" || str.toLower() == "yes" || str.toLower() == "true" || str.toLower() == "on";
42 }
43
44 /**
45 * Tests if the string means 'false'
46 * @param str The string
47 * @return True if the string is 'no', 'false', 'off' or '0'.
48 *
49 * This function tests if the string means 'false'. Use this function when the default value
50 * should be 'true'.
51 */
52 inline bool isFalse(QString const & str)
53 {
54 return str == "0" || str.toLower() == "no" || str.toLower() == "false" || str.toLower() == "off";
55 }
56
57 /**
58 * Converts strings to variant values
59 * @param value The string
60 * @param defaultValue The default value
61 * @return value converted from the string or the default if failed
62 *
63 * This function converts strings to variant values. The type of the expected value is determined
64 * from the default value. If the string fails to convert to the expected type, returns the
65 * default value.
66 *
67 * If the default value is of one of the following types, then the following conversion rules are applied:
68 * @li QVariant::UInt - if the string begins with "0x", base 16 is used; if the string begins with "0",
69 * base 8 is used; otherwise base 10 is used;
70 * @li QVariant::Int - base 10 is used;
71 * @li QVariant::Double - the decimal point is expecte to be '.' regardless which locale is used;
72 * @li QVariant::Bool - tries to use isTrue() and isFalse() methods; otherwise performs a conversion to QVariant::uint;
73 * @li QVariant::Char - if the string begins with "\0x", expects it to be an ASCII code in hex; if the
74 * string begins with "\0", expects it to be an ASCII code in oct; otherwise uses the first character in the string;
75 */
76
77 COMMON_EXPORT QVariant toVariant(QString const & value, QVariant const & defaultValue);
78
79 /**
80 * Converts unicode strings to escaped 7-bit character arrays.
81 * @param str Unicode string
82 * @return Escaped 7-bit character array
83 *
84 * This function converts a unicode (or any) string to the escaped 7-bit character array. Characters that cannot
85 * be output directly as a printable 7-bit character are output as numeric character references. The result can be
86 * directly inserted into XML or HTML documents and later converted back with the eVaf::Common::fromEscapedString()
87 * function.
88 */
89 COMMON_EXPORT QByteArray toEscapedString(QString const & str);
90
91 /**
92 * Converts escaped 7-bit character arrays to unicode string.
93 * @param str Escaped 7-bit character array
94 * @return Unicode string
95 *
96 * This function converts an escaped 7-bit character array to a unicode string. Numeric character references and
97 * character entity references are expanded to actual unicode characters.
98 */
99 COMMON_EXPORT QString fromEscapedString(QByteArray const & str);
100
101 } // namespace eVaf::Common
102 } // namespace eVaf
103
104 #endif // util.h