1 // 2 // Automated Testing Framework (atf) 3 // 4 // Copyright (c) 2007 The NetBSD Foundation, Inc. 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions 9 // are met: 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. 15 // 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. 28 // 29 30 extern "C" { 31 #include <regex.h> 32 } 33 34 #include <cctype> 35 #include <cstring> 36 37 extern "C" { 38 #include "../../atf-c/error.h" 39 40 #include "../../atf-c/detail/text.h" 41 } 42 43 #include "exceptions.hpp" 44 #include "text.hpp" 45 46 namespace impl = atf::text; 47 #define IMPL_NAME "atf::text" 48 49 char* 50 impl::duplicate(const char* str) 51 { 52 char* copy = new char[std::strlen(str) + 1]; 53 std::strcpy(copy, str); 54 return copy; 55 } 56 57 bool 58 impl::match(const std::string& str, const std::string& regex) 59 { 60 bool found; 61 62 // Special case: regcomp does not like empty regular expressions. 63 if (regex.empty()) { 64 found = str.empty(); 65 } else { 66 ::regex_t preg; 67 68 if (::regcomp(&preg, regex.c_str(), REG_EXTENDED) != 0) 69 throw std::runtime_error("Invalid regular expression '" + regex + 70 "'"); 71 72 const int res = ::regexec(&preg, str.c_str(), 0, NULL, 0); 73 regfree(&preg); 74 if (res != 0 && res != REG_NOMATCH) 75 throw std::runtime_error("Invalid regular expression " + regex); 76 77 found = res == 0; 78 } 79 80 return found; 81 } 82 83 std::string 84 impl::to_lower(const std::string& str) 85 { 86 std::string lc; 87 for (std::string::const_iterator iter = str.begin(); iter != str.end(); 88 iter++) 89 lc += std::tolower(*iter); 90 return lc; 91 } 92 93 std::vector< std::string > 94 impl::split(const std::string& str, const std::string& delim) 95 { 96 std::vector< std::string > words; 97 98 std::string::size_type pos = 0, newpos = 0; 99 while (pos < str.length() && newpos != std::string::npos) { 100 newpos = str.find(delim, pos); 101 if (newpos != pos) 102 words.push_back(str.substr(pos, newpos - pos)); 103 pos = newpos + delim.length(); 104 } 105 106 return words; 107 } 108 109 std::string 110 impl::trim(const std::string& str) 111 { 112 std::string::size_type pos1 = str.find_first_not_of(" \t"); 113 std::string::size_type pos2 = str.find_last_not_of(" \t"); 114 115 if (pos1 == std::string::npos && pos2 == std::string::npos) 116 return ""; 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); 121 else 122 return str.substr(pos1, pos2 - pos1 + 1); 123 } 124 125 bool 126 impl::to_bool(const std::string& str) 127 { 128 bool b; 129 130 atf_error_t err = atf_text_to_bool(str.c_str(), &b); 131 if (atf_is_error(err)) 132 throw_atf_error(err); 133 134 return b; 135 } 136 137 int64_t 138 impl::to_bytes(std::string str) 139 { 140 if (str.empty()) 141 throw std::runtime_error("Empty value"); 142 143 const char unit = str[str.length() - 1]; 144 int64_t multiplier; 145 switch (unit) { 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; 150 default: 151 if (!std::isdigit(unit)) 152 throw std::runtime_error(std::string("Unknown size unit '") + unit 153 + "'"); 154 multiplier = 1; 155 } 156 if (multiplier != 1) 157 str.erase(str.length() - 1); 158 159 return to_type< int64_t >(str) * multiplier; 160 } 161