diff options
Diffstat (limited to 'src/shared/Util.cpp')
-rw-r--r-- | src/shared/Util.cpp | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/src/shared/Util.cpp b/src/shared/Util.cpp index ede7a27ea7b..0f7386e4d33 100644 --- a/src/shared/Util.cpp +++ b/src/shared/Util.cpp @@ -17,19 +17,14 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - #include "Util.h" - #include "sockets/socket_include.h" #include "utf8cpp/utf8.h" #include "mersennetwister/MersenneTwister.h" #include <ace/TSS_T.h> - typedef ACE_TSS<MTRand> MTRandTSS; static MTRandTSS mtRand; - #ifdef MULTI_THREAD_MAP - int32 irand (int32 min, int32 max) { int32 result; @@ -39,7 +34,6 @@ int32 irand (int32 min, int32 max) } return result; } - uint32 urand (uint32 min, uint32 max) { uint32 result; @@ -49,7 +43,6 @@ uint32 urand (uint32 min, uint32 max) } return result; } - int32 rand32 () { int32 result; @@ -59,7 +52,6 @@ int32 rand32 () } return result; } - double rand_norm(void) { double result; @@ -69,7 +61,6 @@ double rand_norm(void) } return result; } - double rand_chance (void) { double result; @@ -79,36 +70,28 @@ double rand_chance (void) } return result; } - #else - int32 irand (int32 min, int32 max) { return int32 (mtRand->randInt (max - min)) + min; } - uint32 urand (uint32 min, uint32 max) { return mtRand->randInt (max - min) + min; } - int32 rand32 () { return mtRand->randInt (); } - double rand_norm(void) { return mtRand->randExc (); } - double rand_chance (void) { return mtRand->randExc (100.0); } - #endif - Tokens StrSplit(const std::string &src, const std::string &sep) { Tokens r; @@ -128,13 +111,10 @@ Tokens StrSplit(const std::string &src, const std::string &sep) if (s.length()) r.push_back(s); return r; } - void stripLineInvisibleChars(std::string &str) { static std::string invChars = " \t\7\n"; - size_t wpos = 0; - bool space = false; for(size_t pos = 0; pos < str.size(); ++pos) { @@ -155,21 +135,17 @@ void stripLineInvisibleChars(std::string &str) space = false; } } - if(wpos < str.size()) str.erase(wpos,str.size()); if(str.find("|TInterface")!=std::string::npos) str.clear(); - } - std::string secsToTimeString(uint32 timeInSecs, bool shortText, bool hoursOnly) { uint32 secs = timeInSecs % MINUTE; uint32 minutes = timeInSecs % HOUR / MINUTE; uint32 hours = timeInSecs % DAY / HOUR; uint32 days = timeInSecs / DAY; - std::ostringstream ss; if(days) ss << days << (shortText ? "d" : " Day(s) "); @@ -182,16 +158,13 @@ std::string secsToTimeString(uint32 timeInSecs, bool shortText, bool hoursOnly) if(secs || (!days && !hours && !minutes) ) ss << secs << (shortText ? "s" : " Second(s)."); } - return ss.str(); } - uint32 TimeStringToSecs(const std::string& timestring) { uint32 secs = 0; uint32 buffer = 0; uint32 multiplier = 0; - for(std::string::const_iterator itr = timestring.begin(); itr != timestring.end(); itr++ ) { if(isdigit(*itr)) @@ -214,10 +187,8 @@ uint32 TimeStringToSecs(const std::string& timestring) buffer=0; } } - return secs; } - std::string TimeToTimestampStr(time_t t) { tm* aTm = localtime(&t); @@ -231,37 +202,30 @@ std::string TimeToTimestampStr(time_t t) snprintf(buf,20,"%04d-%02d-%02d_%02d-%02d-%02d",aTm->tm_year+1900,aTm->tm_mon+1,aTm->tm_mday,aTm->tm_hour,aTm->tm_min,aTm->tm_sec); return std::string(buf); } - /// Check if the string is a valid ip address representation bool IsIPAddress(char const* ipaddress) { if(!ipaddress) return false; - // Let the big boys do it. // Drawback: all valid ip address formats are recognized e.g.: 12.23,121234,0xABCD) return inet_addr(ipaddress) != INADDR_NONE; } - /// create PID file uint32 CreatePIDFile(const std::string& filename) { FILE * pid_file = fopen (filename.c_str(), "w" ); if (pid_file == NULL) return 0; - #ifdef WIN32 DWORD pid = GetCurrentProcessId(); #else pid_t pid = getpid(); #endif - fprintf(pid_file, "%d", pid ); fclose(pid_file); - return (uint32)pid; } - size_t utf8length(std::string& utf8str) { try @@ -274,7 +238,6 @@ size_t utf8length(std::string& utf8str) return 0; } } - void utf8truncate(std::string& utf8str,size_t len) { try @@ -282,7 +245,6 @@ void utf8truncate(std::string& utf8str,size_t len) size_t wlen = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size()); if(wlen <= len) return; - std::wstring wstr; wstr.resize(wlen); utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]); @@ -295,7 +257,6 @@ void utf8truncate(std::string& utf8str,size_t len) utf8str = ""; } } - bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize) { try @@ -308,7 +269,6 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize) wsize = 0; return false; } - wsize = len; utf8::utf8to16(utf8str,utf8str+csize,wstr); wstr[len] = L'\0'; @@ -320,17 +280,14 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize) wsize = 0; return false; } - return true; } - bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr) { try { size_t len = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size()); wstr.resize(len); - utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]); } catch(std::exception) @@ -338,17 +295,14 @@ bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr) wstr = L""; return false; } - return true; } - bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str) { try { std::string utf8str2; utf8str2.resize(size*4); // allocate for most long case - char* oend = utf8::utf16to8(wstr,wstr+size,&utf8str2[0]); utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail utf8str = utf8str2; @@ -358,17 +312,14 @@ bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str) utf8str = ""; return false; } - return true; } - bool WStrToUtf8(std::wstring wstr, std::string& utf8str) { try { std::string utf8str2; utf8str2.resize(wstr.size()*4); // allocate for most long case - char* oend = utf8::utf16to8(wstr.c_str(),wstr.c_str()+wstr.size(),&utf8str2[0]); utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail utf8str = utf8str2; @@ -378,20 +329,15 @@ bool WStrToUtf8(std::wstring wstr, std::string& utf8str) utf8str = ""; return false; } - return true; } - typedef wchar_t const* const* wstrlist; - std::wstring GetMainPartOfName(std::wstring wname, uint32 declension) { // supported only Cyrillic cases if(wname.size() < 1 || !isCyrillicCharacter(wname[0]) || declension > 5) return wname; - // Important: end length must be <= MAX_INTERNAL_PLAYER_NAME-MAX_PLAYER_NAME (3 currently) - static wchar_t const a_End[] = { wchar_t(1), wchar_t(0x0430),wchar_t(0x0000)}; static wchar_t const o_End[] = { wchar_t(1), wchar_t(0x043E),wchar_t(0x0000)}; static wchar_t const ya_End[] = { wchar_t(1), wchar_t(0x044F),wchar_t(0x0000)}; @@ -408,7 +354,6 @@ std::wstring GetMainPartOfName(std::wstring wname, uint32 declension) static wchar_t const ie_m_End[] = { wchar_t(2), wchar_t(0x0435),wchar_t(0x043C),wchar_t(0x0000)}; static wchar_t const soft_End[] = { wchar_t(1), wchar_t(0x044C),wchar_t(0x0000)}; static wchar_t const j_End[] = { wchar_t(1), wchar_t(0x0439),wchar_t(0x0000)}; - static wchar_t const* const dropEnds[6][8] = { { &a_End[1], &o_End[1], &ya_End[1], &ie_End[1], &soft_End[1], &j_End[1], NULL, NULL }, { &a_End[1], &ya_End[1], &yeru_End[1], &i_End[1], NULL, NULL, NULL, NULL }, @@ -417,42 +362,34 @@ std::wstring GetMainPartOfName(std::wstring wname, uint32 declension) { &oj_End[1], &io_j_End[1], &ie_j_End[1], &o_m_End[1], &io_m_End[1], &ie_m_End[1], &yu_End[1], NULL }, { &ie_End[1], &i_End[1], NULL, NULL, NULL, NULL, NULL, NULL } }; - for(wchar_t const * const* itr = &dropEnds[declension][0]; *itr; ++itr) { size_t len = size_t((*itr)[-1]); // get length from string size field - if(wname.substr(wname.size()-len,len)==*itr) return wname.substr(0,wname.size()-len); } - return wname; } - bool utf8ToConsole(const std::string& utf8str, std::string& conStr) { #if PLATFORM == PLATFORM_WINDOWS std::wstring wstr; if(!Utf8toWStr(utf8str,wstr)) return false; - conStr.resize(wstr.size()); CharToOemBuffW(&wstr[0],&conStr[0],wstr.size()); #else // not implemented yet conStr = utf8str; #endif - return true; } - bool consoleToUtf8(const std::string& conStr,std::string& utf8str) { #if PLATFORM == PLATFORM_WINDOWS std::wstring wstr; wstr.resize(conStr.size()); OemToCharBuffW(&conStr[0],&wstr[0],conStr.size()); - return WStrToUtf8(wstr,utf8str); #else // not implemented yet @@ -460,23 +397,17 @@ bool consoleToUtf8(const std::string& conStr,std::string& utf8str) return true; #endif } - bool Utf8FitTo(const std::string& str, std::wstring search) { std::wstring temp; - if(!Utf8toWStr(str,temp)) return false; - // converting to lower case wstrToLower( temp ); - if(temp.find(search) == std::wstring::npos) return false; - return true; } - void utf8printf(FILE *out, const char *str, ...) { va_list ap; @@ -484,25 +415,20 @@ void utf8printf(FILE *out, const char *str, ...) vutf8printf(stdout, str, &ap); va_end(ap); } - void vutf8printf(FILE *out, const char *str, va_list* ap) { #if PLATFORM == PLATFORM_WINDOWS char temp_buf[32*1024]; wchar_t wtemp_buf[32*1024]; - size_t temp_len = vsnprintf(temp_buf, 32*1024, str, *ap); - size_t wtemp_len = 32*1024-1; Utf8toWStr(temp_buf, temp_len, wtemp_buf, wtemp_len); - CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len+1); fprintf(out, temp_buf); #else vfprintf(out, str, *ap); #endif } - void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result) { std::ostringstream ss; |