]> git.stg.codes - stg.git/blob - stglibs/common.lib/include/stg/common.h
e2eb0d58b3ad9f70abbd8d065df48958dbb85c62
[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 #include <sstream>
37
38 #include "stg/os_int.h"
39 #include "stg/const.h"
40
41 #define STAT_TIME_3         (1)
42 #define STAT_TIME_2         (2)
43 #define STAT_TIME_1         (3)
44 #define STAT_TIME_1_2       (4)
45 #define STAT_TIME_1_4       (5)
46 #define STAT_TIME_1_6       (6)
47
48 #define FN_STR_LEN      (NAME_MAX)
49
50 #define ST_F    0
51 #define ST_B    1
52 #define ST_KB   2
53 #define ST_MB   3
54
55 //-----------------------------------------------------------------------------
56 const char    * IntToKMG(int64_t a, int statType = ST_F);
57 const char    * LogDate(time_t t);
58 int             ParesTimeStat(const char * str);
59 int             IsTimeStat(struct tm * t, int statTime);
60 /*bool            IsDigit(char c);
61 bool            IsAlpha(char c);*/
62 int             strtodouble2(const char * s, double &a);
63 int             printfd(const char * __file__, const char * fmt, ...);
64 void            Encode12(char * dst, const char * src, size_t srcLen);
65 void            Decode21(char * dst, const char * src);
66
67 void            Encode12str(std::string & dst, const std::string & src);
68 void            Decode21str(std::string & dst, const std::string & src);
69
70 int             ParseIPString(const char * str, uint32_t * ips, int maxIP);
71 void            KOIToWin(const char * s1, char * s2, int l);
72 void            WinToKOI(const char * s1, char * s2, int l);
73 void            KOIToWin(const std::string & s1, std::string * s2);
74 void            WinToKOI(const std::string & s1, std::string * s2);
75 int             DaysInMonth(unsigned year, unsigned mon);
76 int             DaysInCurrentMonth();
77 int             Min8(int a);
78 //char          * inet_ntostr(unsigned long);
79 std::string     inet_ntostring(uint32_t);
80 uint32_t        inet_strington(const std::string & value);
81 int             strprintf(std::string * str, const char * fmt, ...);
82 int             ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2);
83 uint32_t        CalcMask(uint32_t msk);
84 void            TouchFile(const std::string & fileName);
85 #ifdef WIN32
86 void            EncodeStr(char * str, unsigned long serial, int useHDD);
87 void            DecodeStr(char * str, unsigned long serial, int useHDD);
88 #endif //WIN32
89 void            SwapBytes(uint16_t & value);
90 void            SwapBytes(uint32_t & value);
91 void            SwapBytes(uint64_t & value);
92 void            SwapBytes(int16_t & value);
93 void            SwapBytes(int32_t & value);
94 void            SwapBytes(int64_t & value);
95
96 std::string &   TrimL(std::string & val);
97 std::string &   TrimR(std::string & val);
98 std::string &   Trim(std::string & val);
99
100 std::string ToLower(std::string value);
101 std::string ToUpper(std::string value);
102
103 template <typename C, typename F>
104 C Split(const std::string & value, char delim, F conv)
105 {
106 C res;
107 size_t startPos = 0;
108 size_t pos = value.find_first_of(delim);
109 while (pos != std::string::npos)
110     {
111     res.push_back(conv(value.substr(startPos, pos - startPos)));
112     startPos = pos + 1;
113     pos = value.find_first_of(delim, pos + 1);
114     }
115 res.push_back(conv(value.substr(startPos, pos - startPos)));
116 return res;
117 }
118
119 template <typename T>
120 T FromString(const std::string & value)
121 {
122 T res;
123 std::istringstream stream(value);
124 stream >> res;
125 return res;
126 }
127
128 template <typename C>
129 C Split(const std::string & value, char delim)
130 {
131     return Split<C>(value, delim, FromString);
132 }
133
134 std::string IconvString(const std::string & source, const std::string & from, const std::string & to);
135
136 int ParseInt(const std::string & str, int * val);
137 int ParseUnsigned(const std::string & str, unsigned * val);
138 int ParseIntInRange(const std::string & str, int min, int max, int * val);
139 int ParseUnsignedInRange(const std::string & str, unsigned min,
140                          unsigned max, unsigned * val);
141 int ParseYesNo(const std::string & str, bool * val);
142
143 bool WaitPackets(int sd);
144
145 bool ReadAll(int sd, void * dest, size_t size);
146 bool WriteAll(int sd, const void * source, size_t size);
147
148 std::string ToPrintable(const std::string & src);
149
150 //-----------------------------------------------------------------------------
151 int str2x(const std::string & str, int32_t & x);
152 int str2x(const std::string & str, uint32_t & x);
153 #ifndef WIN32
154 int str2x(const std::string & str, int64_t & x);
155 int str2x(const std::string & str, uint64_t & x);
156 #endif
157 //-----------------------------------------------------------------------------
158 const std::string & x2str(uint32_t x, std::string & s);
159 const std::string & x2str(uint64_t x, std::string & s);
160 //-----------------------------------------------------------------------------
161 const std::string & x2str(double x, std::string & s);
162 //-----------------------------------------------------------------------------
163
164 template <typename varT>
165 int str2x(const std::string & str, varT & x);
166 template <typename varT>
167 const std::string & x2str(varT x, std::string & s);
168 template <typename varT>
169 const std::string & x2str(varT x) { std::string s; return x2str(x, s); }
170 template <typename varT>
171 const std::string & unsigned2str(varT x, std::string & s);
172 template <typename varT>
173 const std::string & unsigned2str(varT x) { std::string s; return unsigned2str(x, s); }
174
175 //-----------------------------------------------------------------------------
176 template <typename varT>
177 inline
178 int str2x(const std::string & str, varT & x)
179 {
180     int pos = 0;
181     int minus = 1;
182
183     if (str.empty())
184         return -1;
185
186     if (str[0] == '+')
187         pos++;
188
189     if (str[0] == '-')
190     {
191         pos++;
192         minus = -1;
193     }
194
195     if ((str[pos] < '0' || str[pos] > '9'))
196         return -1;
197
198     x = str[pos++] - '0';
199
200     for (unsigned i = pos; i < str.size(); i++)
201     {
202         if ((str[i] < '0' || str[i] > '9'))
203             return -1;
204
205         x *= 10;
206         x += str[i] - '0';
207     }
208
209     x*= minus;
210
211     return 0;
212 }
213 //-----------------------------------------------------------------------------
214 template <typename varT>
215 inline
216 const std::string & x2str(varT x, std::string & s)
217 {
218     varT xx = x;
219     int pos = 1;
220
221     x /= 10;
222     while (x != 0)
223     {
224         x /= 10;
225         pos++;
226     }
227
228     if (xx < 0)
229     {
230         pos++;
231         s.resize(pos, 0);
232         s[0] = '-';
233     }
234     else if (xx > 0)
235     {
236         s.resize(pos, 0);
237     }
238     else
239     {
240         s.resize(1, 0);
241         s[0] = '0';
242         return s;
243     }
244
245     x = xx;
246
247     while (x != 0)
248     {
249         if (x < 0)
250             s[--pos] = -(x % 10) + '0';
251         else
252             s[--pos] = x % 10 + '0';
253
254         x /= 10;
255     }
256
257     return s;
258 }
259 //-----------------------------------------------------------------------------
260 template <typename varT>
261 inline
262 const std::string & unsigned2str(varT x, std::string & s)
263 {
264     varT xx = x;
265     int pos = 1;
266
267     x /= 10;
268     while (x != 0)
269     {
270         x /= 10;
271         pos++;
272     }
273
274     if (xx > 0)
275     {
276         s.resize(pos, 0);
277     }
278     else
279     {
280         s.resize(1, 0);
281         s[0] = '0';
282         return s;
283     }
284
285     x = xx;
286
287     while (x != 0)
288     {
289         s[--pos] = x % 10 + '0';
290
291         x /= 10;
292     }
293
294     return s;
295 }
296 //-----------------------------------------------------------------------------
297 char * stg_strptime(const char *, const char *, struct tm *);
298 time_t stg_timegm(struct tm *);
299
300 #endif