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