]> git.stg.codes - stg.git/blob - stglibs/common.lib/include/stg/common.h
TUT framework updated to svn version
[stg.git] / stglibs / common.lib / include / stg / common.h
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  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.32 $
23  $Date: 2010/11/08 10:11:19 $
24  $Author: faust $
25  */
26
27 #ifndef common_h
28 #define common_h
29
30 #ifdef __BORLANDC__
31 #include <time.h>
32 #else
33 #include <ctime>
34 #endif
35 #include <string>
36
37 #include "stg/os_int.h"
38 #include "stg/const.h"
39
40 #define STAT_TIME_3         (1)
41 #define STAT_TIME_2         (2)
42 #define STAT_TIME_1         (3)
43 #define STAT_TIME_1_2       (4)
44 #define STAT_TIME_1_4       (5)
45 #define STAT_TIME_1_6       (6)
46
47 #define FN_STR_LEN      (NAME_MAX)
48
49 #define ST_F    0
50 #define ST_B    1
51 #define ST_KB   2
52 #define ST_MB   3
53
54 //-----------------------------------------------------------------------------
55 const char    * IntToKMG(long long a, int statType = ST_F);
56 const char    * LogDate(time_t t);
57 int             ParesTimeStat(const char * str);
58 int             IsTimeStat(struct tm * t, int statTime);
59 /*bool            IsDigit(char c);
60 bool            IsAlpha(char c);*/
61 int             strtodouble2(const char * s, double &a);
62 int             printfd(const char * __file__, const char * fmt, ...);
63 void            Encode12(char * dst, const char * src, size_t srcLen);
64 void            Decode21(char * dst, const char * src);
65
66 void            Encode12str(std::string & dst, const std::string & src);
67 void            Decode21str(std::string & dst, const std::string & src);
68
69 int             ParseIPString(const char * str, uint32_t * ips, int maxIP);
70 void            KOIToWin(const char * s1, char * s2, int l);
71 void            WinToKOI(const char * s1, char * s2, int l);
72 void            KOIToWin(const std::string & s1, std::string * s2);
73 void            WinToKOI(const std::string & s1, std::string * s2);
74 int             DaysInMonth(unsigned year, unsigned mon);
75 int             DaysInCurrentMonth();
76 int             Min8(int a);
77 //char          * inet_ntostr(unsigned long);
78 std::string     inet_ntostring(uint32_t);
79 uint32_t        inet_strington(const std::string & value);
80 int             strprintf(std::string * str, const char * fmt, ...);
81 int             ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2);
82 uint32_t        CalcMask(uint32_t msk);
83 void            TouchFile(const std::string & fileName);
84 #ifdef WIN32
85 void            EncodeStr(char * str, unsigned long serial, int useHDD);
86 void            DecodeStr(char * str, unsigned long serial, int useHDD);
87 #endif //WIN32
88 void            SwapBytes(uint16_t & value);
89 void            SwapBytes(uint32_t & value);
90 void            SwapBytes(uint64_t & value);
91 void            SwapBytes(int16_t & value);
92 void            SwapBytes(int32_t & value);
93 void            SwapBytes(int64_t & value);
94
95 std::string &   TrimL(std::string & val);
96 std::string &   TrimR(std::string & val);
97 std::string &   Trim(std::string & val);
98
99 std::string     IconvString(const std::string & source, const std::string & from, const std::string & to);
100
101 int ParseInt(const std::string & str, int * val);
102 int ParseUnsigned(const std::string & str, unsigned * val);
103 int ParseIntInRange(const std::string & str, int min, int max, int * val);
104 int ParseUnsignedInRange(const std::string & str, unsigned min,
105                          unsigned max, unsigned * val);
106 int ParseYesNo(const std::string & str, bool * val);
107
108 bool WaitPackets(int sd);
109
110 //-----------------------------------------------------------------------------
111 template <typename varT>
112 int str2x(const std::string & str, varT & x)
113 {
114     int pos = 0;
115     int minus = 1;
116
117     if (str.empty())
118         return -1;
119
120     if (str[0] == '+')
121         pos++;
122
123     if (str[0] == '-')
124     {
125         pos++;
126         minus = -1;
127     }
128
129     if ((str[pos] < '0' || str[pos] > '9'))
130         return -1;
131
132     x = str[pos++] - '0';
133
134     for (unsigned i = pos; i < str.size(); i++)
135     {
136         if ((str[i] < '0' || str[i] > '9'))
137             return -1;
138
139         x *= 10;
140         x += str[i] - '0';
141     }
142
143     x*= minus;
144
145     return 0;
146 }
147 //-----------------------------------------------------------------------------
148 template <typename varT>
149 const std::string & x2str(varT x, std::string & s)
150 {
151     varT xx = x;
152     int pos = 1;
153
154     x /= 10;
155     while (x != 0)
156     {
157         x /= 10;
158         pos++;
159     }
160
161     if (xx < 0)
162     {
163         pos++;
164         s.resize(pos, 0);
165         s[0] = '-';
166     }
167     else if (xx > 0)
168     {
169         s.resize(pos, 0);
170     }
171     else
172     {
173         s.resize(1, 0);
174         s[0] = '0';
175         return s;
176     }
177
178     x = xx;
179
180     while (x != 0)
181     {
182         if (x < 0)
183             s[--pos] = -(x % 10) + '0';
184         else
185             s[--pos] = x % 10 + '0';
186
187         x /= 10;
188     }
189
190     return s;
191 }
192 //-----------------------------------------------------------------------------
193 template <typename varT>
194 const std::string & unsigned2str(varT x, std::string & s)
195 {
196     varT xx = x;
197     int pos = 1;
198
199     x /= 10;
200     while (x != 0)
201     {
202         x /= 10;
203         pos++;
204     }
205
206     if (xx > 0)
207     {
208         s.resize(pos, 0);
209     }
210     else
211     {
212         s.resize(1, 0);
213         s[0] = '0';
214         return s;
215     }
216
217     x = xx;
218
219     while (x != 0)
220     {
221         s[--pos] = x % 10 + '0';
222
223         x /= 10;
224     }
225
226     return s;
227 }
228 //-----------------------------------------------------------------------------
229 int str2x(const std::string & str, int & x);
230 int str2x(const std::string & str, unsigned & x);
231 int str2x(const std::string & str, long & x);
232 int str2x(const std::string & str, unsigned long & x);
233 #ifndef WIN32
234 int str2x(const std::string & str, long long & x);
235 int str2x(const std::string & str, unsigned long long & x);
236 #endif
237 //-----------------------------------------------------------------------------
238 const std::string & x2str(unsigned x, std::string & s);
239 const std::string & x2str(unsigned long x, std::string & s);
240 const std::string & x2str(unsigned long long x, std::string & s);
241 //-----------------------------------------------------------------------------
242 char * stg_strptime(const char *, const char *, struct tm *);
243 time_t stg_timegm(struct tm *);
244
245 #endif