tools.h
8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* CC/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __TOOLS_H_
#define __TOOLS_H_
#include <string>
#include <vector>
#include "common.h"
class ivr_tools_t {
public:
/**
* @brief 获取timespec结构体
*
* @param [in/out] ts : struct timespec*
* @param [in/out] milliSeconds : int32_t
* @return int32_t
* @retval
* @see
* @note
* @author zhangyue
* @date 2012/04/27 16:39:17
**/
static int32_t get_abs_timespec(struct timespec* ts, int32_t milliSeconds);
/**
* @brief 获取当前时间戳,毫秒
*
* @return uint64_t
* @retval
* @see
* @note
* @author zhangyue
* @date 2012/04/24 14:23:36
**/
static uint64_t get_timestamp_ms();
/**
* @brief 判断 ip地址 并转换
*
* @param [in] src : string&
* @param [out] uint32_t : string&
* @return bool
* @retval
* @see
* @note
* @author zhangyue
*/
static bool check_ip(const char* src, uint32_t& result);
/**
* @brief 判断 port 并转换
*
* @param [in/out] str : string&
* @return bool
* @retval
* @see
* @note
* @author zhangyue
*/
static bool check_port(const char* src, uint32_t& result);
/**
* @brief 判断 正整数字符串(不以零开头的) 范围[1-2147483647]
*
* @param [in/out] str : string&
* @return bool
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/25 15:43:57
**/
static bool is_positive_integer(std::string& str);
/**
* @brief 判断 非负数字符串 范围[0-2147483647]
*
* @param [in/out] str : string&
* @return bool
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/25 15:44:01
**/
static bool is_nonnegative_integer(std::string& str);
/**
* @brief 判断 整数 范围[-2147483647 ~ 2147483647]
*
* @param [in/out] str : string&
* @return bool
* @retval
* @see
* @note
* @author zhangyue
* @date 2012/02/20 15:43:57
**/
static bool is_integer(std::string& str);
/**
* @brief 判断 字符串中仅包含数字
*
* @param [in/out] str : string&
* @return bool
* @retval
* @see
* @note
* @author zhangyue
* @date 2012/02/19 15:43:57
**/
static bool is_all_digit(std::string& str);
/**
* @brief 拆分(指定分隔符)字符串
*
* @param [in/out] src : const string&
* @param [in/out] split_str : const string&
* @param [in/out] dest : vector<string>&
* @return int32_t
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/25 15:44:05
**/
static bool split_string(const std::string& src,
const std::string& separator, std::vector<std::string>& dest);
/**
* @brief 返回1970到现在的秒数
*
* @return string
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/25 15:44:10
**/
static std::string get_current_second();
/**
* @brief 将时间格式化为 "%Y-%m-%d %H:%M:%S"
*
* @param [in/out] second : string&
* @return string
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/25 15:44:32
**/
static std::string format_datatime_str(std::string& second);
/**
* @brief 字符串转换为double
*
* @param [in/out] str : const std::string&
* @param [in/out] value : double&
* @return bool
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/24 17:33:15
**/
static bool str2double(const std::string& str, double& value);
/**
* @brief 去掉字符串前后的空格
*
* @param [in/out] str : std::string&
* @return void
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/25 15:48:08
**/
static void trim(std::string& str);
/**
* @brief 读取ini文件中的数组集合用到
* 读取同一prefix引导的section,将索引信息保存到vec中
*
* @param [in/out] prefix : const std::string
* @param [in/out] sec : const section_map_t&
* @param [in/out] vec : std::vector<uint32_t>
* @return void
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/26 17:28:47
**/
static void idx2vec(const char* prefix,
const section_map_t& sec, std::vector<uint32_t>& vec);
/**
* @brief 读取ini文件中的数组集合用到
*
* @param [in/out] prefix : const char*
* @param [in/out] sec : const key_map_t&
* @param [in/out] vec : std::vector<uint32_t>&
* @return void
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/26 20:02:26
**/
static void idx2vec(const char* prefix,
const key_map_t& sec, std::vector<uint32_t>& vec);
/**
* @brief 判断std::string和const char*是否相同
*
* @param [in/out] str : const std::string&
* @param [in/out] sz : const char*
* @return bool
* @retval
* @see
* @note 不区分大小写
* @author chenyuzhen
* @date 2011/08/28 14:40:11
**/
static bool str_eq(const std::string& str, const char* sz);
/**
* @brief 判断目录是否存在,如果不存在则创建
*
* @param [in/out] dirname : const char*
* @return void
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/29 20:09:13
**/
static bool check_and_create_dir(const char* dirname);
/**
* @brief 删除字符串中的所有空格
*
* @param [in/out] exp : const std::string&
* @return std::string
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/09/02 10:56:41
**/
static std::string delete_blank(const std::string& exp);
/**
* @brief 将map数值转换为字符串
*
* @param [in/out] std::map<std::string : const
* @param [in/out] val : std::string>
* @return std::string
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/09/02 17:39:24
**/
static std::string map2str(const std::map<std::string, std::string>& val);
static void build_vars(const std::map<std::string, variable_t>& temp,
std::map<std::string, variable_t>& out);
static void destroy_vars(std::map<std::string, variable_t>& vars);
static std::string chlname2no(const std::string& src);
static bool chlname2no(const std::string& src, std::string& dest);
static const char* inet_ntop(uint32_t src, char* dest, uint32_t dest_len);
/**
* @brief 支持中断的sleep
*
* @param [in/out] s : uint32_t
* @return void
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/09/16 12:36:39
**/
static void safe_sleeps(uint32_t s);
/**
* @brief sleep ms
*
* @param [in/out] ms : uint32_t
* @return void
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/10/20 12:09:59
**/
static void safe_sleepms(uint32_t ms);
// return "-h ip -p port"
static std::string get_address(uint32_t ip);
static std::string get_callsource(uint32_t ip, uint32_t port);
static std::string get_callsource(const std::string& ip, uint32_t port);
private:
/**
* @brief 从sting中判断prefix
*
* @param [in/out] prefix : const char*
* @param [in/out] sec : const std::vector<std::string>
* @param [in/out] vec : std::vector<uint32_t>&
* @return void
* @retval
* @see
* @note
* @author chenyuzhen
* @date 2011/08/26 20:03:58
**/
static void idx2vec_imp(const char* prefix,
const std::vector<std::string>& sec, std::vector<uint32_t>& vec);
};
class string_build {
public:
string_build() {
}
private:
string_build(const string_build&);
string_build& operator=(const string_build&);
string_build& operator+(const string_build&);
string_build& operator+=(const string_build&);
public:
string str() const {
return _oss.str();
}
public:
template<typename _T> string_build& operator +(const _T& v) {
_oss << v;
return *this;
}
template<typename _T> string_build& operator +=(const _T& v) {
_oss << v;
return *this;
}
private:
std::ostringstream _oss;
};
#endif //__TOOLS_H_
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */