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