2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "../../atf-c/error.h"
40 #include "../../atf-c/detail/text.h"
43 #include "exceptions.hpp"
46 namespace impl
= atf::text
;
47 #define IMPL_NAME "atf::text"
50 impl::duplicate(const char* str
)
52 char* copy
= new char[std::strlen(str
) + 1];
53 std::strcpy(copy
, str
);
58 impl::match(const std::string
& str
, const std::string
& regex
)
62 // Special case: regcomp does not like empty regular expressions.
68 if (::regcomp(&preg
, regex
.c_str(), REG_EXTENDED
) != 0)
69 throw std::runtime_error("Invalid regular expression '" + regex
+
72 const int res
= ::regexec(&preg
, str
.c_str(), 0, NULL
, 0);
74 if (res
!= 0 && res
!= REG_NOMATCH
)
75 throw std::runtime_error("Invalid regular expression " + regex
);
84 impl::to_lower(const std::string
& str
)
87 for (std::string::const_iterator iter
= str
.begin(); iter
!= str
.end();
89 lc
+= std::tolower(*iter
);
93 std::vector
< std::string
>
94 impl::split(const std::string
& str
, const std::string
& delim
)
96 std::vector
< std::string
> words
;
98 std::string::size_type pos
= 0, newpos
= 0;
99 while (pos
< str
.length() && newpos
!= std::string::npos
) {
100 newpos
= str
.find(delim
, pos
);
102 words
.push_back(str
.substr(pos
, newpos
- pos
));
103 pos
= newpos
+ delim
.length();
110 impl::trim(const std::string
& str
)
112 std::string::size_type pos1
= str
.find_first_not_of(" \t");
113 std::string::size_type pos2
= str
.find_last_not_of(" \t");
115 if (pos1
== std::string::npos
&& pos2
== std::string::npos
)
117 else if (pos1
== std::string::npos
)
118 return str
.substr(0, str
.length() - pos2
);
119 else if (pos2
== std::string::npos
)
120 return str
.substr(pos1
);
122 return str
.substr(pos1
, pos2
- pos1
+ 1);
126 impl::to_bool(const std::string
& str
)
130 atf_error_t err
= atf_text_to_bool(str
.c_str(), &b
);
131 if (atf_is_error(err
))
132 throw_atf_error(err
);
138 impl::to_bytes(std::string str
)
141 throw std::runtime_error("Empty value");
143 const char unit
= str
[str
.length() - 1];
146 case 'k': case 'K': multiplier
= 1 << 10; break;
147 case 'm': case 'M': multiplier
= 1 << 20; break;
148 case 'g': case 'G': multiplier
= 1 << 30; break;
149 case 't': case 'T': multiplier
= int64_t(1) << 40; break;
151 if (!std::isdigit(unit
))
152 throw std::runtime_error(std::string("Unknown size unit '") + unit
157 str
.erase(str
.length() - 1);
159 return to_type
< int64_t >(str
) * multiplier
;