2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  27  $Date: 2010/11/08 10:11:19 $
 
  31 // For old and dub systems
 
  33 #include <sys/types.h>
 
  37 #include <sys/select.h>
 
  42 #include <sys/socket.h>
 
  43 #include <netinet/in.h>
 
  44 #include <arpa/inet.h>
 
  45 #include <sys/select.h>
 
  57 #include "stg/common.h"
 
  59 #ifndef INET_ADDRSTRLEN
 
  60 #   define INET_ADDRSTRLEN 16
 
  65 //---------------------------------------------------------------------------
 
  66 unsigned char koi2win[] = {
 
  67         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 
  68         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
 
  69         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 
  70         0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
 
  71         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
 
  72         0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
 
  73         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
 
  74         0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
 
  75         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
 
  76         0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
 
  77         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
 
  78         0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
 
  79         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
 
  80         0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
 
  81         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
 
  82         0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
 
  83         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
 
  84         0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
 
  85         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
 
  86         0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
 
  87         0xA0, 0xA1, 0xA2, 0xB8, 0xBA, 0xA5, 0xB3, 0xBF,
 
  88         0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xB4, 0xAE, 0xAF,
 
  89         0xB0, 0xB1, 0xB2, 0xA8, 0xAA, 0xB5, 0xB2, 0xAF,
 
  90         0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xA5, 0xBE, 0xBF,
 
  91         0xFE, 0xE0, 0xE1, 0xF6, 0xE4, 0xE5, 0xF4, 0xE3,
 
  92         0xF5, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE,
 
  93         0xEF, 0xFF, 0xF0, 0xF1, 0xF2, 0xF3, 0xE6, 0xE2,
 
  94         0xFC, 0xFB, 0xE7, 0xF8, 0xFD, 0xF9, 0xF7, 0xFA,
 
  95         0xDE, 0xC0, 0xC1, 0xD6, 0xC4, 0xC5, 0xD4, 0xC3,
 
  96         0xD5, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE,
 
  97         0xCF, 0xDF, 0xD0, 0xD1, 0xD2, 0xD3, 0xC6, 0xC2,
 
  98         0xDC, 0xDB, 0xC7, 0xD8, 0xDD, 0xD9, 0xD7, 0xDA};
 
 101 unsigned char win2koi[] = {
 
 102         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 
 103         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
 
 104         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 
 105         0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
 
 106         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
 
 107         0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
 
 108         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
 
 109         0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
 
 110         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
 
 111         0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
 
 112         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
 
 113         0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
 
 114         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
 
 115         0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
 
 116         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
 
 117         0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
 
 118         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
 
 119         0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
 
 120         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
 
 121         0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
 
 122         0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xBD, 0xA6, 0xA7,
 
 123         0xB3, 0xA9, 0xB4, 0xAB, 0xAC, 0xAD, 0xAE, 0xB7,
 
 124         0xB0, 0xB1, 0xB6, 0xA6, 0xAD, 0xB5, 0xB6, 0xB7,
 
 125         0xA3, 0xB9, 0xA4, 0xBB, 0xBC, 0xBD, 0xBE, 0xA7,
 
 126         0xE1, 0xE2, 0xF7, 0xE7, 0xE4, 0xE5, 0xF6, 0xFA,
 
 127         0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0,
 
 128         0xF2, 0xF3, 0xF4, 0xF5, 0xE6, 0xE8, 0xE3, 0xFE,
 
 129         0xFB, 0xFD, 0xFF, 0xF9, 0xF8, 0xFC, 0xE0, 0xF1,
 
 130         0xC1, 0xC2, 0xD7, 0xC7, 0xC4, 0xC5, 0xD6, 0xDA,
 
 131         0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0,
 
 132         0xD2, 0xD3, 0xD4, 0xD5, 0xC6, 0xC8, 0xC3, 0xDE,
 
 133         0xDB, 0xDD, 0xDF, 0xD9, 0xD8, 0xDC, 0xC0, 0xD1};
 
 137 //-----------------------------------------------------------------------------
 
 138 const char * inet_ntop(int af, const void * src, char * dst, unsigned long length)
 
 140 struct sockaddr_in addr;
 
 141 addr.sin_family = af;
 
 143 memcpy(&addr.sin_addr.s_addr, src, sizeof(addr.sin_addr.s_addr));
 
 144 if (WSAAddressToStringA(reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr), 0, dst, &length))
 
 150 //-----------------------------------------------------------------------------
 
 151 int inet_pton(int af, const char * src, void * dst)
 
 153 // Fuck you Microsoft!
 
 154 // Why the hell not to use const char *?
 
 155 size_t slen = strlen(src);
 
 156 char * buf = new char[slen + 1];
 
 157 strncpy(buf, src, slen + 1);
 
 159 struct sockaddr_in addr;
 
 160 addr.sin_family = af;
 
 162 addr.sin_addr.s_addr = 0;
 
 163 int length = sizeof(addr);
 
 164 if (WSAStringToAddressA(buf, af, 0, reinterpret_cast<struct sockaddr *>(&addr), &length))
 
 169 memcpy(dst, &addr, sizeof(addr));
 
 174 //-----------------------------------------------------------------------------
 
 175 int strtodouble2(const char * s, double &a)
 
 186 //-----------------------------------------------------------------------------
 
 188 int printfd(const char * __file__, const char * fmt, ...)
 
 190 int printfd(const char *, const char *, ...)
 
 196 time_t t = time(NULL);
 
 200 vsnprintf(buff, sizeof(buff), fmt, vl);
 
 203 printf("%18s > %s > ", __file__, LogDate(t)+11);
 
 209 //-----------------------------------------------------------------------------
 
 210 int strprintf(std::string * str, const char * fmt, ...)
 
 216 int n = vsnprintf(buff, sizeof(buff), fmt, vl);
 
 223 //-----------------------------------------------------------------------------
 
 224 const char *IntToKMG(int64_t a, int stat)
 
 226 static const double K = 1024;
 
 227 static const double M = 1024 * 1024;
 
 228 static const double G = 1024 * 1024 * 1024;
 
 236         sprintf(str, "%Ld", a);
 
 238         sprintf(str, "%lld", a);
 
 242         sprintf(str, "%.2f kb", value / K);
 
 245         sprintf(str, "%.2f Mb", value / M);
 
 250             sprintf(str, "%.2f Gb", value / G);
 
 255             sprintf(str, "%.2f Gb", value / G);
 
 260             sprintf(str, "%.2f Mb", value / M);
 
 265             sprintf(str, "%.2f Mb", value / M);
 
 269         sprintf(str, "%.2f kb", value / K);
 
 274 //---------------------------------------------------------------------------
 
 275 void KOIToWin(const char * s1, char * s2, int l)
 
 278 for (int j = 0; j < l; j++)
 
 287 //---------------------------------------------------------------------------
 
 288 void WinToKOI(const char * s1, char * s2, int l)
 
 291 for (int j = 0; j < l; j++)
 
 300 //---------------------------------------------------------------------------
 
 301 void KOIToWin(const std::string & s1, std::string * s2)
 
 303 s2->erase(s2->begin(), s2->end());
 
 305 s2->reserve(s1.length());
 
 306 for (int j = 0; j < (int)s1.length(); j++)
 
 309     s2->push_back(koi2win[t]);
 
 312 //---------------------------------------------------------------------------
 
 313 void WinToKOI(const std::string & s1, std::string * s2)
 
 315 s2->erase(s2->begin(), s2->end());
 
 317 s2->reserve(s1.length());
 
 318 for (int j = 0; j < (int)s1.length(); j++)
 
 321     s2->push_back(win2koi[t]);
 
 324 //---------------------------------------------------------------------------
 
 325 void Encode12str(std::string & dst, const std::string & src)
 
 327 dst.erase(dst.begin(), dst.end());
 
 328 for (size_t i = 0; i < src.length(); i++)
 
 330     dst.push_back('a' + (src[i] & 0x0f));
 
 331     dst.push_back('a' + ((src[i] & 0xf0) >> 4));
 
 334 //---------------------------------------------------------------------------
 
 335 void Decode21str(std::string & dst, const std::string & src)
 
 337 dst.erase(dst.begin(), dst.end());
 
 338 for (size_t i = 0; i < src.length() / 2; i++)
 
 340     char c1 = src[i * 2];
 
 341     char c2 = src[i * 2 + 1];
 
 346     dst.push_back(static_cast<char>(c1 + (c2 << 4)));
 
 349 //---------------------------------------------------------------------------
 
 350 std::string Encode12str(const std::string & src)
 
 353 Encode12str(res, src);
 
 356 //---------------------------------------------------------------------------
 
 357 std::string Decode21str(const std::string & src)
 
 360 Decode21str(res, src);
 
 363 //---------------------------------------------------------------------------
 
 364 void Encode12(char * dst, const char * src, size_t srcLen)
 
 366 for (size_t i = 0; i <= srcLen; i++)
 
 368     char c1 = src[i] & 0x0f;
 
 369     char c2 = (src[i] & 0xf0) >> 4;
 
 379 //---------------------------------------------------------------------------
 
 380 void Decode21(char * dst, const char * src)
 
 382 for (size_t i = 0; ; i++)
 
 387     char c1 = src[i * 2];
 
 388     char c2 = src[i * 2 + 1];
 
 393     dst[i] = static_cast<char>(c1 + (c2 << 4));
 
 395 dst[strlen(src) / 2] = 0;
 
 397 //---------------------------------------------------------------------------
 
 398 int ParseIPString(const char * str, uint32_t * ips, int maxIP)
 
 401  *Function Name:ParseIPString
 
 404  ÓÔÒÏËÁ ÄÌÑ ÒÁÚÂÏÒÁ É ÍÁÓÓÉ× ËÕÄÁ ÚÁÎÏÓÉÔØ ÐÏÌÕÞÅÎÎÙÅ ÁÄÒÅÓÁ
 
 407  îÁ ×ÈÏÄÅ ÄÏÌÖÎÁ ÂÙÔØ ÓÔÒÏËÁ ×ÉÄÁ "ip1,ip2,ip3" ÉÌÉ "*"
 
 408  ÷ ÐÅÒ×ÏÍ ÓÌÕÞÁÅ × ÍÁÓÓÉ× ÚÁÎÏÓÑÔÓÑ ÒÁÚÏÂÒÁÎÎÙÅ ÁÄÒÅÓÁ.
 
 409  åÓÌÉ ÉÈ ÍÅÎØÛÅ MAX_IP?, ÔÏ ÐÏÓÌÅÄÎÉÊ ÁÄÒÅÓ ÂÕÄÅÔ 255.255.255.255
 
 410  åÓÌÉ ÓÔÒÏËÁ * , ÔÏ ÐÅÒ×ÁÙÊ ÁÄÒÅÓ ÂÕÄÅÔ 0.0.0.0, Ô.Å. ÌÀÂÏÊ
 
 412  *Returns: 0 ÅÓÌÉ ×ÓÅ ïë
 
 420 strncpy(p, str, 254);
 
 423 memset(ips, 0xFF, sizeof(unsigned long) * maxIP);
 
 425 if (str[0] == '*' && strlen(str) == 1)
 
 431 for (int i = 0; i < maxIP; i++)
 
 433     p1 = strtok(pp, ",\n ");
 
 436     if (p1 == NULL && n == 0)// ÕËÁÚÁÔÅÌØ ÎÕÌØ É ÐÒÏÞÉÔÁÎÏ ÁÄÒÅÓÏ× ÔÏÖÅ ÎÏÌØ
 
 447     if (inet_pton(AF_INET, p1, &in) != 1)
 
 449         //printf("INADDR_NONE\n");
 
 455     /*if (ips[n] == INADDR_NONE)
 
 467 //-----------------------------------------------------------------------------
 
 468 int DaysInCurrentMonth()
 
 470 time_t t = time(NULL);
 
 472 struct tm * lt = localtime(&t);
 
 474 return DaysInMonth(lt->tm_year, lt->tm_mon);
 
 476 //-----------------------------------------------------------------------------
 
 477 int DaysInMonth(unsigned year, unsigned mon)
 
 479 assert(mon < 12 && "Month number should be 0 - 11");
 
 482     case 0: return 31;  //jan
 
 484         if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
 
 487     case 2: return 31;  //mar
 
 488     case 3: return 30;  //apr
 
 489     case 4: return 31;  //may
 
 490     case 5: return 30;  //june
 
 491     case 6: return 31;  //jule
 
 492     case 7: return 31;  //aug
 
 493     case 8: return 30;  //sep
 
 494     case 9: return 31;  //oct
 
 495     case 10: return 30; //nov
 
 496     case 11: return 31; //dec
 
 498 return -1; // We will never reach here
 
 500 //-----------------------------------------------------------------------------
 
 504 æÕÎËÃÉÑ ×ÏÚ×ÒÁÝÁÅÔ ÎÁÉÍÅÎØÛÅÅ ÞÉÓÌÏ ËÒÁÔÎÏÅ 8-ÍÉ ÂÏÌØÛÅÅ ÉÌÉ ÒÁ×ÎÏÅ ÚÁÄÁÎÎÏÍÕ
 
 509 return a + (8 - a % 8);
 
 511 //-----------------------------------------------------------------------------
 
 512 /*char * inet_ntostr(unsigned long ip)
 
 514 struct in_addr addr = {ip};
 
 515 return inet_ntoa(addr);
 
 517 //-----------------------------------------------------------------------------
 
 518 std::string inet_ntostring(uint32_t ip)
 
 520     char buf[INET_ADDRSTRLEN + 1];
 
 521     return inet_ntop(AF_INET, &ip, buf, INET_ADDRSTRLEN);
 
 523 //-----------------------------------------------------------------------------
 
 524 uint32_t inet_strington(const std::string & value)
 
 528     if (inet_pton(AF_INET, value.c_str(), &result) <= 0)
 
 533 //-----------------------------------------------------------------------------
 
 534 std::string TimeToString(time_t time)
 
 536 struct tm brokenTime;
 
 538 brokenTime.tm_wday = 0;
 
 539 brokenTime.tm_yday = 0;
 
 540 brokenTime.tm_isdst = 0;
 
 542 gmtime_r(&time, &brokenTime);
 
 545 strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
 
 549 //-----------------------------------------------------------------------------
 
 550 int ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2)
 
 552 char hs1[10], ms1[10], hs2[10], ms2[10];
 
 557 strncpy(ss, str, 48);
 
 559 p1 = strtok(ss, "-");
 
 565 p2 = strtok(NULL, "-");
 
 571 p1 = strtok(s1, ":");
 
 577 p2 = strtok(NULL, ":");
 
 583 p1 = strtok(s2, ":");
 
 589 p2 = strtok(NULL, ":");
 
 595 if (str2x(hs1, h1) != 0)
 
 598 if (str2x(ms1, m1) != 0)
 
 601 if (str2x(hs2, h2) != 0)
 
 604 if (str2x(ms2, m2) != 0)
 
 609 /*//---------------------------------------------------------------------------
 
 612 if (c >= '0' && c <= '9')
 
 616 //-----------------------------------------------------------------------------
 
 619 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
 
 623 //-----------------------------------------------------------------------------
 
 624 const char * LogDate(time_t t)
 
 627 struct tm * tt = localtime(&t);
 
 629 snprintf(s, 20, "%d-%s%d-%s%d %s%d:%s%d:%s%d",
 
 631          tt->tm_mon + 1 < 10 ? "0" : "", tt->tm_mon + 1,
 
 632          tt->tm_mday    < 10 ? "0" : "", tt->tm_mday,
 
 633          tt->tm_hour    < 10 ? "0" : "", tt->tm_hour,
 
 634          tt->tm_min     < 10 ? "0" : "", tt->tm_min,
 
 635          tt->tm_sec     < 10 ? "0" : "", tt->tm_sec);
 
 639 //-----------------------------------------------------------------------------
 
 640 uint32_t CalcMask(uint32_t msk)
 
 642 if (msk >= 32) return 0xFFffFFff;
 
 643 if (msk == 0) return 0;
 
 644 return htonl(0xFFffFFff << (32 - msk));
 
 646 //---------------------------------------------------------------------------
 
 647 void TouchFile(const std::string & fileName)
 
 649 FILE * f = fopen(fileName.c_str(), "w");
 
 653 //---------------------------------------------------------------------------
 
 655 void EncodeStr(char * str, unsigned long serial, int useHDD)
 
 657 int len = strlen(str);
 
 661 char serial_c[sizeof(serial)];
 
 662 memcpy(serial_c, &serial, sizeof(serial));
 
 664 for (i = 0; i < len; i++)
 
 670         str[i] = str[i]^serial_c[j%sizeof(serial)];
 
 675 for (i = 0; i < 2*len; i++)
 
 679         c1 = (str[i/2] >> 4);
 
 685         c2 = (str[i/2] & 0x0f);
 
 693 //---------------------------------------------------------------------------
 
 694 void DecodeStr(char * str, unsigned long serial, int useHDD)
 
 696 int len = strlen(str);
 
 700 char serial_c[sizeof(serial)];
 
 701 memcpy(serial_c, &serial, sizeof(serial));
 
 703 for (i = 0; i < len; i += 2)
 
 706     c2 = (str[i+1] - 50)<<4;
 
 709 for (i = 0; i < len/2; i++)
 
 712         strdc[i] = strdc[i]^49;
 
 715         strdc[i] = strdc[i]^serial_c[j%sizeof(serial)];
 
 722 //---------------------------------------------------------------------------
 
 724 void SwapBytes(uint16_t & value)
 
 726     value = static_cast<uint16_t>((value >> 8) |
 
 729 //---------------------------------------------------------------------------
 
 730 void SwapBytes(uint32_t & value)
 
 732     value = static_cast<uint32_t>((value >> 24) |
 
 733                                   ((value << 8) &  0x00FF0000L) |
 
 734                                   ((value >> 8) &  0x0000FF00L) |
 
 737 //---------------------------------------------------------------------------
 
 738 void SwapBytes(uint64_t & value)
 
 740     value = static_cast<uint64_t>((value >> 56) |
 
 741                                   ((value << 40) & 0x00FF000000000000LL) |
 
 742                                   ((value << 24) & 0x0000FF0000000000LL) |
 
 743                                   ((value << 8)  & 0x000000FF00000000LL) |
 
 744                                   ((value >> 8)  & 0x00000000FF000000LL) |
 
 745                                   ((value >> 24) & 0x0000000000FF0000LL) |
 
 746                                   ((value >> 40) & 0x000000000000FF00LL) |
 
 749 //---------------------------------------------------------------------------
 
 750 void SwapBytes(int16_t & value)
 
 752     uint16_t temp = value;
 
 756 //---------------------------------------------------------------------------
 
 757 void SwapBytes(int32_t & value)
 
 759     uint32_t temp = value;
 
 763 //---------------------------------------------------------------------------
 
 764 void SwapBytes(int64_t & value)
 
 766     uint64_t temp = value;
 
 770 //---------------------------------------------------------------------------
 
 771 int str2x(const std::string & str, int32_t & x)
 
 773 x = static_cast<int32_t>(strtol(str.c_str(), NULL, 10));
 
 780 //---------------------------------------------------------------------------
 
 781 int str2x(const std::string & str, uint32_t & x)
 
 783 x = static_cast<uint32_t>(strtoul(str.c_str(), NULL, 10));
 
 791 //---------------------------------------------------------------------------
 
 792 int str2x(const std::string & str, int64_t & x)
 
 794 x = strtoll(str.c_str(), NULL, 10);
 
 801 //---------------------------------------------------------------------------
 
 802 int str2x(const std::string & str, uint64_t & x)
 
 804 x = strtoull(str.c_str(), NULL, 10);
 
 812 //---------------------------------------------------------------------------
 
 813 const std::string & x2str(uint32_t x, std::string & s)
 
 815 return unsigned2str(x, s);
 
 817 //---------------------------------------------------------------------------
 
 818 const std::string & x2str(uint64_t x, std::string & s)
 
 820 return unsigned2str(x, s);
 
 822 //---------------------------------------------------------------------------
 
 823 const std::string & x2str(double x, std::string & s)
 
 826 s = snprintf(buf, sizeof(buf), "%f", x);
 
 829 //---------------------------------------------------------------------------
 
 830 std::string & TrimL(std::string & val)
 
 832 size_t pos = val.find_first_not_of(" \t");
 
 833 if (pos == std::string::npos)
 
 835     val.erase(val.begin(), val.end());
 
 843 //---------------------------------------------------------------------------
 
 844 std::string & TrimR(std::string & val)
 
 846 size_t pos = val.find_last_not_of(" \t");
 
 847 if (pos != std::string::npos)
 
 853 //---------------------------------------------------------------------------
 
 854 std::string & Trim(std::string & val)
 
 856 return TrimR(TrimL(val));
 
 858 //---------------------------------------------------------------------------
 
 859 std::string Trim(const std::string & val)
 
 861 std::string res(val);
 
 862 return TrimR(TrimL(res));
 
 864 //---------------------------------------------------------------------------
 
 865 std::string ToLower(const std::string & value)
 
 868     for (std::string::size_type pos = 0; pos < value.length(); ++pos)
 
 869         res += tolower(value[pos]);
 
 872 //---------------------------------------------------------------------------
 
 873 std::string ToUpper(const std::string & value)
 
 876     for (std::string::size_type pos = 0; pos < value.length(); ++pos)
 
 877         res += toupper(value[pos]);
 
 880 //---------------------------------------------------------------------------
 
 882 static int is_leap(unsigned y)
 
 885     return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
 
 888 //---------------------------------------------------------------------------
 
 890 time_t stg_timegm(struct tm * brokenTime)
 
 893 static const unsigned ndays[2][12] ={
 
 894     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
 
 895     {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
 
 897 for (int i = 70; i < brokenTime->tm_year; ++i)
 
 898     res += is_leap(i) ? 366 : 365;
 
 899 for (int i = 0; i < brokenTime->tm_mon; ++i)
 
 900     res += ndays[is_leap(brokenTime->tm_year)][i];
 
 901 res += brokenTime->tm_mday - 1;
 
 903 res += brokenTime->tm_hour;
 
 905 res += brokenTime->tm_min;
 
 907 res += brokenTime->tm_sec;
 
 911 return timegm(brokenTime);
 
 918 ret = mktime(brokenTime);
 
 925 #endif // HAVE_TIMEGM
 
 928 //---------------------------------------------------------------------------
 
 929 std::string IconvString(const std::string & source,
 
 930                         const std::string & from,
 
 931                         const std::string & to)
 
 934     return std::string();
 
 936 size_t inBytesLeft = source.length() + 1;
 
 937 size_t outBytesLeft = source.length() * 2 + 1;
 
 939 char * inBuf = new char[inBytesLeft];
 
 940 char * outBuf = new char[outBytesLeft];
 
 942 strncpy(inBuf, source.c_str(), source.length());
 
 944 inBuf[source.length()] = 0;
 
 946 #if defined(FREE_BSD) || defined(FREE_BSD5) || defined(WIN32)
 
 947 const char * srcPos = inBuf;
 
 949 char * srcPos = inBuf;
 
 951 char * dstPos = outBuf;
 
 953 iconv_t handle = iconv_open(to.c_str(),
 
 956 if (handle == iconv_t(-1))
 
 960         printfd(__FILE__, "IconvString(): iconv from %s to %s failed\n", from.c_str(), to.c_str());
 
 966         printfd(__FILE__, "IconvString(): iconv_open error\n");
 
 973 size_t res = iconv(handle,
 
 974                    &srcPos, &inBytesLeft,
 
 975                    &dstPos, &outBytesLeft);
 
 977 if (res == size_t(-1))
 
 979     printfd(__FILE__, "IconvString(): '%s'\n", strerror(errno));
 
 989 std::string dst(outBuf);
 
 999 int ParseYesNo(const std::string & str, bool * val)
 
1001 if (0 == strncasecmp(str.c_str(), "yes", 3))
 
1007 if (0 == strncasecmp(str.c_str(), "no", 2))
 
1016 int ParseInt(const std::string & str, int * val)
 
1018 if (str2x<int>(str, *val))
 
1023 int ParseUnsigned(const std::string & str, unsigned * val)
 
1025 if (str2x<unsigned>(str, *val))
 
1030 int ParseIntInRange(const std::string & str, int min, int max, int * val)
 
1032 if (ParseInt(str, val) != 0)
 
1035 if (*val < min || *val > max)
 
1041 int ParseUnsignedInRange(const std::string & str, unsigned min,
 
1042                          unsigned max, unsigned * val)
 
1044 if (ParseUnsigned(str, val) != 0)
 
1047 if (*val < min || *val > max)
 
1053 bool WaitPackets(int sd)
 
1061 tv.tv_usec = 500000;
 
1063 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
 
1064 if (res == -1) // Error
 
1067         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
 
1071 if (res == 0) // Timeout