]> git.stg.codes - stg.git/blob - stglibs/common.lib/include/stg/common.h
Changes in SendMessage method.
[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
37 #include "stg/os_int.h"
38 #include "stg/const.h"
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 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 //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(const std::string & value);
102 std::string     ToUpper(const std::string & value);
103
104 std::string     IconvString(const std::string & source, const std::string & from, const std::string & to);
105
106 int ParseInt(const std::string & str, int * val);
107 int ParseUnsigned(const std::string & str, unsigned * val);
108 int ParseIntInRange(const std::string & str, int min, int max, int * val);
109 int ParseUnsignedInRange(const std::string & str, unsigned min,
110                          unsigned max, unsigned * val);
111 int ParseYesNo(const std::string & str, bool * val);
112
113 bool WaitPackets(int sd);
114
115 template <typename varT>
116 int str2x(const std::string & str, varT & x);
117 template <typename varT>
118 const std::string & x2str(varT x, std::string & s);
119 template <typename varT>
120 std::string x2str(varT x) { std::string s; return x2str(x, s); }
121 template <typename varT>
122 const std::string & unsigned2str(varT x, std::string & s);
123 template <typename varT>
124 std::string unsigned2str(varT x) { std::string s; return unsigned2str(x, s); }
125
126 //-----------------------------------------------------------------------------
127 template <typename varT>
128 inline
129 int str2x(const std::string & str, varT & x)
130 {
131     int pos = 0;
132     int minus = 1;
133
134     if (str.empty())
135         return -1;
136
137     if (str[0] == '+')
138         pos++;
139
140     if (str[0] == '-')
141     {
142         pos++;
143         minus = -1;
144     }
145
146     if ((str[pos] < '0' || str[pos] > '9'))
147         return -1;
148
149     x = str[pos++] - '0';
150
151     for (unsigned i = pos; i < str.size(); i++)
152     {
153         if ((str[i] < '0' || str[i] > '9'))
154             return -1;
155
156         x *= 10;
157         x += str[i] - '0';
158     }
159
160     x *= minus;
161
162     return 0;
163 }
164 //-----------------------------------------------------------------------------
165 template <typename varT>
166 inline
167 const std::string & x2str(varT x, std::string & s)
168 {
169     varT xx = x;
170     int pos = 1;
171
172     x /= 10;
173     while (x != 0)
174     {
175         x /= 10;
176         pos++;
177     }
178
179     if (xx < 0)
180     {
181         pos++;
182         s.resize(pos, 0);
183         s[0] = '-';
184     }
185     else if (xx > 0)
186     {
187         s.resize(pos, 0);
188     }
189     else
190     {
191         s.resize(1, 0);
192         s[0] = '0';
193         return s;
194     }
195
196     x = xx;
197
198     while (x != 0)
199     {
200         if (x < 0)
201             s[--pos] = -(x % 10) + '0';
202         else
203             s[--pos] = x % 10 + '0';
204
205         x /= 10;
206     }
207
208     return s;
209 }
210 //-----------------------------------------------------------------------------
211 template <typename varT>
212 inline
213 const std::string & unsigned2str(varT x, std::string & s)
214 {
215     varT xx = x;
216     int pos = 1;
217
218     x /= 10;
219     while (x != 0)
220     {
221         x /= 10;
222         pos++;
223     }
224
225     if (xx > 0)
226     {
227         s.resize(pos, 0);
228     }
229     else
230     {
231         s.resize(1, 0);
232         s[0] = '0';
233         return s;
234     }
235
236     x = xx;
237
238     while (x != 0)
239     {
240         s[--pos] = x % 10 + '0';
241
242         x /= 10;
243     }
244
245     return s;
246 }
247 //-----------------------------------------------------------------------------
248 int str2x(const std::string & str, int32_t & x);
249 int str2x(const std::string & str, uint32_t & x);
250 #ifndef WIN32
251 int str2x(const std::string & str, int64_t & x);
252 int str2x(const std::string & str, uint64_t & x);
253 #endif
254 //-----------------------------------------------------------------------------
255 const std::string & x2str(uint32_t x, std::string & s);
256 const std::string & x2str(uint64_t x, std::string & s);
257 //-----------------------------------------------------------------------------
258 char * stg_strptime(const char *, const char *, struct tm *);
259 time_t stg_timegm(struct tm *);
260
261 #endif