Rev 976 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 976 | Rev 981 | ||
|---|---|---|---|
| Line 19... | Line 19... | ||
| 19 | * along with this program; if not, write to the * |
19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
22 | ***************************************************************************/ |
| 23 | #include "tools.h" |
23 | #include "tools.h" |
| - | 24 | ||
| - | 25 | string niceTime() |
|
| - | 26 | { |
|
| - | 27 | string result; |
|
| - | 28 | struct tm tmStruct; |
|
| - | 29 | time_t t = time(NULL); |
|
| - | 30 | gmtime_r(&t, &tmStruct); |
|
| - | 31 | ||
| - | 32 | result += lpad(itos(tmStruct.tm_hour), 2, '0'); |
|
| - | 33 | result += ":"; |
|
| - | 34 | result += lpad(itos(tmStruct.tm_min), 2, '0'); |
|
| - | 35 | result += ":"; |
|
| - | 36 | result += lpad(itos(tmStruct.tm_sec), 2, '0'); |
|
| - | 37 | ||
| - | 38 | return result; |
|
| - | 39 | } |
|
| 24 | 40 | ||
| 25 | unsigned int bin2uint(string bin) |
41 | unsigned int bin2uint(string bin) |
| 26 | { |
42 | { |
| 27 | unsigned int num = 0; |
43 | unsigned int num = 0; |
| 28 | 44 | ||
| Line 75... | Line 91... | ||
| 75 | } |
91 | } |
| 76 | 92 | ||
| 77 | string bin2hex(string bin) |
93 | string bin2hex(string bin) |
| 78 | { |
94 | { |
| 79 | string hex; |
95 | string hex; |
| 80 | 96 | ||
| 81 | while (bin.size() > 0) |
97 | while (bin.size() > 0) |
| 82 | { |
98 | { |
| 83 | string part = bin.substr(0, 4); |
99 | string part = bin.substr(0, 4); |
| 84 | bin.erase(0, part.size()); |
100 | bin.erase(0, part.size()); |
| 85 | 101 | ||
| Line 122... | Line 138... | ||
| 122 | 138 | ||
| 123 | return hex; |
139 | return hex; |
| 124 | } |
140 | } |
| 125 | 141 | ||
| 126 | string float2bin(float num, int length) |
142 | string float2bin(float num, int length) |
| 127 | { |
143 | { |
| 128 | string bin; |
144 | string bin; |
| 129 | 145 | ||
| 130 | ///FIXME |
146 | ///FIXME |
| 131 | 147 | ||
| 132 | while (bin.size() < length) |
148 | while (bin.size() < length) |
| 133 | bin = "0" + bin; |
149 | bin = "0" + bin; |
| 134 | 150 | ||
| 135 | return bin; |
151 | return bin; |
| 136 | } |
152 | } |
| 137 | 153 | ||
| 138 | string hex2bin(string hex) |
154 | string hex2bin(string hex) |
| 139 | { |
155 | { |
| 140 | hex = strtolower(hex); |
156 | hex = strtolower(hex); |
| 141 | 157 | ||
| Line 160... | Line 176... | ||
| 160 | case 'c': bin += "1100"; break; |
176 | case 'c': bin += "1100"; break; |
| 161 | case 'd': bin += "1101"; break; |
177 | case 'd': bin += "1101"; break; |
| 162 | case 'e': bin += "1110"; break; |
178 | case 'e': bin += "1110"; break; |
| 163 | case 'f': bin += "1111"; break; |
179 | case 'f': bin += "1111"; break; |
| 164 | } |
180 | } |
| 165 | } |
181 | } |
| 166 | 182 | ||
| 167 | return bin; |
183 | return bin; |
| 168 | } |
184 | } |
| 169 | 185 | ||
| 170 | string uint2bin(unsigned int num, int length) |
186 | string uint2bin(unsigned int num, int length) |
| 171 | { |
187 | { |
| 172 | string bin; |
188 | string bin; |
| 173 | 189 | ||
| 174 | while (num) |
190 | while (num) |
| 175 | { |
191 | { |
| 176 | bin = char((num&1)+'0') + bin; |
192 | bin = char((num&1)+'0') + bin; |
| 177 | num >>= 1; |
193 | num >>= 1; |
| 178 | } |
194 | } |
| 179 | 195 | ||
| 180 | while (bin.size() < length) |
196 | while (bin.size() < length) |
| 181 | bin = "0" + bin; |
197 | bin = "0" + bin; |
| 182 | 198 | ||
| 183 | return bin; |
199 | return bin; |
| 184 | } |
200 | } |
| 185 | 201 | ||
| - | 202 | string uint2hex(unsigned int num, int length) |
|
| - | 203 | { |
|
| - | 204 | return bin2hex(uint2bin(num, length)); |
|
| - | 205 | } |
|
| 186 | 206 | ||
| 187 | vector<string> explode(string delimiter, string str) |
207 | vector<string> explode(string delimiter, string str) |
| 188 | { |
208 | { |
| 189 | vector<string> parts; |
209 | vector<string> parts; |
| 190 | string::size_type startPos = 0; |
210 | string::size_type startPos = 0; |
| 191 | string::size_type endPos = 0; |
211 | string::size_type endPos = 0; |
| 192 | 212 | ||
| 193 | while ((endPos = str.find(delimiter, startPos)) != string::npos) |
213 | while ((endPos = str.find(delimiter, startPos)) != string::npos) |
| 194 | { |
214 | { |
| 195 | parts.push_back(str.substr(startPos, endPos-startPos)); |
215 | parts.push_back(str.substr(startPos, endPos-startPos)); |
| 196 | startPos = endPos+delimiter.length(); |
216 | startPos = endPos+delimiter.length(); |
| 197 | } |
217 | } |
| 198 | 218 | ||
| 199 | if (startPos < str.length()) |
219 | if (startPos < str.length()) |
| 200 | { |
220 | { |
| 201 | parts.push_back(str.substr(startPos, str.length()-startPos)); |
221 | parts.push_back(str.substr(startPos, str.length()-startPos)); |
| 202 | } |
222 | } |
| 203 | 223 | ||
| 204 | return parts; |
224 | return parts; |
| 205 | } |
225 | } |
| 206 | 226 | ||
| 207 | string strtoupper(string s) |
227 | string strtoupper(string s) |
| 208 | { |
228 | { |
| 209 | for (unsigned int n = 0; n < s.size(); n++) |
229 | for (unsigned int n = 0; n < s.size(); n++) |
| 210 | s[n] = (char)toupper(s[n]); |
230 | s[n] = (char)toupper(s[n]); |
| 211 | 231 | ||
| 212 | return s; |
232 | return s; |
| 213 | } |
233 | } |
| 214 | 234 | ||
| 215 | string strtolower(string s) |
235 | string strtolower(string s) |
| 216 | { |
236 | { |
| 217 | for (unsigned int n = 0; n < s.size(); n++) |
237 | for (unsigned int n = 0; n < s.size(); n++) |
| 218 | s[n] = (char)tolower(s[n]); |
238 | s[n] = (char)tolower(s[n]); |
| 219 | 239 | ||
| 220 | return s; |
240 | return s; |
| 221 | } |
241 | } |
| 222 | 242 | ||
| 223 | string trim(string s) |
243 | string trim(string s) |
| 224 | { |
244 | { |
| 225 | return trim(s, ' '); |
245 | return trim(s, ' '); |
| 226 | } |
246 | } |
| 227 | 247 | ||
| 228 | string trim(string s, char c) |
248 | string trim(string s, char c) |
| 229 | { |
249 | { |
| 230 | while (s[0] == c) |
250 | while (s[0] == c) |
| 231 | s.erase(0, 1); |
251 | s.erase(0, 1); |
| 232 | 252 | ||
| 233 | while (s[s.length()-1] == c) |
253 | while (s[s.length()-1] == c) |
| 234 | s.erase(s.length()-1, 1); |
254 | s.erase(s.length()-1, 1); |
| 235 | 255 | ||
| 236 | return s; |
256 | return s; |
| 237 | } |
257 | } |
| 238 | 258 | ||
| 239 | int stoi(const string s) |
259 | int stoi(const string s) |
| 240 | { |
260 | { |
| 241 | istringstream in(s); |
261 | istringstream in(s); |
| 242 | int i; |
262 | int i; |
| - | 263 | in >> i; |
|
| - | 264 | return i; |
|
| - | 265 | } |
|
| - | 266 | ||
| - | 267 | string itos(int i) |
|
| - | 268 | { |
|
| - | 269 | ostringstream out; |
|
| - | 270 | out << i; |
|
| - | 271 | return out.str(); |
|
| - | 272 | } |
|
| - | 273 | ||
| - | 274 | unsigned int stou(const string s) |
|
| - | 275 | { |
|
| - | 276 | istringstream in(s); |
|
| - | 277 | unsigned int i; |
|
| 243 | in >> i; |
278 | in >> i; |
| 244 | return i; |
279 | return i; |
| 245 | } |
280 | } |
| 246 | 281 | ||
| 247 | string |
282 | string utos(unsigned int i) |
| 248 | { |
283 | { |
| 249 | ostringstream out; |
284 | ostringstream out; |
| 250 | out << i; |
285 | out << i; |
| 251 | return out.str(); |
286 | return out.str(); |
| 252 | } |
287 | } |