2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2002-07-12 16:24:11 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2002-07-12 16:24:11 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-08 01:08:12 +00:00
|
|
|
#ifndef COMMON_UTIL_H
|
|
|
|
#define COMMON_UTIL_H
|
2002-07-12 16:24:11 +00:00
|
|
|
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/scummsys.h"
|
2006-02-14 23:31:25 +00:00
|
|
|
#include "common/str.h"
|
2002-07-12 16:24:11 +00:00
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/**
|
|
|
|
* @defgroup common_util Util
|
|
|
|
* @ingroup common
|
|
|
|
*
|
|
|
|
* @brief Various utility functions.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-03-19 21:43:27 +00:00
|
|
|
/**
|
|
|
|
* Check whether a given pointer is aligned correctly.
|
|
|
|
* Note that 'alignment' must be a power of two!
|
|
|
|
*/
|
|
|
|
#define IS_ALIGNED(value, alignment) \
|
2021-04-15 19:20:04 +00:00
|
|
|
((((size_t)value) & ((alignment) - 1)) == 0)
|
2009-03-19 21:43:27 +00:00
|
|
|
|
2017-10-07 17:59:47 +00:00
|
|
|
#ifdef ABS
|
|
|
|
#undef ABS
|
|
|
|
#endif
|
2009-03-19 21:43:27 +00:00
|
|
|
|
2006-04-08 12:41:02 +00:00
|
|
|
#ifdef MIN
|
2005-10-11 11:27:18 +00:00
|
|
|
#undef MIN
|
2006-04-08 12:41:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MAX
|
2005-10-11 11:27:18 +00:00
|
|
|
#undef MAX
|
|
|
|
#endif
|
|
|
|
|
2020-11-11 18:48:06 +00:00
|
|
|
/** Template method to return the absolute value of @p x. */
|
2013-07-30 19:02:01 +00:00
|
|
|
template<typename T> inline T ABS(T x) { return (x >= 0) ? x : -x; }
|
2020-11-11 18:48:06 +00:00
|
|
|
|
|
|
|
/** Template method to return the smallest of its parameters. */
|
2013-07-30 19:02:01 +00:00
|
|
|
template<typename T> inline T MIN(T a, T b) { return (a < b) ? a : b; }
|
2020-11-11 18:48:06 +00:00
|
|
|
|
|
|
|
/** Template method to return the largest of its parameters. */
|
2013-07-30 19:02:01 +00:00
|
|
|
template<typename T> inline T MAX(T a, T b) { return (a > b) ? a : b; }
|
2020-11-11 18:48:06 +00:00
|
|
|
|
|
|
|
/** Template method to clip the value @p v so that it remains between @p amin and @p amax. */
|
2013-07-30 19:02:01 +00:00
|
|
|
template<typename T> inline T CLIP(T v, T amin, T amax)
|
2020-06-06 14:53:47 +00:00
|
|
|
{
|
|
|
|
#if !defined(RELEASE_BUILD)
|
2020-11-11 18:48:06 +00:00
|
|
|
// Debug builds use this assert to pinpoint
|
2020-06-06 14:53:47 +00:00
|
|
|
// any problematic cases, where amin and amax
|
|
|
|
// are incorrectly ordered
|
2020-11-11 18:48:06 +00:00
|
|
|
// and thus CLIP() would return an invalid result.
|
2020-06-06 14:53:47 +00:00
|
|
|
assert(amin <= amax);
|
|
|
|
#endif
|
|
|
|
if (v < amin) return amin;
|
|
|
|
else if (v > amax) return amax;
|
|
|
|
return v;
|
|
|
|
}
|
2002-08-25 10:50:18 +00:00
|
|
|
|
2003-05-29 11:40:48 +00:00
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Template method to swap the values of its two parameters.
|
2003-05-29 11:40:48 +00:00
|
|
|
*/
|
2003-06-14 18:15:14 +00:00
|
|
|
template<typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; }
|
2003-05-15 21:40:36 +00:00
|
|
|
|
2018-12-16 21:56:19 +00:00
|
|
|
#ifdef ARRAYSIZE
|
|
|
|
#undef ARRAYSIZE
|
|
|
|
#endif
|
|
|
|
|
2009-03-19 21:43:27 +00:00
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Determine the number of entries in a fixed size array.
|
2009-03-19 21:43:27 +00:00
|
|
|
*/
|
2003-05-17 23:36:47 +00:00
|
|
|
#define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0])))
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2011-08-07 08:19:30 +00:00
|
|
|
/**
|
|
|
|
* Compute a pointer to one past the last element of an array.
|
|
|
|
*/
|
|
|
|
#define ARRAYEND(x) ((x) + ARRAYSIZE((x)))
|
|
|
|
|
2020-11-11 18:48:06 +00:00
|
|
|
/**
|
|
|
|
* Clear an array using the default or provided value.
|
2020-08-31 20:53:21 +00:00
|
|
|
*/
|
|
|
|
template<typename T, size_t N> inline void ARRAYCLEAR(T (&array) [N], const T &value = T()) {
|
|
|
|
T * ptr = array;
|
|
|
|
size_t n = N;
|
|
|
|
while(n--)
|
|
|
|
*ptr++ = value;
|
|
|
|
}
|
2009-01-30 01:17:12 +00:00
|
|
|
|
2010-10-30 00:32:45 +00:00
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Evaluate the name of the current function on compilers supporting this.
|
2010-10-30 00:32:45 +00:00
|
|
|
*/
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define SCUMMVM_CURRENT_FUNCTION __PRETTY_FUNCTION__
|
|
|
|
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
|
|
|
|
# define SCUMMVM_CURRENT_FUNCTION __func__
|
2014-06-20 01:21:14 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2010-10-30 00:32:45 +00:00
|
|
|
# define SCUMMVM_CURRENT_FUNCTION __FUNCTION__
|
|
|
|
#else
|
|
|
|
# define SCUMMVM_CURRENT_FUNCTION "<unknown>"
|
|
|
|
#endif
|
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/** @} */
|
|
|
|
|
2003-10-04 11:50:21 +00:00
|
|
|
namespace Common {
|
2002-07-12 16:24:11 +00:00
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup common_util
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2003-05-29 11:40:48 +00:00
|
|
|
/**
|
2003-10-04 11:50:21 +00:00
|
|
|
* Print a hexdump of the data passed in. The number of bytes per line is
|
|
|
|
* customizable.
|
2020-11-11 18:48:06 +00:00
|
|
|
*
|
|
|
|
* @param data The data to be dumped.
|
|
|
|
* @param len Length of that data.
|
|
|
|
* @param bytesPerLine Number of bytes to print per line (default: 16).
|
|
|
|
* @param startOffset Shift the shown offsets by the starting offset (default: 0).
|
2002-07-25 16:29:07 +00:00
|
|
|
*/
|
2009-05-12 07:07:17 +00:00
|
|
|
extern void hexdump(const byte * data, int len, int bytesPerLine = 16, int startOffset = 0);
|
2002-07-25 16:29:07 +00:00
|
|
|
|
2009-01-30 01:17:12 +00:00
|
|
|
|
2010-04-06 09:27:13 +00:00
|
|
|
/**
|
|
|
|
* Parse a string for a boolean value.
|
2020-11-11 18:48:06 +00:00
|
|
|
*
|
2010-04-06 09:27:13 +00:00
|
|
|
* The strings "true", "yes", and "1" are interpreted as true.
|
|
|
|
* The strings "false", "no", and "0" are interpreted as false.
|
|
|
|
* This function ignores case.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param[in] val The string to parse.
|
|
|
|
* @param[out] valAsBool Parsing result.
|
|
|
|
*
|
|
|
|
* @return True if the string has been parsed correctly, false if an error occurred.
|
2010-04-06 09:27:13 +00:00
|
|
|
*/
|
2011-08-06 07:47:19 +00:00
|
|
|
bool parseBool(const String &val, bool &valAsBool);
|
2010-04-06 09:27:13 +00:00
|
|
|
|
2012-02-20 15:03:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether the given character is alphanumeric (a-z, A-Z, 0-9).
|
2020-11-11 18:48:06 +00:00
|
|
|
*
|
2012-02-20 15:03:39 +00:00
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is alphanumeric, false otherwise.
|
2012-02-20 15:03:39 +00:00
|
|
|
*/
|
|
|
|
bool isAlnum(int c);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether the given character is an alphabetic letter (a-z, A-Z).
|
2020-11-11 18:48:06 +00:00
|
|
|
*
|
2012-02-20 15:03:39 +00:00
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is alphabetic, false otherwise.
|
2012-02-20 15:03:39 +00:00
|
|
|
*/
|
|
|
|
bool isAlpha(int c);
|
|
|
|
|
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Test whether the given character is a decimal digit (0-9).
|
|
|
|
*
|
2012-02-20 15:03:39 +00:00
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is a decimal digit, false otherwise.
|
2012-02-20 15:03:39 +00:00
|
|
|
*/
|
|
|
|
bool isDigit(int c);
|
|
|
|
|
2019-05-17 23:51:43 +00:00
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Test whether the given character is a hexadecimal digit (0-9 or A-F).
|
|
|
|
*
|
2019-05-17 23:51:43 +00:00
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is a hexadecimal digit, false otherwise.
|
2019-05-17 23:51:43 +00:00
|
|
|
*/
|
|
|
|
bool isXDigit(int c);
|
|
|
|
|
2012-02-20 15:03:39 +00:00
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Test whether the given character is a lowercase letter (a-z).
|
|
|
|
*
|
2012-02-20 15:03:39 +00:00
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is a lowercase letter, false otherwise.
|
2012-02-20 15:03:39 +00:00
|
|
|
*/
|
|
|
|
bool isLower(int c);
|
|
|
|
|
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Test whether the given character is a whitespace.
|
|
|
|
*
|
|
|
|
* The following characters are considered a whitespace:
|
|
|
|
* @code
|
|
|
|
* ' ', '\t', '\r', '\n', '\v', '\f'
|
|
|
|
* @endcode
|
2012-02-20 15:03:39 +00:00
|
|
|
*
|
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is a whitespace, false otherwise.
|
2012-02-20 15:03:39 +00:00
|
|
|
*/
|
|
|
|
bool isSpace(int c);
|
|
|
|
|
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Test whether the given character is an uppercase letter (A-Z).
|
|
|
|
*
|
2012-02-20 15:03:39 +00:00
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is an uppercase letter, false otherwise.
|
2012-02-20 15:03:39 +00:00
|
|
|
*/
|
|
|
|
bool isUpper(int c);
|
|
|
|
|
2012-12-13 19:52:09 +00:00
|
|
|
/**
|
|
|
|
* Test whether the given character is printable. This includes the space
|
|
|
|
* character (' ').
|
|
|
|
*
|
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is printable, false otherwise.
|
2012-12-13 19:52:09 +00:00
|
|
|
*/
|
|
|
|
bool isPrint(int c);
|
2016-05-14 19:48:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-10-09 13:02:02 +00:00
|
|
|
* Test whether the given character is a punctuation character,
|
2019-07-24 14:15:57 +00:00
|
|
|
* (i.e. not alphanumeric).
|
2016-05-14 19:48:15 +00:00
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is punctuation, false otherwise.
|
2016-05-14 19:48:15 +00:00
|
|
|
*/
|
|
|
|
bool isPunct(int c);
|
|
|
|
|
2019-07-24 14:15:57 +00:00
|
|
|
/**
|
|
|
|
* Test whether the given character is a control character.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is a control character, false otherwise.
|
2019-07-24 14:15:57 +00:00
|
|
|
*/
|
|
|
|
bool isCntrl(int c);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether the given character has a graphical representation.
|
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is a graphic, false otherwise.
|
2019-07-24 14:15:57 +00:00
|
|
|
*/
|
|
|
|
bool isGraph(int c);
|
|
|
|
|
2021-02-07 19:46:41 +00:00
|
|
|
/**
|
|
|
|
* Test whether the given character is blank.
|
|
|
|
*
|
|
|
|
* The following characters are considered blank:
|
|
|
|
* @code
|
|
|
|
* ' ', '\t'
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* If the parameter is outside the range of a signed or unsigned char, then
|
|
|
|
* false is returned.
|
|
|
|
*
|
|
|
|
* @param c The character to test.
|
|
|
|
*
|
|
|
|
* @return True if the character is blank, false otherwise.
|
|
|
|
*/
|
|
|
|
bool isBlank(int c);
|
|
|
|
|
2019-07-15 11:35:24 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-11 18:48:06 +00:00
|
|
|
* Represent the size of a file in bytes as a number with floating point and
|
2019-07-15 11:35:24 +00:00
|
|
|
* largest suitable units. For example, 1474560 bytes as 1.4 MB.
|
2021-05-04 08:45:03 +00:00
|
|
|
*
|
2020-11-11 18:48:06 +00:00
|
|
|
* @param[in] bytes Size in bytes to be represented.
|
|
|
|
* @param[out] unitsOut String with units.
|
|
|
|
*
|
|
|
|
* @note Use @c _() to translate units correctly.
|
|
|
|
*
|
|
|
|
* @return String with a floating point number representing the given size.
|
2019-07-15 11:35:24 +00:00
|
|
|
*/
|
|
|
|
Common::String getHumanReadableBytes(uint64 bytes, Common::String &unitsOut);
|
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/** @} */
|
|
|
|
|
2012-12-13 19:52:09 +00:00
|
|
|
} // End of namespace Common
|
2003-10-04 11:50:21 +00:00
|
|
|
|
2002-07-12 16:24:11 +00:00
|
|
|
#endif
|