]> git.stg.codes - stg.git/blob - stglibs/common.lib/include/stg/common.h
Added special function to read/write all data (for TCP).
[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 inline
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 inline
124 T FromString(const std::string & value)
125 {
126 T res;
127 std::istringstream stream(value);
128 stream >> res;
129 return res;
130 }
131
132 template <>
133 inline
134 std::string FromString<std::string>(const std::string & value)
135 {
136 return value;
137 }
138
139 template <typename C>
140 inline
141 C Split(const std::string & value, char delim)
142 {
143     return Split<C>(value, delim, FromString<typename C::value_type>);
144 }
145
146 std::string IconvString(const std::string & source, const std::string & from, const std::string & to);
147
148 int ParseInt(const std::string & str, int * val);
149 int ParseUnsigned(const std::string & str, unsigned * val);
150 int ParseIntInRange(const std::string & str, int min, int max, int * val);
151 int ParseUnsignedInRange(const std::string & str, unsigned min,
152                          unsigned max, unsigned * val);
153 int ParseYesNo(const std::string & str, bool * val);
154
155 bool WaitPackets(int sd);
156
157 bool ReadAll(int sd, void * dest, size_t size);
158 bool WriteAll(int sd, const void * source, size_t size);
159
160 //-----------------------------------------------------------------------------
161 int str2x(const std::string & str, int32_t & x);
162 int str2x(const std::string & str, uint32_t & x);
163 inline
164 int str2x(const std::string & str, double & x) { return strtodouble2(str.c_str(), x); }
165 #ifndef WIN32
166 int str2x(const std::string & str, int64_t & x);
167 int str2x(const std::string & str, uint64_t & x);
168 #endif
169 //-----------------------------------------------------------------------------
170 const std::string & x2str(uint32_t x, std::string & s);
171 const std::string & x2str(uint64_t x, std::string & s);
172 //-----------------------------------------------------------------------------
173 const std::string & x2str(double x, std::string & s);
174 //-----------------------------------------------------------------------------
175
176 template <typename varT>
177 int str2x(const std::string & str, varT & x);
178 template <typename varT>
179 const std::string & x2str(varT x, std::string & s);
180 template <typename varT>
181 std::string x2str(varT x) { std::string s; return x2str(x, s); }
182 template <typename varT>
183 const std::string & unsigned2str(varT x, std::string & s);
184 template <typename varT>
185 std::string unsigned2str(varT x) { std::string s; return unsigned2str(x, s); }
186
187 //-----------------------------------------------------------------------------
188 template <typename varT>
189 inline
190 int str2x(const std::string & str, varT & x)
191 {
192     int pos = 0;
193     int minus = 1;
194
195     if (str.empty())
196         return -1;
197
198     if (str[0] == '+')
199         pos++;
200
201     if (str[0] == '-')
202     {
203         pos++;
204         minus = -1;
205     }
206
207     if ((str[pos] < '0' || str[pos] > '9'))
208         return -1;
209
210     x = str[pos++] - '0';
211
212     for (unsigned i = pos; i < str.size(); i++)
213     {
214         if ((str[i] < '0' || str[i] > '9'))
215             return -1;
216
217         x *= 10;
218         x += str[i] - '0';
219     }
220
221     x *= minus;
222
223     return 0;
224 }
225 //-----------------------------------------------------------------------------
226 template <typename varT>
227 inline
228 const std::string & x2str(varT x, std::string & s)
229 {
230     varT xx = x;
231     int pos = 1;
232
233     x /= 10;
234     while (x != 0)
235     {
236         x /= 10;
237         pos++;
238     }
239
240     if (xx < 0)
241     {
242         pos++;
243         s.resize(pos, 0);
244         s[0] = '-';
245     }
246     else if (xx > 0)
247     {
248         s.resize(pos, 0);
249     }
250     else
251     {
252         s.resize(1, 0);
253         s[0] = '0';
254         return s;
255     }
256
257     x = xx;
258
259     while (x != 0)
260     {
261         if (x < 0)
262             s[--pos] = -(x % 10) + '0';
263         else
264             s[--pos] = x % 10 + '0';
265
266         x /= 10;
267     }
268
269     return s;
270 }
271 //-----------------------------------------------------------------------------
272 template <typename varT>
273 inline
274 const std::string & unsigned2str(varT x, std::string & s)
275 {
276     varT xx = x;
277     int pos = 1;
278
279     x /= 10;
280     while (x != 0)
281     {
282         x /= 10;
283         pos++;
284     }
285
286     if (xx > 0)
287     {
288         s.resize(pos, 0);
289     }
290     else
291     {
292         s.resize(1, 0);
293         s[0] = '0';
294         return s;
295     }
296
297     x = xx;
298
299     while (x != 0)
300     {
301         s[--pos] = x % 10 + '0';
302
303         x /= 10;
304     }
305
306     return s;
307 }
308 //-----------------------------------------------------------------------------
309 char * stg_strptime(const char *, const char *, struct tm *);
310 time_t stg_timegm(struct tm *);
311
312 #endif