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