1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "atf-c++/detail/text.hpp"
36 #include "atf-c/detail/text.h"
37 #include "atf-c/error.h"
40 #include "atf-c++/detail/exceptions.hpp"
42 namespace impl = atf::text;
43 #define IMPL_NAME "atf::text"
46 impl::duplicate(const char* str)
48 char* copy = new char[std::strlen(str) + 1];
49 std::strcpy(copy, str);
54 impl::match(const std::string& str, const std::string& regex)
58 // Special case: regcomp does not like empty regular expressions.
64 if (::regcomp(&preg, regex.c_str(), REG_EXTENDED) != 0)
65 throw std::runtime_error("Invalid regular expression '" + regex +
68 const int res = ::regexec(&preg, str.c_str(), 0, NULL, 0);
70 if (res != 0 && res != REG_NOMATCH)
71 throw std::runtime_error("Invalid regular expression " + regex);
80 impl::to_lower(const std::string& str)
83 for (std::string::const_iterator iter = str.begin(); iter != str.end();
85 lc += std::tolower(*iter);
89 std::vector< std::string >
90 impl::split(const std::string& str, const std::string& delim)
92 std::vector< std::string > words;
94 std::string::size_type pos = 0, newpos = 0;
95 while (pos < str.length() && newpos != std::string::npos) {
96 newpos = str.find(delim, pos);
98 words.push_back(str.substr(pos, newpos - pos));
99 pos = newpos + delim.length();
106 impl::trim(const std::string& str)
108 std::string::size_type pos1 = str.find_first_not_of(" \t");
109 std::string::size_type pos2 = str.find_last_not_of(" \t");
111 if (pos1 == std::string::npos && pos2 == std::string::npos)
113 else if (pos1 == std::string::npos)
114 return str.substr(0, str.length() - pos2);
115 else if (pos2 == std::string::npos)
116 return str.substr(pos1);
118 return str.substr(pos1, pos2 - pos1 + 1);
122 impl::to_bool(const std::string& str)
126 atf_error_t err = atf_text_to_bool(str.c_str(), &b);
127 if (atf_is_error(err))
128 throw_atf_error(err);
134 impl::to_bytes(std::string str)
137 throw std::runtime_error("Empty value");
139 const char unit = str[str.length() - 1];
142 case 'k': case 'K': multiplier = 1 << 10; break;
143 case 'm': case 'M': multiplier = 1 << 20; break;
144 case 'g': case 'G': multiplier = 1 << 30; break;
145 case 't': case 'T': multiplier = int64_t(1) << 40; break;
147 if (!std::isdigit(unit))
148 throw std::runtime_error(std::string("Unknown size unit '") + unit
153 str.erase(str.length() - 1);
155 return to_type< int64_t >(str) * multiplier;