26 float int_part = floorf(v);
27 float float_part = v - int_part;
28 if( float_part > 0.5f )
36 double int_part = floor(v);
37 double float_part = v - int_part;
38 if( float_part > 0.5f )
39 return int32(int_part + 1.0f);
41 return int32(int_part);
44 vector<string>
StrSplit(
const string &src,
const string &sep)
48 for (string::const_iterator i = src.begin(); i != src.end(); i++) {
49 if (sep.find(*i) != string::npos) {
50 if (s.length()) r.push_back(s);
56 if (s.length()) r.push_back(s);
69 char thread_name[200];
74 info.dwThreadID = GetCurrentThreadId();
76 info.szName = thread_name;
81 RaiseException(0x406D1388, 0,
sizeof(info)/
sizeof(DWORD), (ULONG_PTR*)&info);
83 RaiseException(0x406D1388, 0,
sizeof(info)/
sizeof(DWORD), (DWORD*)&info);
86 __except(EXCEPTION_CONTINUE_EXECUTION)
101 struct tm * ti = localtime( &rawtime );
105 ti->tm_hour += dLength;
108 ti->tm_mday += dLength;
111 ti->tm_mday += 7 * dLength;
114 ti->tm_mon += dLength;
118 ti->tm_year += dLength;
121 ti->tm_min += dLength;
130 char * p = (
char*)str;
134 number_temp.reserve(10);
143 while(isdigit(*p) && *p != 0)
157 multiplier = TIME_YEAR;
161 multiplier = TIME_MONTH;
165 multiplier = TIME_DAY;
169 multiplier = TIME_HOUR;
178 multipliee = atoi(number_temp.c_str());
179 time_to_ban += (multiplier * multipliee);
187 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
191 "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
211 int seconds = (int)timestamp;
263 szResult += szTempBuf;
264 szResult +=
" years, ";
269 szResult += szTempBuf;
270 szResult +=
" months, ";
275 szResult += szTempBuf;
276 szResult +=
" days, ";
281 szResult += szTempBuf;
282 szResult +=
" hours, ";
287 szResult += szTempBuf;
288 szResult +=
" minutes, ";
293 szResult += szTempBuf;
294 szResult +=
" seconds";
302 int seconds = (int)timestamp;
354 szResult += szTempBuf;
360 szResult += szTempBuf;
361 szResult +=
" months ";
366 szResult += szTempBuf;
367 szResult +=
" days ";
372 szResult += szTempBuf;
378 szResult += szTempBuf;
379 szResult +=
" mins ";
384 szResult += szTempBuf;
394 time_t t = (time_t)timestamp;
395 tm * pTM = localtime(&t);
398 szResult += szDayNames[pTM->tm_wday];
402 szResult += szTempBuf;
405 szResult += szMonthNames[pTM->tm_mon];
409 szResult += szTempBuf;
412 szResult += szTempBuf;
415 szResult += szTempBuf;
418 szResult += szTempBuf;
424 bool ParseCIDRBan(
unsigned int IP,
unsigned int Mask,
unsigned int MaskBits)
429 unsigned char * source_ip = (
unsigned char*)&IP;
430 unsigned char * mask = (
unsigned char*)&Mask;
431 int full_bytes = MaskBits / 8;
432 int leftover_bits = MaskBits % 8;
442 static const unsigned char leftover_bits_compare[9] = {
457 if( memcmp( source_ip, mask, full_bytes ) != 0 )
464 if( leftover_bits > 0 )
466 if( ( source_ip[full_bytes] & leftover_bits_compare[leftover_bits] ) !=
467 ( mask[full_bytes] & leftover_bits_compare[leftover_bits] ) )
480 unsigned int bytes[4];
482 if( sscanf(str,
"%u.%u.%u.%u", &bytes[0], &bytes[1], &bytes[2], &bytes[3]) != 4 )
487 res = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
491 int get_tokens(
const char* szInput,
char** pOutput,
int iMaxCount,
char cSeperator)
503 while(*p !=
'\0' && *p != cSeperator)
514 pOutput[count++] = q;
524 pOutput[count++] = q;
527 while(*p == cSeperator)
538 if( count == iMaxCount )
547 #ifdef HAVE_WINDOWS_H
548 char* ip_string =
strdup(szIPList);
551 int count =
get_tokens(ip_string, (
char**)tokens, 30,
':');
553 unsigned int ips[30];
555 for(
int i = 0; i < count; ++i)
557 char* t = strchr(tokens[i],
'/');
561 masks[i] = atoi(t+1);
566 ips[i] = inet_addr(tokens[i]);
569 char computer_name[100] = {0};
570 DWORD computer_name_len = 100;
571 GetComputerNameA(computer_name, &computer_name_len);
573 hostent* ent = gethostbyname(computer_name);
580 for(
int i = 0; ent->h_addr_list[i] != NULL; ++i )
584 for(
int j = 0; j < count; ++j )
586 if(
ParseCIDRBan(*(
unsigned int*)ent->h_addr_list[i], ips[j], masks[j]) )
611 std::string Strings::trim(
const std::string& str,
bool left,
bool right)
613 size_t lspaces, rspaces, len = str.length(), i;
615 lspaces = rspaces = 0;
620 i < len && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
624 if(right ==
true && lspaces < len)
627 i >= 0 && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
631 return str.substr(lspaces, len-lspaces-rspaces);
634 void Strings::trim(std::string& str,
bool left,
bool right)
636 size_t lspaces, rspaces, len = str.length(), i;
638 lspaces = rspaces = 0;
643 i < len && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
647 if(right ==
true && lspaces < len)
650 i >= 0 && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
655 str = str.substr(lspaces, len-lspaces-rspaces);
658 std::wstring Strings::trim(
const std::wstring& str,
bool left,
bool right)
660 size_t lspaces, rspaces, len = str.length(), i;
662 lspaces = rspaces = 0;
667 i < len && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
671 if(right ==
true && lspaces < len)
674 i >= 0 && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
678 return str.substr(lspaces, len-lspaces-rspaces);
681 void Strings::trim(std::wstring& str,
bool left,
bool right)
683 size_t lspaces, rspaces, len = str.length(), i;
685 lspaces = rspaces = 0;
690 i < len && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
694 if(right ==
true && lspaces < len)
697 i >= 0 && (str[i] ==
' ' || str[i] ==
'\t' || str[i] ==
'\r');
702 str = str.substr(lspaces, len-lspaces-rspaces);
705 std::string Strings::toUpperCase(
const std::string& TString)
707 std::string retval(TString);
708 std::transform(retval.begin(), retval.end(), retval.begin(),
static_cast<int(*)(
int)
>(::toupper));
712 void Strings::toUpperCase(std::string& TString)
714 std::transform(TString.begin(), TString.end(), TString.begin(),
static_cast<int(*)(
int)
>(::toupper));
717 std::wstring Strings::toUpperCase(
const std::wstring& TString)
719 std::wstring retval(TString);
720 std::transform(retval.begin(), retval.end(), retval.begin(),
static_cast<int(*)(
int)
>(::toupper));
724 void Strings::toUpperCase(std::wstring& TString)
726 std::transform(TString.begin(), TString.end(), TString.begin(),
static_cast<int(*)(
int)
>(::toupper));
729 std::string Strings::toLowerCase(
const std::string& TString)
731 std::string retval(TString);
732 std::transform(retval.begin(), retval.end(), retval.begin(),
static_cast<int(*)(
int)
>(::tolower));
736 void Strings::toLowerCase(std::string& TString)
738 std::transform(TString.begin(), TString.end(), TString.begin(),
static_cast<int(*)(
int)
>(::tolower));
741 std::wstring Strings::toLowerCase(
const std::wstring& TString)
743 std::wstring retval(TString);
744 std::transform(retval.begin(), retval.end(), retval.begin(),
static_cast<int(*)(
int)
>(::tolower));
748 void Strings::toLowerCase(std::wstring& TString)
750 std::transform(TString.begin(), TString.end(), TString.begin(),
static_cast<int(*)(
int)
>(::tolower));
757 std::string Strings::CaseFold( std::string & str )
759 std::string s2 = str;
767 std::wstring Strings::CaseFold( std::wstring & str )
769 std::wstring s2 = str;
777 std::wstring Strings::StringToWString( std::string & str )
781 wstr.resize( str.size() );
782 size_t ret_len = mbstowcs( &wstr[0], str.c_str(), str.size() );
784 if (ret_len != str.size()) {
786 sLog.Error(
"Utils::Strings::StringToWString",
"unable to convert std::string to std::wstring, wide character string");
int get_tokens(const char *szInput, char **pOutput, int iMaxCount, char cSeperator)
int32 ascent_roundf(float v)
bool CheckIPs(const char *szIPList)
string ConvertTimeStampToString(uint32 timestamp)
const char * szMonthNames[12]
void MakeIntString(char *buf, int num)
static std::string toUpperCase(const std::string &TString)
int32 GetTimePeriodFromString(const char *str)
void MakeIntStringNoZero(char *buf, int num)
std::string sprintf(const char *fmt,...)
sprintf for std::string.
int32 ascent_roundd(double v)
#define sLog
Evaluates to a NewLog instance.
string ConvertTimeStampToStringNC(uint32 timestamp)
static std::string toLowerCase(const std::string &TString)
unsigned int MakeIP(const char *str)
void SetThreadName(const char *format,...)
int32 float2int32(const float value)
Fastest Method of float2int32.
time_t convTimePeriod(uint32 dLength, char dType)
string ConvertTimeStampToDataTime(uint32 timestamp)
bool ParseCIDRBan(unsigned int IP, unsigned int Mask, unsigned int MaskBits)
vector< string > StrSplit(const string &src, const string &sep)
const char * szDayNames[7]