]>
vaikene.ee Git - evaf/blob - src/libs/Common/util.h
3 * @brief Global utility functions for eVaf
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
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.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
20 #ifndef __COMMON_UTIL_H
21 # define __COMMON_UTIL_H
23 #include "libcommon.h"
32 * Tests if the string means 'true'
33 * @param str The string
34 * @return True if the string is 'yes', 'true', 'on' or '1'.
36 * This function tests if the string means 'true'. Use this function when the default value
39 inline bool isTrue(QString
const & str
)
41 return str
== "1" || str
.toLower() == "yes" || str
.toLower() == "true" || str
.toLower() == "on";
45 * Tests if the string means 'false'
46 * @param str The string
47 * @return True if the string is 'no', 'false', 'off' or '0'.
49 * This function tests if the string means 'false'. Use this function when the default value
52 inline bool isFalse(QString
const & str
)
54 return str
== "0" || str
.toLower() == "no" || str
.toLower() == "false" || str
.toLower() == "off";
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
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
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 a UTF-16 code in hex; if the
74 * string begins with "\0", expects it to be a UTF-16 code in oct; if the string begins with "&", expects it to
75 * be a numeric character reference ("&#nnnn;" or "&#xhhhh;") or a predefined character entity reference;
76 * otherwise uses the first character in the string;
79 COMMON_EXPORT QVariant
toVariant(QString
const & value
, QVariant
const & defaultValue
);
82 * Converts unicode strings to escaped 7-bit character arrays.
83 * @param str Unicode string
84 * @return Escaped 7-bit character array
86 * This function converts a unicode (or any) string to the escaped 7-bit character array. Characters that cannot
87 * be output directly as a printable 7-bit character are output as numeric character references. The result can be
88 * directly inserted into XML or HTML documents and later converted back with the eVaf::Common::strFromEscapedCharArray()
91 * For example, "Groß" becomes "Groß".
93 COMMON_EXPORT QByteArray
strToEscapedCharArray(QString
const & str
);
96 * Converts escaped 7-bit character arrays to unicode string.
97 * @param str Escaped 7-bit character array
98 * @return Unicode string
100 * This function converts an escaped 7-bit character array to the unicode string. Numeric character references and
101 * character entity references are expanded to actual unicode characters.
103 COMMON_EXPORT QString
strFromEscapedCharArray(QByteArray
const & str
);
106 * Converts binary arrays to escaped 7-bit character arrays
107 * @param src Binary array
108 * @return Escaped 7-bit character array
110 * This function converts a binary array to the escaped 7-bit character array. Bytes that cannot be output
111 * directly as printable 7-bit characters are output as numeric character references. The result can be directly
112 * inserted into XML or HTML documents and later converted back with the eVaf::Common::binFromEscapedCharArray()
115 * For example, "Hello\r\n" becomes "Hello
".
117 COMMON_EXPORT QByteArray
binToEscapedCharArray(QByteArray
const & src
);
120 * Converts escaped 7-bit character arrays to binary arrays
121 * @param str Escaped 7-bit character array
122 * @return Binary array
124 * This function converts an escaped 7-bit character array to the binary array. Numeric character references and
125 * character entoty references are expanded to characters and binary bytes.
127 COMMON_EXPORT QByteArray
binFromEscapedCharArray(QByteArray
const & str
);
129 } // namespace eVaf::Common