/** * @file Common/util.h * @brief Global utility functions for eVaf * @author Enar Vaikene * * Copyright (c) 2011 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * * This file can be used under the terms of the GNU General Public License * version 3.0 as published by the Free Software Foundation and appearing in * the file LICENSE included in the packaging of this file. Please review the * the following information to ensure the GNU General Public License version * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. * * Alternatively, this file may be used in accordance with the Commercial License * Agreement provided with the Software. */ #ifndef __COMMON_UTIL_H # define __COMMON_UTIL_H #include "libcommon.h" #include #include namespace eVaf { namespace Common { /** * Tests if the string means 'true' * @param str The string * @return True if the string is 'yes', 'true', 'on' or '1'. * * This function tests if the string means 'true'. Use this function when the default value * should be 'false'. */ inline bool isTrue(QString const & str) { return str == "1" || str.toLower() == "yes" || str.toLower() == "true" || str.toLower() == "on"; } /** * Tests if the string means 'false' * @param str The string * @return True if the string is 'no', 'false', 'off' or '0'. * * This function tests if the string means 'false'. Use this function when the default value * should be 'true'. */ inline bool isFalse(QString const & str) { return str == "0" || str.toLower() == "no" || str.toLower() == "false" || str.toLower() == "off"; } /** * Converts strings to variant values * @param value The string * @param defaultValue The default value * @return value converted from the string or the default if failed * * This function converts strings to variant values. The type of the expected value is determined * from the default value. If the string fails to convert to the expected type, returns the * default value. * * If the default value is of one of the following types, then the following conversion rules are applied: * @li QVariant::UInt - if the string begins with "0x", base 16 is used; if the string begins with "0", * base 8 is used; otherwise base 10 is used; * @li QVariant::Int - base 10 is used; * @li QVariant::Double - the decimal point is expecte to be '.' regardless which locale is used; * @li QVariant::Bool - tries to use isTrue() and isFalse() methods; otherwise performs a conversion to QVariant::uint; * @li QVariant::Char - if the string begins with "\0x", expects it to be an ASCII code in hex; if the * string begins with "\0", expects it to be an ASCII code in oct; otherwise uses the first character in the string; */ COMMON_EXPORT QVariant toVariant(QString const & value, QVariant const & defaultValue); /** * Converts unicode strings to escaped 7-bit character arrays. * @param str Unicode string * @return Escaped 7-bit character array * * This function converts a unicode (or any) string to the escaped 7-bit character array. Characters that cannot * be output directly as a printable 7-bit character are output as numeric character references. The result can be * directly inserted into XML or HTML documents and later converted back with the eVaf::Common::strFromEscapedCharArray() * function. */ COMMON_EXPORT QByteArray strToEscapedCharArray(QString const & str); /** * Converts escaped 7-bit character arrays to unicode string. * @param str Escaped 7-bit character array * @return Unicode string * * This function converts an escaped 7-bit character array to the unicode string. Numeric character references and * character entity references are expanded to actual unicode characters. */ COMMON_EXPORT QString strFromEscapedCharArray(QByteArray const & str); /** * Converts binary arrays to escaped 7-bit character arrays * @param src Binary array * @return Escaped 7-bit character array * * This function converts a binary array to the escaped 7-bit character array. Bytes that cannot be output * directly as printable 7-bit characters are output as numeric character references. The result can be directly * inserted into XML or HTML documents and later converted back with the eVaf::Common::binFromEscapedCharArray() * function. */ COMMON_EXPORT QByteArray binToEscapedCharArray(QByteArray const & src); /** * Converts escaped 7-bit character arrays to binary arrays * @param str Escaped 7-bit character array * @return Binary array * * This function converts an escaped 7-bit character array to the binary array. Numeric character references and * character entoty references are expanded to characters and binary bytes. */ COMMON_EXPORT QByteArray binFromEscapedCharArray(QByteArray const & str); } // namespace eVaf::Common } // namespace eVaf #endif // util.h