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 "exceptions.hpp"
41 namespace impl
= tools::text
;
42 #define IMPL_NAME "tools::text"
45 impl::duplicate(const char* str
)
47 char* copy
= new char[std::strlen(str
) + 1];
48 std::strcpy(copy
, str
);
53 impl::match(const std::string
& str
, const std::string
& regex
)
57 // Special case: regcomp does not like empty regular expressions.
63 if (::regcomp(&preg
, regex
.c_str(), REG_EXTENDED
) != 0)
64 throw std::runtime_error("Invalid regular expression '" + regex
+
67 const int res
= ::regexec(&preg
, str
.c_str(), 0, NULL
, 0);
69 if (res
!= 0 && res
!= REG_NOMATCH
)
70 throw std::runtime_error("Invalid regular expression " + regex
);
79 impl::to_lower(const std::string
& str
)
82 for (std::string::const_iterator iter
= str
.begin(); iter
!= str
.end();
84 lc
+= std::tolower(*iter
);
88 std::vector
< std::string
>
89 impl::split(const std::string
& str
, const std::string
& delim
)
91 std::vector
< std::string
> words
;
93 std::string::size_type pos
= 0, newpos
= 0;
94 while (pos
< str
.length() && newpos
!= std::string::npos
) {
95 newpos
= str
.find(delim
, pos
);
97 words
.push_back(str
.substr(pos
, newpos
- pos
));
98 pos
= newpos
+ delim
.length();
105 impl::trim(const std::string
& str
)
107 std::string::size_type pos1
= str
.find_first_not_of(" \t");
108 std::string::size_type pos2
= str
.find_last_not_of(" \t");
110 if (pos1
== std::string::npos
&& pos2
== std::string::npos
)
112 else if (pos1
== std::string::npos
)
113 return str
.substr(0, str
.length() - pos2
);
114 else if (pos2
== std::string::npos
)
115 return str
.substr(pos1
);
117 return str
.substr(pos1
, pos2
- pos1
+ 1);
121 impl::to_bool(const std::string
& str
)
123 const std::string lower
= to_lower(str
);
124 if (lower
== "yes" || lower
== "true")
126 else if (lower
== "no" || lower
== "false")
129 // XXX Not really a libc error.
130 throw system_error(IMPL_NAME
"::to_bool", "Cannot convert string "
131 "'" + str
+ "' to boolean", EINVAL
);
136 impl::to_bytes(std::string str
)
139 throw std::runtime_error("Empty value");
141 const char unit
= str
[str
.length() - 1];
144 case 'k': case 'K': multiplier
= 1 << 10; break;
145 case 'm': case 'M': multiplier
= 1 << 20; break;
146 case 'g': case 'G': multiplier
= 1 << 30; break;
147 case 't': case 'T': multiplier
= int64_t(1) << 40; break;
149 if (!std::isdigit(unit
))
150 throw std::runtime_error(std::string("Unknown size unit '") + unit
155 str
.erase(str
.length() - 1);
157 return to_type
< int64_t >(str
) * multiplier
;