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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/11/08 10:11:19 $
38 #include "stg/os_int.h"
39 #include "stg/const.h"
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)
48 #define FN_STR_LEN (NAME_MAX)
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);
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);
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();
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);
87 void EncodeStr(char * str, unsigned long serial, int useHDD);
88 void DecodeStr(char * str, unsigned long serial, int useHDD);
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);
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);
102 std::string ToLower(std::string value);
103 std::string ToUpper(std::string value);
105 template <typename C, typename F>
107 C Split(const std::string & value, char delim, F conv)
111 size_t pos = value.find_first_of(delim);
112 while (pos != std::string::npos)
114 res.push_back(conv(value.substr(startPos, pos - startPos)));
116 pos = value.find_first_of(delim, pos + 1);
118 res.push_back(conv(value.substr(startPos, pos - startPos)));
122 template <typename T>
124 T FromString(const std::string & value)
127 std::istringstream stream(value);
134 std::string FromString<std::string>(const std::string & value)
139 template <typename C>
141 C Split(const std::string & value, char delim)
143 return Split<C>(value, delim, FromString<typename C::value_type>);
146 std::string IconvString(const std::string & source, const std::string & from, const std::string & to);
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);
155 bool WaitPackets(int sd);
157 bool ReadAll(int sd, void * dest, size_t size);
158 bool WriteAll(int sd, const void * source, size_t size);
160 std::string ToPrintable(const std::string & src);
162 //-----------------------------------------------------------------------------
163 int str2x(const std::string & str, int32_t & x);
164 int str2x(const std::string & str, uint32_t & x);
166 int str2x(const std::string & str, double & x) { return strtodouble2(str.c_str(), x); }
168 int str2x(const std::string & str, int64_t & x);
169 int str2x(const std::string & str, uint64_t & x);
171 //-----------------------------------------------------------------------------
172 const std::string & x2str(uint32_t x, std::string & s);
173 const std::string & x2str(uint64_t x, std::string & s);
174 //-----------------------------------------------------------------------------
175 const std::string & x2str(double x, std::string & s);
176 //-----------------------------------------------------------------------------
178 template <typename varT>
179 int str2x(const std::string & str, varT & x);
180 template <typename varT>
181 const std::string & x2str(varT x, std::string & s);
182 template <typename varT>
183 std::string x2str(varT x) { std::string s; return x2str(x, s); }
184 template <typename varT>
185 const std::string & unsigned2str(varT x, std::string & s);
186 template <typename varT>
187 std::string unsigned2str(varT x) { std::string s; return unsigned2str(x, s); }
189 //-----------------------------------------------------------------------------
190 template <typename varT>
192 int str2x(const std::string & str, varT & x)
209 if ((str[pos] < '0' || str[pos] > '9'))
212 x = str[pos++] - '0';
214 for (unsigned i = pos; i < str.size(); i++)
216 if ((str[i] < '0' || str[i] > '9'))
227 //-----------------------------------------------------------------------------
228 template <typename varT>
230 const std::string & x2str(varT x, std::string & s)
264 s[--pos] = -(x % 10) + '0';
266 s[--pos] = x % 10 + '0';
273 //-----------------------------------------------------------------------------
274 template <typename varT>
276 const std::string & unsigned2str(varT x, std::string & s)
303 s[--pos] = x % 10 + '0';
310 //-----------------------------------------------------------------------------
311 char * stg_strptime(const char *, const char *, struct tm *);
312 time_t stg_timegm(struct tm *);