1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc // documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc extern "C" {
31*0a6a1f1dSLionel Sambuc #include <regex.h>
32*0a6a1f1dSLionel Sambuc }
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc #include <cctype>
35*0a6a1f1dSLionel Sambuc #include <cerrno>
36*0a6a1f1dSLionel Sambuc #include <cstring>
37*0a6a1f1dSLionel Sambuc
38*0a6a1f1dSLionel Sambuc #include "exceptions.hpp"
39*0a6a1f1dSLionel Sambuc #include "text.hpp"
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel Sambuc namespace impl = tools::text;
42*0a6a1f1dSLionel Sambuc #define IMPL_NAME "tools::text"
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc char*
duplicate(const char * str)45*0a6a1f1dSLionel Sambuc impl::duplicate(const char* str)
46*0a6a1f1dSLionel Sambuc {
47*0a6a1f1dSLionel Sambuc char* copy = new char[std::strlen(str) + 1];
48*0a6a1f1dSLionel Sambuc std::strcpy(copy, str);
49*0a6a1f1dSLionel Sambuc return copy;
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc bool
match(const std::string & str,const std::string & regex)53*0a6a1f1dSLionel Sambuc impl::match(const std::string& str, const std::string& regex)
54*0a6a1f1dSLionel Sambuc {
55*0a6a1f1dSLionel Sambuc bool found;
56*0a6a1f1dSLionel Sambuc
57*0a6a1f1dSLionel Sambuc // Special case: regcomp does not like empty regular expressions.
58*0a6a1f1dSLionel Sambuc if (regex.empty()) {
59*0a6a1f1dSLionel Sambuc found = str.empty();
60*0a6a1f1dSLionel Sambuc } else {
61*0a6a1f1dSLionel Sambuc ::regex_t preg;
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc if (::regcomp(&preg, regex.c_str(), REG_EXTENDED) != 0)
64*0a6a1f1dSLionel Sambuc throw std::runtime_error("Invalid regular expression '" + regex +
65*0a6a1f1dSLionel Sambuc "'");
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc const int res = ::regexec(&preg, str.c_str(), 0, NULL, 0);
68*0a6a1f1dSLionel Sambuc regfree(&preg);
69*0a6a1f1dSLionel Sambuc if (res != 0 && res != REG_NOMATCH)
70*0a6a1f1dSLionel Sambuc throw std::runtime_error("Invalid regular expression " + regex);
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc found = res == 0;
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc return found;
76*0a6a1f1dSLionel Sambuc }
77*0a6a1f1dSLionel Sambuc
78*0a6a1f1dSLionel Sambuc std::string
to_lower(const std::string & str)79*0a6a1f1dSLionel Sambuc impl::to_lower(const std::string& str)
80*0a6a1f1dSLionel Sambuc {
81*0a6a1f1dSLionel Sambuc std::string lc;
82*0a6a1f1dSLionel Sambuc for (std::string::const_iterator iter = str.begin(); iter != str.end();
83*0a6a1f1dSLionel Sambuc iter++)
84*0a6a1f1dSLionel Sambuc lc += std::tolower(*iter);
85*0a6a1f1dSLionel Sambuc return lc;
86*0a6a1f1dSLionel Sambuc }
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc std::vector< std::string >
split(const std::string & str,const std::string & delim)89*0a6a1f1dSLionel Sambuc impl::split(const std::string& str, const std::string& delim)
90*0a6a1f1dSLionel Sambuc {
91*0a6a1f1dSLionel Sambuc std::vector< std::string > words;
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc std::string::size_type pos = 0, newpos = 0;
94*0a6a1f1dSLionel Sambuc while (pos < str.length() && newpos != std::string::npos) {
95*0a6a1f1dSLionel Sambuc newpos = str.find(delim, pos);
96*0a6a1f1dSLionel Sambuc if (newpos != pos)
97*0a6a1f1dSLionel Sambuc words.push_back(str.substr(pos, newpos - pos));
98*0a6a1f1dSLionel Sambuc pos = newpos + delim.length();
99*0a6a1f1dSLionel Sambuc }
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc return words;
102*0a6a1f1dSLionel Sambuc }
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel Sambuc std::string
trim(const std::string & str)105*0a6a1f1dSLionel Sambuc impl::trim(const std::string& str)
106*0a6a1f1dSLionel Sambuc {
107*0a6a1f1dSLionel Sambuc std::string::size_type pos1 = str.find_first_not_of(" \t");
108*0a6a1f1dSLionel Sambuc std::string::size_type pos2 = str.find_last_not_of(" \t");
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc if (pos1 == std::string::npos && pos2 == std::string::npos)
111*0a6a1f1dSLionel Sambuc return "";
112*0a6a1f1dSLionel Sambuc else if (pos1 == std::string::npos)
113*0a6a1f1dSLionel Sambuc return str.substr(0, str.length() - pos2);
114*0a6a1f1dSLionel Sambuc else if (pos2 == std::string::npos)
115*0a6a1f1dSLionel Sambuc return str.substr(pos1);
116*0a6a1f1dSLionel Sambuc else
117*0a6a1f1dSLionel Sambuc return str.substr(pos1, pos2 - pos1 + 1);
118*0a6a1f1dSLionel Sambuc }
119*0a6a1f1dSLionel Sambuc
120*0a6a1f1dSLionel Sambuc bool
to_bool(const std::string & str)121*0a6a1f1dSLionel Sambuc impl::to_bool(const std::string& str)
122*0a6a1f1dSLionel Sambuc {
123*0a6a1f1dSLionel Sambuc const std::string lower = to_lower(str);
124*0a6a1f1dSLionel Sambuc if (lower == "yes" || lower == "true")
125*0a6a1f1dSLionel Sambuc return true;
126*0a6a1f1dSLionel Sambuc else if (lower == "no" || lower == "false")
127*0a6a1f1dSLionel Sambuc return false;
128*0a6a1f1dSLionel Sambuc else {
129*0a6a1f1dSLionel Sambuc // XXX Not really a libc error.
130*0a6a1f1dSLionel Sambuc throw system_error(IMPL_NAME "::to_bool", "Cannot convert string "
131*0a6a1f1dSLionel Sambuc "'" + str + "' to boolean", EINVAL);
132*0a6a1f1dSLionel Sambuc }
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc
135*0a6a1f1dSLionel Sambuc int64_t
to_bytes(std::string str)136*0a6a1f1dSLionel Sambuc impl::to_bytes(std::string str)
137*0a6a1f1dSLionel Sambuc {
138*0a6a1f1dSLionel Sambuc if (str.empty())
139*0a6a1f1dSLionel Sambuc throw std::runtime_error("Empty value");
140*0a6a1f1dSLionel Sambuc
141*0a6a1f1dSLionel Sambuc const char unit = str[str.length() - 1];
142*0a6a1f1dSLionel Sambuc int64_t multiplier;
143*0a6a1f1dSLionel Sambuc switch (unit) {
144*0a6a1f1dSLionel Sambuc case 'k': case 'K': multiplier = 1 << 10; break;
145*0a6a1f1dSLionel Sambuc case 'm': case 'M': multiplier = 1 << 20; break;
146*0a6a1f1dSLionel Sambuc case 'g': case 'G': multiplier = 1 << 30; break;
147*0a6a1f1dSLionel Sambuc case 't': case 'T': multiplier = int64_t(1) << 40; break;
148*0a6a1f1dSLionel Sambuc default:
149*0a6a1f1dSLionel Sambuc if (!std::isdigit(unit))
150*0a6a1f1dSLionel Sambuc throw std::runtime_error(std::string("Unknown size unit '") + unit
151*0a6a1f1dSLionel Sambuc + "'");
152*0a6a1f1dSLionel Sambuc multiplier = 1;
153*0a6a1f1dSLionel Sambuc }
154*0a6a1f1dSLionel Sambuc if (multiplier != 1)
155*0a6a1f1dSLionel Sambuc str.erase(str.length() - 1);
156*0a6a1f1dSLionel Sambuc
157*0a6a1f1dSLionel Sambuc return to_type< int64_t >(str) * multiplier;
158*0a6a1f1dSLionel Sambuc }
159