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 $
30 #include "stg/os_int.h"
31 #include "stg/const.h"
36 #include <climits> // NAME_MAX
38 #include <unistd.h> // uid_t, gid_t
39 #include <sys/stat.h> // mode_t
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 /*bool IsDigit(char c);
61 bool IsAlpha(char c);*/
62 int strtodouble2(const char * s, double &a);
63 int printfd(const char * __file__, const char * fmt, ...);
64 void Encode12(char * dst, const char * src, size_t srcLen);
65 void Decode21(char * dst, const char * src);
67 void Encode12str(std::string & dst, const std::string & src);
68 void Decode21str(std::string & dst, const std::string & src);
69 inline std::string Encode12str(const std::string & src) { std::string dst; Encode12str(dst, src); return dst; }
70 inline std::string Decode21str(const std::string & src) { std::string dst; Decode21str(dst, src); return dst; }
72 int ParseIPString(const char * str, uint32_t * ips, int maxIP);
73 void KOIToWin(const char * s1, char * s2, int l);
74 void WinToKOI(const char * s1, char * s2, int l);
75 void KOIToWin(const std::string & s1, std::string * s2);
76 void WinToKOI(const std::string & s1, std::string * s2);
77 int DaysInMonth(unsigned year, unsigned mon);
78 int DaysInCurrentMonth();
80 //char * inet_ntostr(unsigned long);
81 std::string inet_ntostring(uint32_t);
82 uint32_t inet_strington(const std::string & value);
83 int strprintf(std::string * str, const char * fmt, ...);
84 int ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2);
85 uint32_t CalcMask(uint32_t msk);
86 void TouchFile(const std::string & fileName);
88 void EncodeStr(char * str, unsigned long serial, int useHDD);
89 void DecodeStr(char * str, unsigned long serial, int useHDD);
91 void SwapBytes(uint16_t & value);
92 void SwapBytes(uint32_t & value);
93 void SwapBytes(uint64_t & value);
94 void SwapBytes(int16_t & value);
95 void SwapBytes(int32_t & value);
96 void SwapBytes(int64_t & value);
98 std::string & TrimL(std::string & val);
99 std::string & TrimR(std::string & val);
100 std::string & Trim(std::string & val);
102 std::string ToLower(std::string value);
103 std::string ToUpper(std::string value);
105 template <typename C, typename F>
106 C Split(const std::string & value, char delim, F conv)
110 size_t pos = value.find_first_of(delim);
111 while (pos != std::string::npos)
113 res.push_back(conv(value.substr(startPos, pos - startPos)));
115 pos = value.find_first_of(delim, pos + 1);
117 res.push_back(conv(value.substr(startPos, pos - startPos)));
121 template <typename T>
122 T FromString(const std::string & value)
125 std::istringstream stream(value);
130 template <typename C>
131 C Split(const std::string & value, char delim)
133 return Split<C>(value, delim, FromString);
136 std::string IconvString(const std::string & source, const std::string & from, const std::string & to);
138 int ParseInt(const std::string & str, int * val);
139 int ParseUnsigned(const std::string & str, unsigned * val);
140 int ParseIntInRange(const std::string & str, int min, int max, int * val);
141 int ParseUnsignedInRange(const std::string & str, unsigned min,
142 unsigned max, unsigned * val);
143 int ParseYesNo(const std::string & str, bool * val);
145 bool WaitPackets(int sd);
147 bool ReadAll(int sd, void * dest, size_t size);
148 bool WriteAll(int sd, const void * source, size_t size);
150 std::string ToPrintable(const std::string & src);
152 std::string formatTime(time_t value);
153 time_t readTime(const std::string & value);
154 //-----------------------------------------------------------------------------
155 int str2x(const std::string & str, int32_t & x);
156 int str2x(const std::string & str, uint32_t & x);
157 int str2x(const std::string & str, double & x);
159 int str2x(const std::string & str, int64_t & x);
160 int str2x(const std::string & str, uint64_t & x);
162 //-----------------------------------------------------------------------------
163 const std::string & x2str(uint32_t x, std::string & s);
164 const std::string & x2str(uint64_t x, std::string & s);
165 //-----------------------------------------------------------------------------
166 const std::string & x2str(double x, std::string & s);
167 //-----------------------------------------------------------------------------
169 template <typename varT>
170 int str2x(const std::string & str, varT & x);
171 template <typename varT>
172 const std::string & x2str(varT x, std::string & s);
173 template <typename varT>
174 std::string x2str(varT x) { std::string s; return x2str(x, s); }
175 template <typename varT>
176 const std::string & unsigned2str(varT x, std::string & s);
177 template <typename varT>
178 std::string unsigned2str(varT x) { std::string s; return unsigned2str(x, s); }
180 //-----------------------------------------------------------------------------
181 template <typename varT>
183 int str2x(const std::string & str, varT & x)
200 if ((str[pos] < '0' || str[pos] > '9'))
203 x = str[pos++] - '0';
205 for (unsigned i = pos; i < str.size(); i++)
207 if ((str[i] < '0' || str[i] > '9'))
218 //-----------------------------------------------------------------------------
219 template <typename varT>
221 const std::string & x2str(varT x, std::string & s)
255 s[--pos] = -(x % 10) + '0';
257 s[--pos] = x % 10 + '0';
264 //-----------------------------------------------------------------------------
265 template <typename varT>
267 const std::string & unsigned2str(varT x, std::string & s)
294 s[--pos] = x % 10 + '0';
301 //-----------------------------------------------------------------------------
302 char * stg_strptime(const char *, const char *, struct tm *);
303 time_t stg_timegm(struct tm *);
305 uid_t str2uid(const std::string& name);
306 gid_t str2gid(const std::string& name);
307 mode_t str2mode(const std::string& mode);