]> git.stg.codes - stg.git/blob - stglibs/common.lib/common.cpp
Виправлено помилки компіляції на FreeBSD4
[stg.git] / stglibs / common.lib / common.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25  /*
26  $Revision: 1.42 $
27  $Date: 2010/11/08 10:11:19 $
28  $Author: faust $
29  */
30
31
32 /*#include <sys/stat.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <iconv.h>
39
40
41 #include <stdarg.h>
42
43
44 #include <sys/types.h>
45 #include <math.h>
46
47 #ifdef WIN32
48 #include <sysutils.hpp>
49 #else
50 #include <unistd.h>
51 #include <sys/wait.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <sys/socket.h>
55 #endif*/
56
57 #ifdef FREE_BSD
58 #include <sys/types.h>
59 #endif
60
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <iconv.h>
65
66 #include <cstdlib>
67 #include <cstdarg>
68 #include <cstdio>
69 #include <cstring>
70 #include <cerrno>
71 #include <cassert>
72
73 #include "common.h"
74
75 #ifndef INET_ADDRSTRLEN
76 #   define INET_ADDRSTRLEN 16
77 #endif
78
79 using namespace std;
80
81 //-----------------------------------------------------------------------------
82 int strtodouble2(const char * s, double &a)
83 {
84 char *res;
85
86 a = strtod(s, &res);
87
88 if (*res != 0)
89     return EINVAL;
90
91 return 0;
92 }
93 //-----------------------------------------------------------------------------
94 #ifdef DEBUG
95 int printfd(const char * __file__, const char * fmt, ...)
96 #else
97 int printfd(const char *, const char *, ...)
98 #endif
99 {
100 #ifdef DEBUG
101 char buff[1024];
102
103 time_t t = time(NULL);
104
105 va_list vl;
106 va_start(vl, fmt);
107 vsnprintf(buff, sizeof(buff), fmt, vl);
108 va_end(vl);
109
110 printf("%18s > %s > ", __file__, LogDate(t)+11);
111 printf("%s", buff);
112
113 #endif
114 return 0;
115 }
116 //-----------------------------------------------------------------------------
117 int strprintf(string * str, const char * fmt, ...)
118 {
119 char buff[1024];
120
121 va_list vl;
122 va_start(vl, fmt);
123 int n = vsnprintf(buff, sizeof(buff), fmt, vl);
124 va_end(vl);
125 buff[1023] = 0;
126 *str = buff;
127
128 return n;
129 }
130 //-----------------------------------------------------------------------------
131 const char *IntToKMG(long long a, int stat)
132 {
133 static int64_t M = 1024*1024;
134 static int64_t G = 1024*1024*1024;
135 static char str[30];
136
137 switch (stat)
138     {
139     case ST_B:
140         #ifdef __WIN32__
141         sprintf(str, "%Ld", a);
142         #else
143         sprintf(str, "%lld", a);
144         #endif
145         break;
146     case ST_KB:
147         sprintf(str, "%.2f kb", double(a)/1024.0);
148         break;
149     case ST_MB:
150         sprintf(str, "%.2f Mb", double(a)/(1024.0*1024.0));
151         break;
152     default:
153         if (a > G)
154             {
155             sprintf(str, "%.2f Gb", double(a)/double(G));
156             return &str[0];
157             }
158         if (a < -G)
159             {
160             sprintf(str, "%.2f Gb", double(a)/double(G));
161             return &str[0];
162             }
163         if (a > M)
164             {
165             sprintf(str, "%.2f Mb", double(a)/double(M));
166             return &str[0];
167             }
168         if (a < -M)
169             {
170             sprintf(str, "%.2f Mb", double(a)/double(M));
171             return &str[0];
172             }
173
174         sprintf(str, "%.2f kb", double(a)/1024.0);
175         break;
176     }
177 return str;
178 }
179 //---------------------------------------------------------------------------
180 unsigned char koi2win[] = {
181         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
182         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
183         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
184         0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
185         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
186         0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
187         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
188         0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
189         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
190         0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
191         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
192         0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
193         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
194         0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
195         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
196         0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
197         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
198         0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
199         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
200         0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
201         0xA0, 0xA1, 0xA2, 0xB8, 0xBA, 0xA5, 0xB3, 0xBF,
202         0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xB4, 0xAE, 0xAF,
203         0xB0, 0xB1, 0xB2, 0xA8, 0xAA, 0xB5, 0xB2, 0xAF,
204         0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xA5, 0xBE, 0xBF,
205         0xFE, 0xE0, 0xE1, 0xF6, 0xE4, 0xE5, 0xF4, 0xE3,
206         0xF5, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE,
207         0xEF, 0xFF, 0xF0, 0xF1, 0xF2, 0xF3, 0xE6, 0xE2,
208         0xFC, 0xFB, 0xE7, 0xF8, 0xFD, 0xF9, 0xF7, 0xFA,
209         0xDE, 0xC0, 0xC1, 0xD6, 0xC4, 0xC5, 0xD4, 0xC3,
210         0xD5, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE,
211         0xCF, 0xDF, 0xD0, 0xD1, 0xD2, 0xD3, 0xC6, 0xC2,
212         0xDC, 0xDB, 0xC7, 0xD8, 0xDD, 0xD9, 0xD7, 0xDA};
213
214
215 unsigned char win2koi[] = {
216         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
217         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
218         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
219         0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
220         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
221         0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
222         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
223         0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
224         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
225         0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
226         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
227         0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
228         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
229         0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
230         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
231         0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
232         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
233         0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
234         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
235         0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
236         0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xBD, 0xA6, 0xA7,
237         0xB3, 0xA9, 0xB4, 0xAB, 0xAC, 0xAD, 0xAE, 0xB7,
238         0xB0, 0xB1, 0xB6, 0xA6, 0xAD, 0xB5, 0xB6, 0xB7,
239         0xA3, 0xB9, 0xA4, 0xBB, 0xBC, 0xBD, 0xBE, 0xA7,
240         0xE1, 0xE2, 0xF7, 0xE7, 0xE4, 0xE5, 0xF6, 0xFA,
241         0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0,
242         0xF2, 0xF3, 0xF4, 0xF5, 0xE6, 0xE8, 0xE3, 0xFE,
243         0xFB, 0xFD, 0xFF, 0xF9, 0xF8, 0xFC, 0xE0, 0xF1,
244         0xC1, 0xC2, 0xD7, 0xC7, 0xC4, 0xC5, 0xD6, 0xDA,
245         0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0,
246         0xD2, 0xD3, 0xD4, 0xD5, 0xC6, 0xC8, 0xC3, 0xDE,
247         0xDB, 0xDD, 0xDF, 0xD9, 0xD8, 0xDC, 0xC0, 0xD1};
248 //---------------------------------------------------------------------------
249 void KOIToWin(const char * s1, char * s2, int l)
250 {
251 unsigned char t;
252 for (int j = 0; j < l; j++)
253     {
254     t = s1[j];
255     s2[j] = koi2win[t];
256
257     if (s1[j] == 0)
258         break;
259     }
260 }
261 //---------------------------------------------------------------------------
262 void WinToKOI(const char * s1, char * s2, int l)
263 {
264 unsigned char t;
265 for (int j = 0; j < l; j++)
266     {
267     t = s1[j];
268     s2[j] = win2koi[t];
269
270     if (s1[j] == 0)
271         break;
272     }
273 }
274 //---------------------------------------------------------------------------
275 void KOIToWin(const string & s1, string * s2)
276 {
277 s2->erase(s2->begin(), s2->end());
278 unsigned char t;
279 s2->reserve(s1.length());
280 for (int j = 0; j < (int)s1.length(); j++)
281     {
282     t = s1[j];
283     s2->push_back(koi2win[t]);
284     }
285 }
286 //---------------------------------------------------------------------------
287 void WinToKOI(const string & s1, string * s2)
288 {
289 s2->erase(s2->begin(), s2->end());
290 unsigned char t;
291 s2->reserve(s1.length());
292 for (int j = 0; j < (int)s1.length(); j++)
293     {
294     t = s1[j];
295     s2->push_back(win2koi[t]);
296     }
297 }
298 //---------------------------------------------------------------------------
299 void Encode12str(string & dst, const string & src)
300 {
301 dst.erase(dst.begin(), dst.end());
302 for (size_t i = 0; i < src.length(); i++)
303     {
304     dst.push_back('a' + (src[i] & 0x0f));
305     dst.push_back('a' + ((src[i] & 0xf0) >> 4));
306     }
307 }
308 //---------------------------------------------------------------------------
309 void Decode21str(std::string & dst, const std::string & src)
310 {
311 dst.erase(dst.begin(), dst.end());
312 for (size_t i = 0; i < src.length() / 2; i++)
313     {
314     char c1 = src[i * 2];
315     char c2 = src[i * 2 + 1];
316
317     c1 -= 'a';
318     c2 -= 'a';
319
320     dst.push_back(c1 + (c2 << 4));
321     }
322 }
323 //---------------------------------------------------------------------------
324 void Encode12(char * dst, const char * src, size_t srcLen)
325 {
326 for (size_t i = 0; i <= srcLen; i++)
327     {
328     if (src[i] == 0)
329         {
330         dst[i * 2] = 'a';
331         dst[i * 2 + 1] = 'a';
332         break;
333         }
334     char c1 = src[i] & 0x0f;
335     char c2 = (src[i] & 0xf0) >> 4;
336
337     c1 += 'a';
338     c2 += 'a';
339
340     dst[i * 2] = c1;
341     dst[i * 2 + 1] = c2;
342     }
343 dst[srcLen * 2] = 0;
344 }
345 //---------------------------------------------------------------------------
346 void Decode21(char * dst, const char * src)
347 {
348 for (size_t i = 0; ; i++)
349     {
350     if (src[i * 2] == 0)
351         break;
352
353     char c1 = src[i * 2];
354     char c2 = src[i * 2 + 1];
355
356     c1 -= 'a';
357     c2 -= 'a';
358
359     dst[i] = c1 + (c2 << 4);
360     }
361 dst[strlen(src) / 2] = 0;
362 }
363 //---------------------------------------------------------------------------
364 int ParseIPString(const char * str, uint32_t * ips, int maxIP)
365 {
366 /*
367  *Function Name:ParseIPString
368  *
369  *Parameters:
370  ÓÔÒÏËÁ ÄÌÑ ÒÁÚÂÏÒÁ É ÍÁÓÓÉ× ËÕÄÁ ÚÁÎÏÓÉÔØ ÐÏÌÕÞÅÎÎÙÅ ÁÄÒÅÓÁ
371  *
372  *Description:
373  îÁ ×ÈÏÄÅ ÄÏÌÖÎÁ ÂÙÔØ ÓÔÒÏËÁ ×ÉÄÁ "ip1,ip2,ip3" ÉÌÉ "*"
374  ÷ ÐÅÒ×ÏÍ ÓÌÕÞÁÅ × ÍÁÓÓÉ× ÚÁÎÏÓÑÔÓÑ ÒÁÚÏÂÒÁÎÎÙÅ ÁÄÒÅÓÁ.
375  åÓÌÉ ÉÈ ÍÅÎØÛÅ MAX_IP?, ÔÏ ÐÏÓÌÅÄÎÉÊ ÁÄÒÅÓ ÂÕÄÅÔ 255.255.255.255
376  åÓÌÉ ÓÔÒÏËÁ * , ÔÏ ÐÅÒ×ÁÙÊ ÁÄÒÅÓ ÂÕÄÅÔ 0.0.0.0, Ô.Å. ÌÀÂÏÊ
377  *
378  *Returns: 0 ÅÓÌÉ ×ÓÅ ïë
379  *
380  */
381
382 char p[255];
383 char * p1, *pp;
384 int n = 0;
385
386 strncpy(p, str, 254);
387 pp = p;
388
389 memset(ips, 0xFF, sizeof(unsigned long) * maxIP);
390
391 if (str[0] == '*' && strlen(str) == 1)
392     {
393     ips[0] = 0;
394     return 0;
395     }
396
397 for (int i = 0; i < maxIP; i++)
398     {
399     p1 = strtok(pp, ",\n ");
400     pp = NULL;
401
402     if (p1 == NULL && n == 0)// ÕËÁÚÁÔÅÌØ ÎÕÌØ É ÐÒÏÞÉÔÁÎÏ ÁÄÒÅÓÏ× ÔÏÖÅ ÎÏÌØ
403         {
404         return EINVAL;
405         }
406
407     if (p1 == NULL && n)
408         {
409         return 0;
410         }
411
412     struct in_addr in;
413     if (!inet_aton(p1, &in))
414         {
415         //printf("INADDR_NONE\n");
416         return EINVAL;
417         }
418
419     ips[n] = in.s_addr;
420
421     /*if (ips[n] == INADDR_NONE)
422         return EINVAL;*/
423
424     n++;
425
426     if (n >= maxIP)
427         return 0;
428
429     }
430
431 return 0;
432 }
433 //-----------------------------------------------------------------------------
434 int DaysInCurrentMonth()
435 {
436 time_t t = time(NULL);
437
438 struct tm * lt = localtime(&t);
439
440 return DaysInMonth(lt->tm_year, lt->tm_mon);
441 }
442 //-----------------------------------------------------------------------------
443 int DaysInMonth(unsigned year, unsigned mon)
444 {
445 assert(mon < 12 && "Month number should be 0 - 11");
446 switch (mon)
447     {
448     case 0: return 31;  //jan
449     case 1:
450         if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
451             return 29;
452         return 28;      //feb
453     case 2: return 31;  //mar
454     case 3: return 30;  //apr
455     case 4: return 31;  //may
456     case 5: return 30;  //june
457     case 6: return 31;  //jule
458     case 7: return 31;  //aug
459     case 8: return 30;  //sep
460     case 9: return 31;  //oct
461     case 10: return 30; //nov
462     case 11: return 31; //dec
463     }
464 return -1; // We will never reach here
465 }
466 //-----------------------------------------------------------------------------
467 int Min8(int a)
468 {
469 /*
470 æÕÎËÃÉÑ ×ÏÚ×ÒÁÝÁÅÔ ÎÁÉÍÅÎØÛÅÅ ÞÉÓÌÏ ËÒÁÔÎÏÅ 8-ÍÉ ÂÏÌØÛÅÅ ÉÌÉ ÒÁ×ÎÏÅ ÚÁÄÁÎÎÏÍÕ
471  * */
472 if (a % 8 == 0)
473     return a;
474
475 return a + (8 - a % 8);
476 }
477 //-----------------------------------------------------------------------------
478 /*char * inet_ntostr(unsigned long ip)
479 {
480 struct in_addr addr = {ip};
481 return inet_ntoa(addr);
482 }*/
483 //-----------------------------------------------------------------------------
484 std::string inet_ntostring(uint32_t ip)
485 {
486     char buf[INET_ADDRSTRLEN + 1];
487     return inet_ntop(AF_INET, &ip, buf, INET_ADDRSTRLEN);
488 }
489 //-----------------------------------------------------------------------------
490 uint32_t inet_strington(const std::string & value)
491 {
492     uint32_t result;
493
494     if (inet_pton(AF_INET, value.c_str(), &result) <= 0)
495         return 0;
496
497     return result;
498 }
499 //-----------------------------------------------------------------------------
500 int ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2)
501 {
502 char hs1[10], ms1[10], hs2[10], ms2[10];
503 char s1[25], s2[25];
504 char ss[49];
505 char *p1, *p2;
506
507 strncpy(ss, str, 48);
508
509 p1 = strtok(ss, "-");
510 if (!p1)
511     return -1;
512
513 strncpy(s1, p1, 24);
514
515 p2 = strtok(NULL, "-");
516 if (!p2)
517     return -1;
518
519 strncpy(s2, p2, 24);
520
521 p1 = strtok(s1, ":");
522 if (!p1)
523     return -1;
524
525 strncpy(hs1, p1, 9);
526
527 p2 = strtok(NULL, ":");
528 if (!p2)
529     return -1;
530
531 strncpy(ms1, p2, 9);
532
533 p1 = strtok(s2, ":");
534 if (!p1)
535     return -1;
536
537 strncpy(hs2, p1, 9);
538
539 p2 = strtok(NULL, ":");
540 if (!p2)
541     return -1;
542
543 strncpy(ms2, p2, 9);
544
545 if (str2x(hs1, h1) != 0)
546     return -1;
547
548 if (str2x(ms1, m1) != 0)
549     return -1;
550
551 if (str2x(hs2, h2) != 0)
552     return -1;
553
554 if (str2x(ms2, m2) != 0)
555     return -1;
556
557 return 0;
558 }
559 /*//---------------------------------------------------------------------------
560 bool IsDigit(char c)
561 {
562 if (c >= '0' && c <= '9')
563     return true;
564 return false;
565 }
566 //-----------------------------------------------------------------------------
567 bool IsAlpha(char c)
568 {
569 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
570     return true;
571 return false;
572 }*/
573 //-----------------------------------------------------------------------------
574 const char * LogDate(time_t t)
575 {
576 static char s[32];
577 struct tm * tt = localtime(&t);
578
579 snprintf(s, 20, "%d-%s%d-%s%d %s%d:%s%d:%s%d",
580          tt->tm_year + 1900,
581          tt->tm_mon + 1 < 10 ? "0" : "", tt->tm_mon + 1,
582          tt->tm_mday    < 10 ? "0" : "", tt->tm_mday,
583          tt->tm_hour    < 10 ? "0" : "", tt->tm_hour,
584          tt->tm_min     < 10 ? "0" : "", tt->tm_min,
585          tt->tm_sec     < 10 ? "0" : "", tt->tm_sec);
586
587 return s;
588 }
589 //-----------------------------------------------------------------------------
590 uint32_t CalcMask(uint32_t msk)
591 {
592 if (msk >= 32) return 0xFFffFFff;
593 if (msk == 0) return 0;
594 return htonl(0xFFffFFff << (32 - msk));
595 }
596 //---------------------------------------------------------------------------
597 void TouchFile(const string & fileName)
598 {
599 FILE * f = fopen(fileName.c_str(), "w");
600 if (f)
601     fclose(f);
602 }
603 //---------------------------------------------------------------------------
604 #ifdef WIN32
605 void EncodeStr(char * str, unsigned long serial, int useHDD)
606 {
607 int len = strlen(str);
608 char stren[100];
609 int i, j = 0;
610 char c1, c2;
611 char serial_c[sizeof(serial)];
612 memcpy(serial_c, &serial, sizeof(serial));
613
614 for (i = 0; i < len; i++)
615     {
616     if (!useHDD)
617         str[i] = str[i]^49;
618     else
619         {
620         str[i] = str[i]^serial_c[j%sizeof(serial)];
621         j++;
622         }
623     }
624
625 for (i = 0; i < 2*len; i++)
626     {
627     if (i%2)
628         {
629         c1 = (str[i/2] >> 4);
630         c1 = c1 + 50;
631         stren[i] = c1;
632         }
633     else
634         {
635         c2 = (str[i/2] & 0x0f);
636         c2 += 50;
637         stren[i] = c2;
638         }
639     }
640 stren[i] = 0;
641 strcpy(str, stren);
642 }
643 //---------------------------------------------------------------------------
644 void DecodeStr(char * str, unsigned long serial, int useHDD)
645 {
646 int len = strlen(str);
647 char strdc[100];
648 int i, j = 0;
649 char c1, c2;
650 char serial_c[sizeof(serial)];
651 memcpy(serial_c, &serial, sizeof(serial));
652
653 for (i = 0; i < len; i += 2)
654     {
655     c1 = (str[i] - 50);
656     c2 = (str[i+1] - 50)<<4;
657     strdc[i/2] = c1+c2;
658     }
659 for (i = 0; i < len/2; i++)
660     {
661     if (!useHDD)
662         strdc[i] = strdc[i]^49;
663     else
664         {
665         strdc[i] = strdc[i]^serial_c[j%sizeof(serial)];
666         j++;
667         }
668     }
669 strdc[i] = 0;
670 strcpy(str, strdc);
671 }
672 //---------------------------------------------------------------------------
673 #endif //WIN32
674 void SwapBytes(uint16_t & value)
675 {
676     value = (value >> 8) |
677             (value << 8);
678 }
679 //---------------------------------------------------------------------------
680 void SwapBytes(uint32_t & value)
681 {
682     value = (value >> 24) |
683             ((value << 8) &  0x00FF0000L)|
684             ((value >> 8) &  0x0000FF00L)|
685             (value << 24);
686 }
687 //---------------------------------------------------------------------------
688 void SwapBytes(uint64_t & value)
689 {
690     value = (value >> 56) |
691             ((value << 40) & 0x00FF000000000000LL) |
692             ((value << 24) & 0x0000FF0000000000LL) |
693             ((value << 8)  & 0x000000FF00000000LL) |
694             ((value >> 8)  & 0x00000000FF000000LL) |
695             ((value >> 24) & 0x0000000000FF0000LL) |
696             ((value >> 40) & 0x000000000000FF00LL) |
697             (value << 56);
698 }
699 //---------------------------------------------------------------------------
700 void SwapBytes(int16_t & value)
701 {
702     uint16_t temp = value;
703     SwapBytes(temp);
704     value = temp;
705 }
706 //---------------------------------------------------------------------------
707 void SwapBytes(int32_t & value)
708 {
709     uint32_t temp = value;
710     SwapBytes(temp);
711     value = temp;
712 }
713 //---------------------------------------------------------------------------
714 void SwapBytes(int64_t & value)
715 {
716     uint64_t temp = value;
717     SwapBytes(temp);
718     value = temp;
719 }
720 //---------------------------------------------------------------------------
721 int str2x(const std::string & str, int & x)
722 {
723 x = strtol(str.c_str(), NULL, 10);
724
725 if (errno == ERANGE)
726     return -1;
727
728 return 0;
729 }
730 //---------------------------------------------------------------------------
731 int str2x(const std::string & str, unsigned & x)
732 {
733 x = strtoul(str.c_str(), NULL, 10);
734
735 if (errno == ERANGE)
736     return -1;
737
738 return 0;
739 }
740 //---------------------------------------------------------------------------
741 int str2x(const std::string & str, long & x)
742 {
743 x = strtol(str.c_str(), NULL, 10);
744
745 if (errno == ERANGE)
746     return -1;
747
748 return 0;
749 }
750 //---------------------------------------------------------------------------
751 int str2x(const std::string & str, unsigned long & x)
752 {
753 x = strtoul(str.c_str(), NULL, 10);
754
755 if (errno == ERANGE)
756     return -1;
757
758 return 0;
759 }
760 //---------------------------------------------------------------------------
761 int str2x(const std::string & str, long long & x)
762 {
763 x = strtoll(str.c_str(), NULL, 10);
764
765 if (errno == ERANGE)
766     return -1;
767
768 return 0;
769 }
770 //---------------------------------------------------------------------------
771 int str2x(const std::string & str, unsigned long long & x)
772 {
773 x = strtoull(str.c_str(), NULL, 10);
774
775 if (errno == ERANGE)
776     return -1;
777
778 return 0;
779 }
780 //---------------------------------------------------------------------------
781 const std::string & x2str(unsigned x, std::string & s)
782 {
783 return unsigned2str(x, s);
784 }
785 //---------------------------------------------------------------------------
786 const std::string & x2str(unsigned long x, std::string & s)
787 {
788 return unsigned2str(x, s);
789 }
790 //---------------------------------------------------------------------------
791 const std::string & x2str(unsigned long long x, std::string & s)
792 {
793 return unsigned2str(x, s);
794 }
795 //---------------------------------------------------------------------------
796 std::string & TrimL(std::string & val)
797 {
798 size_t pos = val.find_first_not_of(" \t");
799 if (pos == std::string::npos)
800     {
801     val.erase(val.begin(), val.end());
802     }
803 else
804     {
805     val.erase(0, pos);
806     }
807 return val;
808 }
809 //---------------------------------------------------------------------------
810 std::string & TrimR(std::string & val)
811 {
812 size_t pos = val.find_last_not_of(" \t");
813 if (pos != std::string::npos)
814     {
815     val.erase(pos + 1);
816     }
817 return val;
818 }
819 //---------------------------------------------------------------------------
820 std::string & Trim(std::string & val)
821 {
822 return TrimR(TrimL(val));
823 }
824 //---------------------------------------------------------------------------
825 time_t stg_timegm(struct tm * brokenTime)
826 {
827 #ifdef HAVE_TIMEGM
828 return timegm(brokenTime);
829 #else
830 time_t ret;
831 char *tz;
832 tz = getenv("TZ");
833 setenv("TZ", "", 1);
834 tzset();
835 ret = mktime(brokenTime);
836 if (tz)
837     setenv("TZ", tz, 1);
838 else
839     unsetenv("TZ");
840 tzset();
841 return ret;
842 #endif
843 }
844 //---------------------------------------------------------------------------
845 std::string IconvString(const std::string & source,
846                         const std::string & from,
847                         const std::string & to)
848 {
849 if (source.empty())
850     return std::string();
851
852 size_t inBytesLeft = source.length() + 1;
853 size_t outBytesLeft = source.length() * 2 + 1;
854
855 char * inBuf = new char[inBytesLeft];
856 char * outBuf = new char[outBytesLeft];
857
858 strncpy(inBuf, source.c_str(), source.length());
859
860 inBuf[source.length()] = 0;
861
862 #if defined(FREE_BSD) || defined(FREE_BSD5)
863 const char * srcPos = inBuf;
864 #else
865 char * srcPos = inBuf;
866 #endif
867 char * dstPos = outBuf;
868
869 iconv_t handle = iconv_open(to.c_str(),
870                             from.c_str());
871
872 if (handle == iconv_t(-1))
873     {
874     if (errno == EINVAL)
875         {
876         printfd(__FILE__, "IconvString(): iconv from %s to %s failed\n", from.c_str(), to.c_str());
877         delete[] outBuf;
878         delete[] inBuf;
879         return source;
880         }
881     else
882         printfd(__FILE__, "IconvString(): iconv_open error\n");
883
884     delete[] outBuf;
885     delete[] inBuf;
886     return source;
887     }
888
889 size_t res = iconv(handle,
890                    &srcPos, &inBytesLeft,
891                    &dstPos, &outBytesLeft);
892
893 if (res == size_t(-1))
894     {
895     printfd(__FILE__, "IconvString(): '%s'\n", strerror(errno));
896
897     iconv_close(handle);
898     delete[] outBuf;
899     delete[] inBuf;
900     return source;
901     }
902
903 dstPos = 0;
904
905 std::string dst(outBuf);
906
907 iconv_close(handle);
908
909 delete[] outBuf;
910 delete[] inBuf;
911
912 return dst;
913 }
914 //---------------------------------------------------------------------------