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