]> git.stg.codes - stg.git/blob - stglibs/common.lib/common.h
Синхронізовано з CVS
[stg.git] / stglibs / common.lib / 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 <ctime>
31 #include <string>
32
33 #include "os_int.h"
34 #include "stg_const.h"
35
36 #define STAT_TIME_3         (1)
37 #define STAT_TIME_2         (2)
38 #define STAT_TIME_1         (3)
39 #define STAT_TIME_1_2       (4)
40 #define STAT_TIME_1_4       (5)
41 #define STAT_TIME_1_6       (6)
42
43 #define FN_STR_LEN      (NAME_MAX)
44
45 #define ST_F    0
46 #define ST_B    1
47 #define ST_KB   2
48 #define ST_MB   3
49
50 //-----------------------------------------------------------------------------
51 const char    * IntToKMG(long long a, int statType = ST_F);
52 const char    * LogDate(time_t t);
53 int             ParesTimeStat(const char * str);
54 int             IsTimeStat(struct tm * t, int statTime);
55 /*bool            IsDigit(char c);
56 bool            IsAlpha(char c);*/
57 int             strtodouble2(const char * s, double &a);
58 int             printfd(const char * __file__, const char * fmt, ...);
59 void            Encode12(char * dst, const char * src, size_t srcLen);
60 void            Decode21(char * dst, const char * src);
61
62 void            Encode12str(std::string & dst, const std::string & src);
63 void            Decode21str(std::string & dst, const std::string & src);
64
65 int             ParseIPString(const char * str, uint32_t * ips, int maxIP);
66 void            KOIToWin(const char * s1, char * s2, int l);
67 void            WinToKOI(const char * s1, char * s2, int l);
68 void            KOIToWin(const std::string & s1, std::string * s2);
69 void            WinToKOI(const std::string & s1, std::string * s2);
70 int             DaysInMonth(unsigned year, unsigned mon);
71 int             DaysInCurrentMonth();
72 int             Min8(int a);
73 //char          * inet_ntostr(unsigned long);
74 std::string     inet_ntostring(uint32_t);
75 uint32_t        inet_strington(const std::string & value);
76 int             strprintf(std::string * str, const char * fmt, ...);
77 int             ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2);
78 uint32_t        CalcMask(uint32_t msk);
79 void            TouchFile(const std::string & fileName);
80 #ifdef WIN32
81 void            EncodeStr(char * str, unsigned long serial, int useHDD);
82 void            DecodeStr(char * str, unsigned long serial, int useHDD);
83 #endif //WIN32
84 void            SwapBytes(uint16_t & value);
85 void            SwapBytes(uint32_t & value);
86 void            SwapBytes(uint64_t & value);
87 void            SwapBytes(int16_t & value);
88 void            SwapBytes(int32_t & value);
89 void            SwapBytes(int64_t & value);
90
91 std::string &   TrimL(std::string & val);
92 std::string &   TrimR(std::string & val);
93 std::string &   Trim(std::string & val);
94
95 std::string     IconvString(const std::string & source, const std::string & from, const std::string & to);
96
97 //-----------------------------------------------------------------------------
98 template <typename varT>
99 int str2x(const std::string & str, varT & x)
100 {
101     int pos = 0;
102     int minus = 1;
103
104     if (str.empty())
105         return -1;
106
107     if (str[0] == '+')
108         pos++;
109
110     if (str[0] == '-')
111     {
112         pos++;
113         minus = -1;
114     }
115
116     if ((str[pos] < '0' || str[pos] > '9'))
117         return -1;
118
119     x = str[pos++] - '0';
120
121     for (unsigned i = pos; i < str.size(); i++)
122     {
123         if ((str[i] < '0' || str[i] > '9'))
124             return -1;
125
126         x *= 10;
127         x += str[i] - '0';
128     }
129
130     x*= minus;
131
132     return 0;
133 }
134 //-----------------------------------------------------------------------------
135 template <typename varT>
136 const std::string & x2str(varT x, std::string & s)
137 {
138     varT xx = x;
139     int pos = 1;
140
141     x /= 10;
142     while (x != 0)
143     {
144         x /= 10;
145         pos++;
146     }
147
148     if (xx < 0)
149     {
150         pos++;
151         s.resize(pos, 0);
152         s[0] = '-';
153     }
154     else if (xx > 0)
155     {
156         s.resize(pos, 0);
157     }
158     else
159     {
160         s.resize(1, 0);
161         s[0] = '0';
162         return s;
163     }
164
165     x = xx;
166
167     while (x != 0)
168     {
169         if (x < 0)
170             s[--pos] = -(x % 10) + '0';
171         else
172             s[--pos] = x % 10 + '0';
173
174         x /= 10;
175     }
176
177     return s;
178 }
179 //-----------------------------------------------------------------------------
180 template <typename varT>
181 const std::string & unsigned2str(varT x, std::string & s)
182 {
183     varT xx = x;
184     int pos = 1;
185
186     x /= 10;
187     while (x != 0)
188     {
189         x /= 10;
190         pos++;
191     }
192
193     if (xx > 0)
194     {
195         s.resize(pos, 0);
196     }
197     else
198     {
199         s.resize(1, 0);
200         s[0] = '0';
201         return s;
202     }
203
204     x = xx;
205
206     while (x != 0)
207     {
208         s[--pos] = x % 10 + '0';
209
210         x /= 10;
211     }
212
213     return s;
214 }
215 //-----------------------------------------------------------------------------
216 int str2x(const std::string & str, int & x);
217 int str2x(const std::string & str, unsigned & x);
218 int str2x(const std::string & str, long & x);
219 int str2x(const std::string & str, unsigned long & x);
220 int str2x(const std::string & str, long long & x);
221 int str2x(const std::string & str, unsigned long long & x);
222 //-----------------------------------------------------------------------------
223 const std::string & x2str(unsigned x, std::string & s);
224 const std::string & x2str(unsigned long x, std::string & s);
225 const std::string & x2str(unsigned long long x, std::string & s);
226 //-----------------------------------------------------------------------------
227 char * stg_strptime(const char *, const char *, struct tm *);
228 time_t stg_timegm(struct tm *);
229
230 #endif