📄 util.h
字号:
ret[i] = '/';
}
}
return ret;
}
static string toNmdcFile(const string& file) {
if(file.empty())
return Util::emptyString;
string ret(file.substr(1));
for(string::size_type i = 0; i < ret.length(); ++i) {
if(ret[i] == '/') {
ret[i] = '\\';
}
}
return ret;
}
static string formatBytes(int64_t aBytes);
static string formatExactSize(int64_t aBytes);
static string formatSeconds(int64_t aSec) {
char buf[64];
sprintf(buf, "%01lu:%02d:%02d", (unsigned long)(aSec / (60*60)), (int)((aSec / 60) % 60), (int)(aSec % 60));
return buf;
}
static string formatParams(const string& msg, StringMap& params, bool filter);
static string formatTime(const string &msg, const time_t t);
static int64_t toInt64(const string& aString) {
#ifdef _WIN32
return _atoi64(aString.c_str());
#else
return atoll(aString.c_str());
#endif
}
static int toInt(const string& aString) {
return atoi(aString.c_str());
}
static u_int32_t toUInt32(const string& str) {
return toUInt32(str.c_str());
}
static u_int32_t toUInt32(const char* c) {
return (u_int32_t)atoi(c);
}
static double toDouble(const string& aString) {
// Work-around for atof and locales...
lconv* lv = localeconv();
string::size_type i = aString.find_last_of(".,");
if(i != string::npos && aString[i] != lv->decimal_point[0]) {
string tmp(aString);
tmp[i] = lv->decimal_point[0];
return atof(tmp.c_str());
}
return atof(aString.c_str());
}
static float toFloat(const string& aString) {
return (float)toDouble(aString.c_str());
}
static string toString(short val) {
char buf[8];
sprintf(buf, "%d", (int)val);
return buf;
}
static string toString(unsigned short val) {
char buf[8];
sprintf(buf, "%u", (unsigned int)val);
return buf;
}
static string toString(int val) {
char buf[16];
sprintf(buf, "%d", val);
return buf;
}
static string toString(unsigned int val) {
char buf[16];
sprintf(buf, "%u", val);
return buf;
}
static string toString(long val) {
char buf[32];
sprintf(buf, "%ld", val);
return buf;
}
static string toString(unsigned long val) {
char buf[32];
sprintf(buf, "%lu", val);
return buf;
}
static string toString(long long val) {
char buf[32];
#ifdef _MSC_VER
sprintf(buf, "%I64d", val);
#else
sprintf(buf, "%lld", val);
#endif
return buf;
}
static string toString(unsigned long long val) {
char buf[32];
#ifdef _MSC_VER
sprintf(buf, "%I64u", val);
#else
sprintf(buf, "%llu", val);
#endif
return buf;
}
static string toString(double val) {
char buf[16];
sprintf(buf, "%0.2f", val);
return buf;
}
static string toString(const StringList& lst) {
if(lst.size() == 1)
return lst[0];
string tmp("[");
for(StringList::const_iterator i = lst.begin(); i != lst.end(); ++i) {
tmp += *i + ',';
}
if(tmp.length() == 1)
tmp.push_back(']');
else
tmp[tmp.length()-1] = ']';
return tmp;
}
static string toHexEscape(char val) {
char buf[sizeof(int)*2+1+1];
sprintf(buf, "%%%X", val&0x0FF);
return buf;
}
static char fromHexEscape(const string aString) {
unsigned int res = 0;
sscanf(aString.c_str(), "%X", &res);
return static_cast<char>(res);
}
template<typename T>
static T& intersect(T& t1, const T& t2) {
for(typename T::iterator i = t1.begin(); i != t1.end();) {
if(find_if(t2.begin(), t2.end(), bind1st(equal_to<typename T::value_type>(), *i)) == t2.end())
i = t1.erase(i);
else
++i;
}
return t1;
}
static string encodeURI(const string& /*aString*/, bool reverse = false);
static string getLocalIp();
static bool isPrivateIp(string const& ip);
/**
* Case insensitive substring search.
* @return First position found or string::npos
*/
static string::size_type findSubString(const string& aString, const string& aSubString, string::size_type start = 0) throw();
static wstring::size_type findSubString(const wstring& aString, const wstring& aSubString, wstring::size_type start = 0) throw();
/* Utf-8 versions of strnicmp and stricmp, unicode char code order (!) */
static int stricmp(const char* a, const char* b);
static int strnicmp(const char* a, const char* b, size_t n);
static int stricmp(const wchar_t* a, const wchar_t* b) {
while(*a && Text::toLower(*a) == Text::toLower(*b))
++a, ++b;
return ((int)Text::toLower(*a)) - ((int)Text::toLower(*b));
}
static int strnicmp(const wchar_t* a, const wchar_t* b, size_t n) {
while(n && *a && Text::toLower(*a) == Text::toLower(*b))
--n, ++a, ++b;
return n == 0 ? 0 : ((int)Text::toLower(*a)) - ((int)Text::toLower(*b));
}
static int stricmp(const string& a, const string& b) { return stricmp(a.c_str(), b.c_str()); }
static int strnicmp(const string& a, const string& b, size_t n) { return strnicmp(a.c_str(), b.c_str(), n); }
static int stricmp(const wstring& a, const wstring& b) { return stricmp(a.c_str(), b.c_str()); }
static int strnicmp(const wstring& a, const wstring& b, size_t n) { return strnicmp(a.c_str(), b.c_str(), n); }
static string validateMessage(string tmp, bool reverse, bool checkNewLines = true);
static string getOsVersion();
static string getIpCountry (string IP);
static bool getAway() { return away; }
static void setAway(bool aAway) {
away = aAway;
if (away)
awayTime = time(NULL);
}
static bool getManualAway() { return manualAway; }
static void setManualAway(bool aManualAway) { manualAway = aManualAway; }
static string getAwayMessage();
static void setAwayMessage(const string& aMsg) { awayMsg = aMsg; }
static u_int32_t rand();
static u_int32_t rand(u_int32_t high) { return rand() % high; }
static u_int32_t rand(u_int32_t low, u_int32_t high) { return rand(high-low) + low; }
static double randd() { return ((double)rand()) / ((double)0xffffffff); }
private:
static string appPath;
static string dataPath;
static bool away;
static bool manualAway;
static string awayMsg;
static time_t awayTime;
typedef map<u_int32_t, u_int16_t> CountryList;
typedef CountryList::iterator CountryIter;
static CountryList countries;
};
/** Case insensitive hash function for strings */
struct noCaseStringHash {
#if _MSC_VER < 1300
enum {bucket_size = 4};
enum {min_buckets = 8};
#else
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
#endif // _MSC_VER == 1200
size_t operator()(const string* s) const {
return operator()(*s);
}
size_t operator()(const string& s) const {
size_t x = 0;
const char* end = s.data() + s.size();
for(const char* str = s.data(); str < end; ) {
wchar_t c = 0;
int n = Text::utf8ToWc(str, c);
if(n < 0) {
x = x*32 - x + '_';
str += abs(n);
} else {
x = x*32 - x + (size_t)Text::toLower(c);
str += n;
}
}
return x;
}
size_t operator()(const wstring* s) const {
return operator()(*s);
}
size_t operator()(const wstring& s) const {
size_t x = 0;
const wchar_t* y = s.data();
wstring::size_type j = s.size();
for(wstring::size_type i = 0; i < j; ++i) {
x = x*31 + (size_t)Text::toLower(y[i]);
}
return x;
}
};
/** Case insensitive string comparison */
struct noCaseStringEq {
bool operator()(const string* a, const string* b) const {
return a == b || Util::stricmp(*a, *b) == 0;
}
bool operator()(const string& a, const string& b) const {
return Util::stricmp(a, b) == 0;
}
bool operator()(const wstring* a, const wstring* b) const {
return a == b || Util::stricmp(*a, *b) == 0;
}
bool operator()(const wstring& a, const wstring& b) const {
return Util::stricmp(a, b) == 0;
}
};
/** Case insensitive string ordering */
struct noCaseStringLess {
bool operator()(const string* a, const string* b) const {
return Util::stricmp(*a, *b) < 0;
}
bool operator()(const string& a, const string& b) const {
return Util::stricmp(a, b) < 0;
}
bool operator()(const wstring* a, const wstring* b) const {
return Util::stricmp(*a, *b) < 0;
}
bool operator()(const wstring& a, const wstring& b) const {
return Util::stricmp(a, b) < 0;
}
};
#endif // !defined(UTIL_H)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -