nod/include/NOD/Util.hpp

326 lines
8.5 KiB
C++
Raw Normal View History

2015-06-28 05:43:53 +00:00
#ifndef __NOD_UTIL_HPP__
#define __NOD_UTIL_HPP__
2015-07-14 00:38:16 +00:00
#if _WIN32 && UNICODE
#include <wctype.h>
#include <direct.h>
2015-08-31 21:25:55 +00:00
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
2015-09-27 04:35:00 +00:00
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
2015-07-14 00:38:16 +00:00
#else
#include <ctype.h>
2016-01-21 06:30:37 +00:00
#include <sys/file.h>
2016-01-22 23:45:58 +00:00
#include <unistd.h>
2016-01-23 23:36:58 +00:00
#include <errno.h>
2016-01-25 02:00:02 +00:00
#include <sys/param.h>
#include <sys/statvfs.h>
2015-07-14 00:38:16 +00:00
#endif
#include <sys/stat.h>
2015-07-02 18:33:55 +00:00
#include <string>
2016-01-24 05:06:54 +00:00
#include <cstring>
2015-07-02 18:33:55 +00:00
#include <algorithm>
#include <LogVisor/LogVisor.hpp>
2016-01-24 05:06:54 +00:00
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#include <sys/stat.h>
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
#if !defined(S_ISLNK)
#define S_ISLNK(m) 0
#endif
#endif
#undef min
#undef max
2015-07-02 18:33:55 +00:00
2015-06-28 05:43:53 +00:00
namespace NOD
{
2016-01-24 05:06:54 +00:00
/* define our own min/max to avoid MSVC BS */
template<typename T>
inline T min(T a, T b) { return a < b ? a : b; }
template<typename T>
inline T max(T a, T b) { return a > b ? a : b; }
2015-06-28 05:43:53 +00:00
/* Log Module */
extern LogVisor::LogModule LogModule;
2015-07-02 20:37:07 +00:00
/* filesystem char type */
2015-07-02 18:33:55 +00:00
#if _WIN32 && UNICODE
#define NOD_UCS2 1
2015-07-02 20:37:07 +00:00
typedef struct _stat Sstat;
static inline int Mkdir(const wchar_t* path, int) {return _wmkdir(path);}
static inline int Stat(const wchar_t* path, Sstat* statout) {return _wstat(path, statout);}
#else
typedef struct stat Sstat;
static inline int Mkdir(const char* path, mode_t mode) {return mkdir(path, mode);}
static inline int Stat(const char* path, Sstat* statout) {return stat(path, statout);}
2015-07-02 18:33:55 +00:00
#endif
/* String-converting views */
#if NOD_UCS2
typedef wchar_t SystemChar;
typedef std::wstring SystemString;
static inline void ToLower(SystemString& str)
{std::transform(str.begin(), str.end(), str.begin(), towlower);}
static inline void ToUpper(SystemString& str)
{std::transform(str.begin(), str.end(), str.begin(), towupper);}
2016-01-24 05:06:54 +00:00
static inline size_t StrLen(const SystemChar* str) {return wcslen(str);}
2015-07-02 18:33:55 +00:00
class SystemUTF8View
{
std::string m_utf8;
public:
SystemUTF8View(const SystemString& str)
2015-08-31 21:25:55 +00:00
{
int len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0, nullptr, nullptr);
2015-08-31 21:43:23 +00:00
m_utf8.assign(len, '\0');
2015-08-31 21:25:55 +00:00
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), &m_utf8[0], len, nullptr, nullptr);
}
2015-07-02 18:33:55 +00:00
inline const std::string& utf8_str() {return m_utf8;}
};
class SystemStringView
{
std::wstring m_sys;
public:
SystemStringView(const std::string& str)
2015-08-31 21:25:55 +00:00
{
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0);
2015-08-31 21:43:23 +00:00
m_sys.assign(len, L'\0');
2015-08-31 21:25:55 +00:00
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), &m_sys[0], len);
}
2015-07-02 18:33:55 +00:00
inline const std::wstring& sys_str() {return m_sys;}
};
#ifndef _S
#define _S(val) L ## val
#endif
#else
typedef char SystemChar;
typedef std::string SystemString;
static inline void ToLower(SystemString& str)
{std::transform(str.begin(), str.end(), str.begin(), tolower);}
static inline void ToUpper(SystemString& str)
{std::transform(str.begin(), str.end(), str.begin(), toupper);}
static inline size_t StrLen(const SystemChar* str) {return strlen(str);}
2015-07-02 18:33:55 +00:00
class SystemUTF8View
{
const std::string& m_utf8;
public:
SystemUTF8View(const SystemString& str)
: m_utf8(str) {}
inline const std::string& utf8_str() {return m_utf8;}
};
class SystemStringView
{
const std::string& m_sys;
public:
SystemStringView(const std::string& str)
: m_sys(str) {}
inline const std::string& sys_str() {return m_sys;}
};
#ifndef _S
#define _S(val) val
#endif
#endif
2016-01-22 23:45:58 +00:00
static inline void Unlink(const SystemChar* file)
{
2016-01-24 05:06:54 +00:00
#if NOD_UCS2
2016-01-22 23:45:58 +00:00
_wunlink(file);
#else
unlink(file);
#endif
}
static inline int StrCmp(const SystemChar* str1, const SystemChar* str2)
{
2016-01-24 05:06:54 +00:00
#if NOD_UCS2
return wcscmp(str1, str2);
#else
return strcmp(str1, str2);
#endif
}
static inline int StrCaseCmp(const SystemChar* str1, const SystemChar* str2)
{
2016-01-24 05:06:54 +00:00
#if NOD_UCS2
return _wcsicmp(str1, str2);
#else
return strcasecmp(str1, str2);
#endif
}
2015-09-13 03:41:19 +00:00
#undef bswap16
#undef bswap32
#undef bswap64
2015-06-28 05:43:53 +00:00
/* Type-sensitive byte swappers */
template <typename T>
static inline T bswap16(T val)
{
#if __GNUC__
return __builtin_bswap16(val);
#elif _WIN32
return _byteswap_ushort(val);
#else
return (val = (val << 8) | ((val >> 8) & 0xFF));
#endif
}
template <typename T>
static inline T bswap32(T val)
{
#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
}
template <typename T>
static inline T bswap64(T val)
{
#if __GNUC__
return __builtin_bswap64(val);
#elif _WIN32
return _byteswap_uint64(val);
#else
return ((val & 0xFF00000000000000ULL) >> 56) |
((val & 0x00FF000000000000ULL) >> 40) |
((val & 0x0000FF0000000000ULL) >> 24) |
((val & 0x000000FF00000000ULL) >> 8) |
((val & 0x00000000FF000000ULL) << 8) |
((val & 0x0000000000FF0000ULL) << 24) |
((val & 0x000000000000FF00ULL) << 40) |
((val & 0x00000000000000FFULL) << 56);
#endif
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
static inline int16_t SBig(int16_t val) {return bswap16(val);}
static inline uint16_t SBig(uint16_t val) {return bswap16(val);}
static inline int32_t SBig(int32_t val) {return bswap32(val);}
static inline uint32_t SBig(uint32_t val) {return bswap32(val);}
static inline int64_t SBig(int64_t val) {return bswap64(val);}
static inline uint64_t SBig(uint64_t val) {return bswap64(val);}
static inline int16_t SLittle(int16_t val) {return val;}
static inline uint16_t SLittle(uint16_t val) {return val;}
static inline int32_t SLittle(int32_t val) {return val;}
static inline uint32_t SLittle(uint32_t val) {return val;}
static inline int64_t SLittle(int64_t val) {return val;}
static inline uint64_t SLittle(uint64_t val) {return val;}
#else
static inline int16_t SLittle(int16_t val) {return bswap16(val);}
static inline uint16_t SLittle(uint16_t val) {return bswap16(val);}
static inline int32_t SLittle(int32_t val) {return bswap32(val);}
static inline uint32_t SLittle(uint32_t val) {return bswap32(val);}
static inline int64_t SLittle(int64_t val) {return bswap64(val);}
static inline uint64_t SLittle(uint64_t val) {return bswap64(val);}
static inline int16_t SBig(int16_t val) {return val;}
static inline uint16_t SBig(uint16_t val) {return val;}
static inline int32_t SBig(int32_t val) {return val;}
static inline uint32_t SBig(uint32_t val) {return val;}
static inline int64_t SBig(int64_t val) {return val;}
static inline uint64_t SBig(uint64_t val) {return val;}
#endif
2016-01-21 06:30:37 +00:00
#ifndef ROUND_UP_32
#define ROUND_UP_32(val) (((val) + 31) & ~31)
#define ROUND_UP_16(val) (((val) + 15) & ~15)
#endif
enum class FileLockType
{
None = 0,
Read,
Write
};
static inline FILE* Fopen(const SystemChar* path, const SystemChar* mode, FileLockType lock=FileLockType::None)
{
#if NOD_UCS2
FILE* fp = _wfopen(path, mode);
if (!fp)
return nullptr;
#else
FILE* fp = fopen(path, mode);
if (!fp)
return nullptr;
#endif
if (lock != FileLockType::None)
{
#if _WIN32
OVERLAPPED ov = {};
LockFileEx((HANDLE)(uintptr_t)_fileno(fp), (lock == FileLockType::Write) ? LOCKFILE_EXCLUSIVE_LOCK : 0, 0, 0, 1, &ov);
#else
if (flock(fileno(fp), ((lock == FileLockType::Write) ? LOCK_EX : LOCK_SH) | LOCK_NB))
LogModule.report(LogVisor::Error, "flock %s: %s", path, strerror(errno));
#endif
}
return fp;
}
2016-01-23 23:36:58 +00:00
static inline int FSeek(FILE* fp, int64_t offset, int whence)
{
2016-01-25 02:13:09 +00:00
#if _WIN32
2016-01-23 23:36:58 +00:00
return _fseeki64(fp, offset, whence);
#elif __APPLE__ || __FreeBSD__
return fseeko(fp, offset, whence);
#else
return fseeko64(fp, offset, whence);
#endif
}
2016-01-25 02:00:02 +00:00
static inline bool CheckFreeSpace(const SystemChar* path, size_t reqSz)
{
#if _WIN32
ULARGE_INTEGER freeBytes;
if (!GetDiskFreeSpaceExW(path, &freeBytes, nullptr, nullptr))
LogModule.report(LogVisor::FatalError, "GetDiskFreeSpaceExW %s: %d", path, GetLastError());
return reqSz < freeBytes;
#else
struct statvfs svfs;
if (statvfs(path, &svfs))
LogModule.report(LogVisor::FatalError, "statvfs %s: %s", path, strerror(errno));
return reqSz < svfs.f_bsize * svfs.f_bfree;
#endif
}
#if __GNUC__
__attribute__((__format__ (__printf__, 1, 2)))
#endif
2016-01-24 06:44:17 +00:00
static inline void Printf(const SystemChar* fmt, ...)
{
va_list args;
va_start(args, fmt);
#if NOD_UCS2
vwprintf(fmt, args);
#else
vprintf(fmt, args);
#endif
va_end(args);
}
2015-06-28 05:43:53 +00:00
}
#endif // __NOD_UTIL_HPP__