]> git.stg.codes - stg.git/blob - stglibs/common.lib/include/stg/common.h
Added functions to convert strings into uids/gids/modes.
[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 #include "stg/os_int.h"
31 #include "stg/const.h"
32
33 #include <string>
34 #include <sstream>
35 #include <ctime>
36
37 #include <unistd.h> // uid_t, gid_t
38 #include <sys/stat.h> // mode_t
39
40 #define STAT_TIME_3         (1)
41 #define STAT_TIME_2         (2)
42 #define STAT_TIME_1         (3)
43 #define STAT_TIME_1_2       (4)
44 #define STAT_TIME_1_4       (5)
45 #define STAT_TIME_1_6       (6)
46
47 #define FN_STR_LEN      (NAME_MAX)
48
49 #define ST_F    0
50 #define ST_B    1
51 #define ST_KB   2
52 #define ST_MB   3
53
54 //-----------------------------------------------------------------------------
55 const char    * IntToKMG(int64_t a, int statType = ST_F);
56 const char    * LogDate(time_t t);
57 int             ParesTimeStat(const char * str);
58 int             IsTimeStat(struct tm * t, int statTime);
59 /*bool            IsDigit(char c);
60 bool            IsAlpha(char c);*/
61 int             strtodouble2(const char * s, double &a);
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 inline std::string Encode12str(const std::string & src) { std::string dst; Encode12str(dst, src); return dst; }
69 inline std::string Decode21str(const std::string & src) { std::string dst; Decode21str(dst, src); return dst; }
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 //char          * inet_ntostr(unsigned long);
80 std::string     inet_ntostring(uint32_t);
81 uint32_t        inet_strington(const std::string & value);
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
101 std::string ToLower(std::string value);
102 std::string ToUpper(std::string value);
103
104 template <typename C, typename F>
105 C Split(const std::string & value, char delim, F conv)
106 {
107 C res;
108 size_t startPos = 0;
109 size_t pos = value.find_first_of(delim);
110 while (pos != std::string::npos)
111     {
112     res.push_back(conv(value.substr(startPos, pos - startPos)));
113     startPos = pos + 1;
114     pos = value.find_first_of(delim, pos + 1);
115     }
116 res.push_back(conv(value.substr(startPos, pos - startPos)));
117 return res;
118 }
119
120 template <typename T>
121 T FromString(const std::string & value)
122 {
123 T res;
124 std::istringstream stream(value);
125 stream >> res;
126 return res;
127 }
128
129 template <typename C>
130 C Split(const std::string & value, char delim)
131 {
132     return Split<C>(value, delim, FromString);
133 }
134
135 std::string IconvString(const std::string & source, const std::string & from, const std::string & to);
136
137 int ParseInt(const std::string & str, int * val);
138 int ParseUnsigned(const std::string & str, unsigned * val);
139 int ParseIntInRange(const std::string & str, int min, int max, int * val);
140 int ParseUnsignedInRange(const std::string & str, unsigned min,
141                          unsigned max, unsigned * val);
142 int ParseYesNo(const std::string & str, bool * val);
143
144 bool WaitPackets(int sd);
145
146 bool ReadAll(int sd, void * dest, size_t size);
147 bool WriteAll(int sd, const void * source, size_t size);
148
149 std::string ToPrintable(const std::string & src);
150
151 //-----------------------------------------------------------------------------
152 int str2x(const std::string & str, int32_t & x);
153 int str2x(const std::string & str, uint32_t & x);
154 #ifndef WIN32
155 int str2x(const std::string & str, int64_t & x);
156 int str2x(const std::string & str, uint64_t & x);
157 #endif
158 //-----------------------------------------------------------------------------
159 const std::string & x2str(uint32_t x, std::string & s);
160 const std::string & x2str(uint64_t x, std::string & s);
161 //-----------------------------------------------------------------------------
162 const std::string & x2str(double x, std::string & s);
163 //-----------------------------------------------------------------------------
164
165 template <typename varT>
166 int str2x(const std::string & str, varT & x);
167 template <typename varT>
168 const std::string & x2str(varT x, std::string & s);
169 template <typename varT>
170 std::string x2str(varT x) { std::string s; return x2str(x, s); }
171 template <typename varT>
172 const std::string & unsigned2str(varT x, std::string & s);
173 template <typename varT>
174 std::string unsigned2str(varT x) { std::string s; return unsigned2str(x, s); }
175
176 //-----------------------------------------------------------------------------
177 template <typename varT>
178 inline
179 int str2x(const std::string & str, varT & x)
180 {
181     int pos = 0;
182     int minus = 1;
183
184     if (str.empty())
185         return -1;
186
187     if (str[0] == '+')
188         pos++;
189
190     if (str[0] == '-')
191     {
192         pos++;
193         minus = -1;
194     }
195
196     if ((str[pos] < '0' || str[pos] > '9'))
197         return -1;
198
199     x = str[pos++] - '0';
200
201     for (unsigned i = pos; i < str.size(); i++)
202     {
203         if ((str[i] < '0' || str[i] > '9'))
204             return -1;
205
206         x *= 10;
207         x += str[i] - '0';
208     }
209
210     x*= minus;
211
212     return 0;
213 }
214 //-----------------------------------------------------------------------------
215 template <typename varT>
216 inline
217 const std::string & x2str(varT x, std::string & s)
218 {
219     varT xx = x;
220     int pos = 1;
221
222     x /= 10;
223     while (x != 0)
224     {
225         x /= 10;
226         pos++;
227     }
228
229     if (xx < 0)
230     {
231         pos++;
232         s.resize(pos, 0);
233         s[0] = '-';
234     }
235     else if (xx > 0)
236     {
237         s.resize(pos, 0);
238     }
239     else
240     {
241         s.resize(1, 0);
242         s[0] = '0';
243         return s;
244     }
245
246     x = xx;
247
248     while (x != 0)
249     {
250         if (x < 0)
251             s[--pos] = -(x % 10) + '0';
252         else
253             s[--pos] = x % 10 + '0';
254
255         x /= 10;
256     }
257
258     return s;
259 }
260 //-----------------------------------------------------------------------------
261 template <typename varT>
262 inline
263 const std::string & unsigned2str(varT x, std::string & s)
264 {
265     varT xx = x;
266     int pos = 1;
267
268     x /= 10;
269     while (x != 0)
270     {
271         x /= 10;
272         pos++;
273     }
274
275     if (xx > 0)
276     {
277         s.resize(pos, 0);
278     }
279     else
280     {
281         s.resize(1, 0);
282         s[0] = '0';
283         return s;
284     }
285
286     x = xx;
287
288     while (x != 0)
289     {
290         s[--pos] = x % 10 + '0';
291
292         x /= 10;
293     }
294
295     return s;
296 }
297 //-----------------------------------------------------------------------------
298 char * stg_strptime(const char *, const char *, struct tm *);
299 time_t stg_timegm(struct tm *);
300
301 uid_t str2uid(const std::string& name);
302 gid_t str2gid(const std::string& name);
303 mode_t str2mode(const std::string& mode);
304
305 #endif