4 #include <boost/regex.hpp>
7 std::string
strip_CR(const std::string
& str
)
14 void istrip_CR(std::string
& str
)
17 size_t xl
= str
.length();
19 char y
= str
[xl
- crc
- 1];
20 if(y
!= '\r' && y
!= '\n')
24 str
= str
.substr(0, xl
- crc
);
27 int firstchar(const std::string
& str
)
30 return static_cast<unsigned char>(str
[0]);
35 int extract_token(std::string
& str
, std::string
& tok
, const char* sep
, bool sequence
) throw(std::bad_alloc
)
40 return (tok
== "") ? -2 : -1;
42 size_t s
= str
.find_first_of(sep
);
43 if(s
< str
.length()) {
44 int ech
= static_cast<unsigned char>(str
[s
]);
45 size_t t
= sequence
? min(str
.find_first_not_of(sep
, s
), str
.length()) : (s
+ 1);
46 tok
= str
.substr(0, s
);
55 int ech
= (s
< str
.length()) ? static_cast<unsigned char>(str
[s
]) : -1;
56 tok
= str
.substr(0, s
);
57 str
= str
.substr(s
+ 1);
61 int string_to_bool(const std::string
& x
)
64 for(size_t i
= 0; i
< y
.length(); i
++)
66 if(y
== "on" || y
== "true" || y
== "yes" || y
== "1" || y
== "enable" || y
== "enabled")
68 if(y
== "off" || y
== "false" || y
== "no" || y
== "0" || y
== "disable" || y
== "disabled")
73 regex_results::regex_results()
78 regex_results::regex_results(std::vector
<std::string
> res
)
84 regex_results::operator bool() const
89 bool regex_results::operator!() const
94 size_t regex_results::size() const
96 return results
.size();
98 const std::string
& regex_results::operator[](size_t i
) const
103 regex_results
regex(const std::string
& regexp
, const std::string
& str
, const char* ex
) throw(std::bad_alloc
,
106 static std::map
<std::string
, boost::regex
*> regexps
;
107 if(!regexps
.count(regexp
)) {
108 boost::regex
* y
= NULL
;
110 y
= new boost::regex(regexp
, boost::regex::extended
& ~boost::regex::collate
);
112 } catch(std::bad_alloc
& e
) {
115 } catch(std::exception
& e
) {
116 throw std::runtime_error(e
.what());
120 boost::smatch matches
;
121 bool x
= boost::regex_match(str
.begin(), str
.end(), matches
, *(regexps
[regexp
]));
123 std::vector
<std::string
> res
;
124 for(size_t i
= 0; i
< matches
.size(); i
++)
125 res
.push_back(matches
.str(i
));
126 return regex_results(res
);
128 throw std::runtime_error(ex
);
130 return regex_results();
133 bool regex_match(const std::string
& regexp
, const std::string
& str
) throw(std::bad_alloc
, std::runtime_error
)
135 return regex(regexp
, str
);