📄 util.h
字号:
/*
* Copyright (C) 2001-2006 Jacek Sieka, arnetheduck on gmail point com
*
* 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
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if !defined(UTIL_H)
#define UTIL_H
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef _WIN32
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#endif
#include "Text.h"
template<typename T, bool flag> struct ReferenceSelector {
typedef T ResultType;
};
template<typename T> struct ReferenceSelector<T,true> {
typedef const T& ResultType;
};
template<typename T> class IsOfClassType {
public:
template<typename U> static char check(int U::*);
template<typename U> static float check(...);
public:
enum { Result = sizeof(check<T>(0)) };
};
template<typename T> struct TypeTraits {
typedef IsOfClassType<T> ClassType;
typedef ReferenceSelector<T, ((ClassType::Result == 1) || (sizeof(T) > sizeof(char*)) ) > Selector;
typedef typename Selector::ResultType ParameterType;
};
#define GETSET(type, name, name2) \
private: type name; \
public: TypeTraits<type>::ParameterType get##name2() const { return name; } \
void set##name2(TypeTraits<type>::ParameterType a##name2) { name = a##name2; }
#define LIT(x) x, (sizeof(x)-1)
/** Evaluates op(pair<T1, T2>.first, compareTo) */
template<class T1, class T2, class op = equal_to<T1> >
class CompareFirst {
public:
CompareFirst(const T1& compareTo) : a(compareTo) { }
bool operator()(const pair<T1, T2>& p) { return op()(p.first, a); }
private:
CompareFirst& operator=(const CompareFirst&);
const T1& a;
};
/** Evaluates op(pair<T1, T2>.second, compareTo) */
template<class T1, class T2, class op = equal_to<T2> >
class CompareSecond {
public:
CompareSecond(const T2& compareTo) : a(compareTo) { }
bool operator()(const pair<T1, T2>& p) { return op()(p.second, a); }
private:
CompareSecond& operator=(const CompareSecond&);
const T2& a;
};
template<class T>
struct PointerHash {
#if _MSC_VER >= 1300
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
#endif
size_t operator()(const T* a) const { return ((size_t)a)/sizeof(T); }
bool operator()(const T* a, const T* b) { return a < b; }
};
template<>
struct PointerHash<void> {
size_t operator()(const void* a) const { return ((size_t)a)>>2; }
};
/**
* Compares two values
* @return -1 if v1 < v2, 0 if v1 == v2 and 1 if v1 > v2
*/
template<typename T1>
inline int compare(const T1& v1, const T1& v2) { return (v1 < v2) ? -1 : ((v1 == v2) ? 0 : 1); }
class Flags {
public:
typedef int MaskType;
Flags() : flags(0) { }
Flags(const Flags& rhs) : flags(rhs.flags) { }
Flags(MaskType f) : flags(f) { }
bool isSet(MaskType aFlag) const { return (flags & aFlag) == aFlag; }
bool isAnySet(MaskType aFlag) const { return (flags & aFlag) != 0; }
void setFlag(MaskType aFlag) { flags |= aFlag; }
void unsetFlag(MaskType aFlag) { flags &= ~aFlag; }
Flags& operator=(const Flags& rhs) { flags = rhs.flags; return *this; }
private:
MaskType flags;
};
template<typename T>
class AutoArray {
typedef T* TPtr;
public:
explicit AutoArray(TPtr t) : p(t) { }
explicit AutoArray(size_t size) : p(new T[size]) { }
~AutoArray() { delete[] p; }
operator TPtr() { return p; }
AutoArray& operator=(TPtr t) { delete[] p; p = t; return *this; }
private:
AutoArray(const AutoArray&);
AutoArray& operator=(const AutoArray&);
TPtr p;
};
class Util
{
public:
static tstring emptyStringT;
static string emptyString;
static wstring emptyStringW;
static void initialize();
/**
* Get the path to the application executable.
* This is mainly intended for use on Windows.
*
* @return Path to executable file.
*/
static string getAppPath() { return appPath; }
static string getAppName() {
#ifdef _WIN32
TCHAR buf[MAX_PATH+1];
DWORD x = GetModuleFileName(NULL, buf, MAX_PATH);
return Text::wideToUtf8(wstring(buf, x));
#else // _WIN32
char buf[PATH_MAX + 1];
int n;
n = readlink("/proc/self/exe", buf, PATH_MAX);
if (n == -1) {
return emptyString;
}
buf[n] = '\0';
return string(buf);
#endif // _WIN32
}
/** Path of temporary storage */
static string getTempPath() {
#ifdef _WIN32
TCHAR buf[MAX_PATH + 1];
DWORD x = GetTempPath(MAX_PATH, buf);
return Text::wideToUtf8(wstring(buf, x));
#else
return "/tmp/";
#endif
}
/** Path of resource directory */
static string getDataPath();
/** Path of configuration files */
static string getConfigPath();
/** Path of file lists */
static string getListPath() {
return getConfigPath() + "FileLists" PATH_SEPARATOR_STR;
}
/** Notepad filename */
static string getNotepadFile() {
return getConfigPath() + "Notepad.txt";
}
static string translateError(int aError) {
#ifdef _WIN32
LPVOID lpMsgBuf;
DWORD chars = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
aError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
if(chars == 0) {
return string();
}
string tmp = Text::fromT((LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
string::size_type i = 0;
while( (i = tmp.find_first_of("\r\n", i)) != string::npos) {
tmp.erase(i, 1);
}
return tmp;
#else // _WIN32
return strerror(aError);
#endif // _WIN32
}
static string getFilePath(const string& path) {
string::size_type i = path.rfind(PATH_SEPARATOR);
return (i != string::npos) ? path.substr(0, i + 1) : path;
}
static string getFileName(const string& path) {
string::size_type i = path.rfind(PATH_SEPARATOR);
return (i != string::npos) ? path.substr(i + 1) : path;
}
static string getFileExt(const string& path) {
string::size_type i = path.rfind('.');
return (i != string::npos) ? path.substr(i) : Util::emptyString;
}
static string getLastDir(const string& path) {
string::size_type i = path.rfind(PATH_SEPARATOR);
if(i == string::npos)
return Util::emptyString;
string::size_type j = path.rfind(PATH_SEPARATOR, i-1);
return (j != string::npos) ? path.substr(j+1, i-j-1) : path;
}
static wstring getFilePath(const wstring& path) {
wstring::size_type i = path.rfind(PATH_SEPARATOR);
return (i != wstring::npos) ? path.substr(0, i + 1) : path;
}
static wstring getFileName(const wstring& path) {
wstring::size_type i = path.rfind(PATH_SEPARATOR);
return (i != wstring::npos) ? path.substr(i + 1) : path;
}
static wstring getFileExt(const wstring& path) {
wstring::size_type i = path.rfind('.');
return (i != wstring::npos) ? path.substr(i) : Util::emptyStringW;
}
static wstring getLastDir(const wstring& path) {
wstring::size_type i = path.rfind(PATH_SEPARATOR);
if(i == wstring::npos)
return Util::emptyStringW;
wstring::size_type j = path.rfind(PATH_SEPARATOR, i-1);
return (j != wstring::npos) ? path.substr(j+1, i-j-1) : path;
}
static void decodeUrl(const string& aUrl, string& aServer, u_int16_t& aPort, string& aFile);
static string validateFileName(string aFile);
static string cleanPathChars(string aNick);
static string formatBytes(const string& aString) {
return formatBytes(toInt64(aString));
}
static double toBytes(TCHAR* aSize);
static string toDOS(const string& tmp);
static string getShortTimeString(time_t t = time(NULL) );
static string getTimeString() {
char buf[64];
time_t _tt;
time(&_tt);
tm* _tm = localtime(&_tt);
if(_tm == NULL) {
strcpy(buf, "xx:xx:xx");
} else {
strftime(buf, 64, "%X", _tm);
}
return buf;
}
static string toAdcFile(const string& file) {
if(file == "files.xml.bz2" || file == "MyList.DcLst")
return file;
string ret;
ret.reserve(file.length() + 1);
ret += '/';
ret += file;
for(string::size_type i = 0; i < ret.length(); ++i) {
if(ret[i] == '\\') {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -