2014-04-20 09:14:15 +00:00
|
|
|
|
// This file is part of libAthena.
|
2013-02-16 18:28:30 +00:00
|
|
|
|
//
|
2014-04-20 09:14:15 +00:00
|
|
|
|
// libAthena is free software: you can redistribute it and/or modify
|
2013-02-16 18:28:30 +00:00
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
2014-04-20 09:14:15 +00:00
|
|
|
|
// libAthena is distributed in the hope that it will be useful,
|
2013-02-16 18:28:30 +00:00
|
|
|
|
// 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
|
2014-04-20 09:14:15 +00:00
|
|
|
|
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
|
2013-02-16 18:28:30 +00:00
|
|
|
|
|
2013-01-26 20:19:24 +00:00
|
|
|
|
#ifndef __UTILITY_H__
|
|
|
|
|
#define __UTILITY_H__
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2013-09-09 03:36:54 +00:00
|
|
|
|
#include <vector>
|
2014-05-31 00:40:35 +00:00
|
|
|
|
#include <stdarg.h>
|
2015-04-11 04:20:18 +00:00
|
|
|
|
#include <string.h>
|
2014-12-29 04:46:43 +00:00
|
|
|
|
#include "Athena/Global.hpp"
|
2014-09-29 03:33:36 +00:00
|
|
|
|
#include "Athena/Types.hpp"
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
2014-04-20 09:14:15 +00:00
|
|
|
|
namespace Athena
|
2013-07-22 03:06:54 +00:00
|
|
|
|
{
|
|
|
|
|
namespace utility
|
|
|
|
|
{
|
2015-04-11 04:12:42 +00:00
|
|
|
|
inline bool isEmpty(atInt8* buf, atUint32 size) {return !memcmp(buf, buf + 1, size - 1);}
|
|
|
|
|
bool isSystemBigEndian();
|
2013-07-22 03:06:54 +00:00
|
|
|
|
|
2015-05-19 03:24:56 +00:00
|
|
|
|
inline atInt16 swap16(atInt16 val)
|
2015-04-11 04:12:42 +00:00
|
|
|
|
{
|
|
|
|
|
#if __GNUC__
|
|
|
|
|
return __builtin_bswap16(val);
|
|
|
|
|
#elif _WIN32
|
|
|
|
|
return _byteswap_ushort(val);
|
|
|
|
|
#else
|
|
|
|
|
return (val = (val << 8) | ((val >> 8) & 0xFF));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
inline atUint16 swapU16(atUint16 val) {return (atUint16)swap16(val);}
|
2015-05-19 03:24:56 +00:00
|
|
|
|
inline atInt32 swap32(atInt32 val)
|
2015-04-11 04:12:42 +00:00
|
|
|
|
{
|
|
|
|
|
#if __GNUC__
|
|
|
|
|
return __builtin_bswap32(val);
|
|
|
|
|
#elif _WIN32
|
|
|
|
|
return _byteswap_ulong(val);
|
|
|
|
|
#else
|
|
|
|
|
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
|
|
|
|
|
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
|
|
|
|
|
return val;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
inline atUint32 swapU32(atUint32 val) {return (atUint32)swap32(val);}
|
2015-05-19 03:24:56 +00:00
|
|
|
|
inline atInt64 swap64(atInt64 val)
|
2015-04-11 04:12:42 +00:00
|
|
|
|
{
|
|
|
|
|
#if __GNUC__
|
|
|
|
|
return __builtin_bswap64(val);
|
|
|
|
|
#elif _WIN32
|
|
|
|
|
return _byteswap_uint64(val);
|
|
|
|
|
#else
|
|
|
|
|
return (val = ((atInt64)((((atInt64)(val) & 0xFF00000000000000ULL) >> 56) |
|
|
|
|
|
(((atInt64)(val) & 0x00FF000000000000ULL) >> 40) |
|
|
|
|
|
(((atInt64)(val) & 0x0000FF0000000000ULL) >> 24) |
|
|
|
|
|
(((atInt64)(val) & 0x000000FF00000000ULL) >> 8) |
|
|
|
|
|
(((atInt64)(val) & 0x00000000FF000000ULL) << 8) |
|
|
|
|
|
(((atInt64)(val) & 0x0000000000FF0000ULL) << 24) |
|
|
|
|
|
(((atInt64)(val) & 0x000000000000FF00ULL) << 40) |
|
|
|
|
|
(((atInt64)(val) & 0x00000000000000FFULL) << 56))));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
inline atUint64 swapU64(atUint64 val) {return (atUint64)swap64(val);}
|
|
|
|
|
inline float swapFloat(float val)
|
|
|
|
|
{
|
2015-05-18 21:03:13 +00:00
|
|
|
|
atInt32 ival = swap64(static_cast<atInt32>(val));
|
|
|
|
|
return static_cast<float>(ival);
|
2015-04-11 04:12:42 +00:00
|
|
|
|
}
|
|
|
|
|
inline double swapDouble(double val)
|
|
|
|
|
{
|
2015-05-18 21:03:13 +00:00
|
|
|
|
atInt64 ival = swap64(static_cast<atInt64>(val));
|
|
|
|
|
return static_cast<double>(ival);
|
2015-04-11 04:12:42 +00:00
|
|
|
|
}
|
|
|
|
|
inline atInt16 LittleInt16(atInt16& val)
|
|
|
|
|
{
|
|
|
|
|
if (Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swap16(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atUint16 LittleUint16(atUint16& val)
|
|
|
|
|
{
|
|
|
|
|
atInt16 ret = val;
|
|
|
|
|
LittleInt16(ret);
|
|
|
|
|
val = ret;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atInt16 BigInt16(atInt16& val)
|
|
|
|
|
{
|
|
|
|
|
if (!Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swap16(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atUint16 BigUint16(atUint16& val)
|
|
|
|
|
{
|
|
|
|
|
atInt16 ret = val;
|
|
|
|
|
BigInt16(ret);
|
|
|
|
|
val = ret;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atInt32 LittleInt32(atInt32& val)
|
|
|
|
|
{
|
|
|
|
|
if (Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swap32(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atUint32 LittleUint32(atUint32& val)
|
|
|
|
|
{
|
|
|
|
|
atInt32 ret = val;
|
|
|
|
|
LittleInt32(ret);
|
|
|
|
|
val = ret;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atInt32 BigInt32(atInt32& val)
|
|
|
|
|
{
|
|
|
|
|
if (!Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swap32(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atUint32 BigUint32(atUint32& val)
|
|
|
|
|
{
|
|
|
|
|
atInt32 ret = val;
|
|
|
|
|
BigInt32(ret);
|
|
|
|
|
val = ret;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atInt64 LittleInt64(atInt64& val)
|
|
|
|
|
{
|
|
|
|
|
if (Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swap64(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atUint64 LittleUint64(atUint64& val)
|
|
|
|
|
{
|
|
|
|
|
atInt64 ret = val;
|
|
|
|
|
LittleInt64(ret);
|
|
|
|
|
val = ret;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atInt64 BigInt64(atInt64& val)
|
|
|
|
|
{
|
|
|
|
|
if (!Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swap64(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline atUint64 BigUint64(atUint64& val)
|
|
|
|
|
{
|
|
|
|
|
atInt64 ret = val;
|
|
|
|
|
BigInt64(ret);
|
|
|
|
|
val = ret;
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
2014-09-09 10:19:19 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
inline float LittleFloat(float& val)
|
|
|
|
|
{
|
|
|
|
|
if (Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swapFloat(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline float BigFloat(float& val)
|
|
|
|
|
{
|
|
|
|
|
if (!Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swapFloat(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline double LittleDouble(double& val)
|
|
|
|
|
{
|
|
|
|
|
if (Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swapDouble(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
inline double BigDouble(double& val)
|
|
|
|
|
{
|
|
|
|
|
if (!Athena::utility::isSystemBigEndian())
|
|
|
|
|
val = Athena::utility::swapDouble(val);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
|
2015-04-11 04:12:42 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
2014-06-30 00:26:34 +00:00
|
|
|
|
void fillRandom(atUint8 * rndArea, atUint64 count);
|
2013-09-09 03:36:54 +00:00
|
|
|
|
std::vector<std::string> split(const std::string &s, char delim);
|
2015-05-18 21:03:13 +00:00
|
|
|
|
atUint64 rand64();
|
2014-01-19 04:01:31 +00:00
|
|
|
|
std::string join(const std::vector<std::string>& elems, const std::string& delims);
|
2013-09-09 03:36:54 +00:00
|
|
|
|
void tolower(std::string& str);
|
|
|
|
|
void toupper(std::string& str);
|
2014-05-31 00:40:35 +00:00
|
|
|
|
std::string vsprintf(const char* fmt, va_list list);
|
|
|
|
|
std::string sprintf(const char* fmt, ...);
|
2014-01-11 18:27:29 +00:00
|
|
|
|
bool parseBool(const std::string& boolean, bool* valid = NULL);
|
2013-09-09 03:36:54 +00:00
|
|
|
|
|
2014-01-10 21:13:06 +00:00
|
|
|
|
int countChar(const std::string& str, const char chr, int* lastOccur = NULL);
|
2013-09-09 03:36:54 +00:00
|
|
|
|
|
2014-11-29 03:32:37 +00:00
|
|
|
|
// trim from start
|
2015-05-19 03:24:56 +00:00
|
|
|
|
std::string& ltrim(std::string& s);
|
2014-11-29 03:32:37 +00:00
|
|
|
|
|
|
|
|
|
// trim from end
|
2015-05-19 03:24:56 +00:00
|
|
|
|
std::string& rtrim(std::string& s);
|
2014-11-29 03:32:37 +00:00
|
|
|
|
|
|
|
|
|
// trim from both ends
|
2015-05-19 03:24:56 +00:00
|
|
|
|
std::string& trim(std::string& s);
|
2014-12-28 22:38:55 +00:00
|
|
|
|
atUint64 fileSize(const std::string& filename);
|
2013-07-22 03:06:54 +00:00
|
|
|
|
} // utility
|
2014-04-20 09:14:15 +00:00
|
|
|
|
} // Athena
|
2013-01-26 20:19:24 +00:00
|
|
|
|
#endif
|