]> git.stg.codes - stg.git/blob - stglibs/common.lib/include/stg/common.h
Fixed unary x2str.
[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 inline std::string Encode12str(const std::string & src) { std::string dst; Encode12str(dst, src); return dst; }
70 inline std::string Decode21str(const std::string & src) { std::string dst; Decode21str(dst, src); return dst; }
71
72 int             ParseIPString(const char * str, uint32_t * ips, int maxIP);
73 void            KOIToWin(const char * s1, char * s2, int l);
74 void            WinToKOI(const char * s1, char * s2, int l);
75 void            KOIToWin(const std::string & s1, std::string * s2);
76 void            WinToKOI(const std::string & s1, std::string * s2);
77 int             DaysInMonth(unsigned year, unsigned mon);
78 int             DaysInCurrentMonth();
79 int             Min8(int a);
80 //char          * inet_ntostr(unsigned long);
81 std::string     inet_ntostring(uint32_t);
82 uint32_t        inet_strington(const std::string & value);
83 int             strprintf(std::string * str, const char * fmt, ...);
84 int             ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2);
85 uint32_t        CalcMask(uint32_t msk);
86 void            TouchFile(const std::string & fileName);
87 #ifdef WIN32
88 void            EncodeStr(char * str, unsigned long serial, int useHDD);
89 void            DecodeStr(char * str, unsigned long serial, int useHDD);
90 #endif //WIN32
91 void            SwapBytes(uint16_t & value);
92 void            SwapBytes(uint32_t & value);
93 void            SwapBytes(uint64_t & value);
94 void            SwapBytes(int16_t & value);
95 void            SwapBytes(int32_t & value);
96 void            SwapBytes(int64_t & value);
97
98 std::string &   TrimL(std::string & val);
99 std::string &   TrimR(std::string & val);
100 std::string &   Trim(std::string & val);
101
102 std::string ToLower(std::string value);
103 std::string ToUpper(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 + 1);
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 bool ReadAll(int sd, void * dest, size_t size);
148 bool WriteAll(int sd, const void * source, size_t size);
149
150 std::string ToPrintable(const std::string & src);
151
152 //-----------------------------------------------------------------------------
153 int str2x(const std::string & str, int32_t & x);
154 int str2x(const std::string & str, uint32_t & x);
155 #ifndef WIN32
156 int str2x(const std::string & str, int64_t & x);
157 int str2x(const std::string & str, uint64_t & x);
158 #endif
159 //-----------------------------------------------------------------------------
160 const std::string & x2str(uint32_t x, std::string & s);
161 const std::string & x2str(uint64_t x, std::string & s);
162 //-----------------------------------------------------------------------------
163 const std::string & x2str(double x, std::string & s);
164 //-----------------------------------------------------------------------------
165
166 template <typename varT>
167 int str2x(const std::string & str, varT & x);
168 template <typename varT>
169 const std::string & x2str(varT x, std::string & s);
170 template <typename varT>
171 std::string x2str(varT x) { std::string s; return x2str(x, s); }
172 template <typename varT>
173 const std::string & unsigned2str(varT x, std::string & s);
174 template <typename varT>
175 std::string unsigned2str(varT x) { std::string s; return unsigned2str(x, s); }
176
177 //-----------------------------------------------------------------------------
178 template <typename varT>
179 inline
180 int str2x(const std::string & str, varT & x)
181 {
182     int pos = 0;
183     int minus = 1;
184
185     if (str.empty())
186         return -1;
187
188     if (str[0] == '+')
189         pos++;
190
191     if (str[0] == '-')
192     {
193         pos++;
194         minus = -1;
195     }
196
197     if ((str[pos] < '0' || str[pos] > '9'))
198         return -1;
199
200     x = str[pos++] - '0';
201
202     for (unsigned i = pos; i < str.size(); i++)
203     {
204         if ((str[i] < '0' || str[i] > '9'))
205             return -1;
206
207         x *= 10;
208         x += str[i] - '0';
209     }
210
211     x*= minus;
212
213     return 0;
214 }
215 //-----------------------------------------------------------------------------
216 template <typename varT>
217 inline
218 const std::string & x2str(varT x, std::string & s)
219 {
220     varT xx = x;
221     int pos = 1;
222
223     x /= 10;
224     while (x != 0)
225     {
226         x /= 10;
227         pos++;
228     }
229
230     if (xx < 0)
231     {
232         pos++;
233         s.resize(pos, 0);
234         s[0] = '-';
235     }
236     else if (xx > 0)
237     {
238         s.resize(pos, 0);
239     }
240     else
241     {
242         s.resize(1, 0);
243         s[0] = '0';
244         return s;
245     }
246
247     x = xx;
248
249     while (x != 0)
250     {
251         if (x < 0)
252             s[--pos] = -(x % 10) + '0';
253         else
254             s[--pos] = x % 10 + '0';
255
256         x /= 10;
257     }
258
259     return s;
260 }
261 //-----------------------------------------------------------------------------
262 template <typename varT>
263 inline
264 const std::string & unsigned2str(varT x, std::string & s)
265 {
266     varT xx = x;
267     int pos = 1;
268
269     x /= 10;
270     while (x != 0)
271     {
272         x /= 10;
273         pos++;
274     }
275
276     if (xx > 0)
277     {
278         s.resize(pos, 0);
279     }
280     else
281     {
282         s.resize(1, 0);
283         s[0] = '0';
284         return s;
285     }
286
287     x = xx;
288
289     while (x != 0)
290     {
291         s[--pos] = x % 10 + '0';
292
293         x /= 10;
294     }
295
296     return s;
297 }
298 //-----------------------------------------------------------------------------
299 char * stg_strptime(const char *, const char *, struct tm *);
300 time_t stg_timegm(struct tm *);
301
302 #endif