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