forked from augcampos/asterisk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.cpp
More file actions
35 lines (28 loc) · 995 Bytes
/
StringUtils.cpp
File metadata and controls
35 lines (28 loc) · 995 Bytes
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
/*
* StringUtils.cpp
*
* Created on: Aug 23, 2011
* Author: augcampos
*/
#include "asteriskcpp/utils/StringUtils.h"
namespace asteriskcpp {
std::string search_and_replace(const std::string& str, const std::string &oldsubstr, const std::string &newsubstr) {
std::string::size_type startidx;
std::string tmp(str);
while ((startidx = tmp.find(oldsubstr)) != std::string::npos) {
tmp.replace(startidx, oldsubstr.size(), newsubstr);
}
return (tmp);
}
time_t stringToTime(const std::string& str, const std::string& format) {
struct tm result;
strptime(str.c_str(), format.c_str(), &result);
return (mktime(&result));
}
bool stringToBool(const std::string& str) {
return ((str.compare("true") == 0 || str.compare("yes") == 0));
}
std::string str2Log(const std::string& str) {
return (search_and_replace(search_and_replace(str, "\r", "\\r"), "\n", "\\n"));
}
}