1// 2// Automated Testing Framework (atf) 3// 4// Copyright (c) 2007, 2008, 2010 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#if !defined(_ATF_CXX_TEXT_HPP_) 31#define _ATF_CXX_TEXT_HPP_ 32 33#include <sstream> 34#include <stdexcept> 35#include <string> 36#include <vector> 37 38namespace atf { 39namespace text { 40 41//! 42//! \brief Duplicates a C string using the new[] allocator. 43//! 44//! Replaces the functionality of strdup by using the new[] allocator and 45//! thus allowing the resulting memory to be managed by utils::auto_array. 46//! 47char* duplicate(const char*); 48 49//! 50//! \brief Joins multiple words into a string. 51//! 52//! Joins a list of words into a string, separating them using the provided 53//! separator. Empty words are not omitted. 54//! 55template< class T > 56std::string 57join(const T& words, const std::string& separator) 58{ 59 std::string str; 60 61 typename T::const_iterator iter = words.begin(); 62 bool done = iter == words.end(); 63 while (!done) { 64 str += *iter; 65 iter++; 66 if (iter != words.end()) 67 str += separator; 68 else 69 done = true; 70 } 71 72 return str; 73} 74 75//! 76//! \brief Checks if the string matches a regular expression. 77//! 78bool match(const std::string&, const std::string&); 79 80//! 81//! \brief Splits a string into words. 82//! 83//! Splits the given string into multiple words, all separated by the 84//! given delimiter. Multiple occurrences of the same delimiter are 85//! not condensed so that rejoining the words later on using the same 86//! delimiter results in the original string. 87//! 88std::vector< std::string > split(const std::string&, const std::string&); 89 90//! 91//! \brief Removes whitespace from the beginning and end of a string. 92//! 93std::string trim(const std::string&); 94 95//! 96//! \brief Converts a string to a boolean value. 97//! 98bool to_bool(const std::string&); 99 100//! 101//! \brief Changes the case of a string to lowercase. 102//! 103//! Returns a new string that is a lowercased version of the original 104//! one. 105//! 106std::string to_lower(const std::string&); 107 108//! 109//! \brief Converts the given object to a string. 110//! 111//! Returns a string with the representation of the given object. There 112//! must exist an operator<< method for that object. 113//! 114template< class T > 115std::string 116to_string(const T& ob) 117{ 118 std::ostringstream ss; 119 ss << ob; 120 return ss.str(); 121} 122 123//! 124//! \brief Converts the given string to another type. 125//! 126//! Attempts to convert the given string to the requested type. Throws 127//! an exception if the conversion failed. 128//! 129template< class T > 130T 131to_type(const std::string& str) 132{ 133 std::istringstream ss(str); 134 T value; 135 ss >> value; 136 if (!ss.eof() || (!ss.eof() && !ss.good())) 137 throw std::runtime_error("Cannot convert string to requested type"); 138 return value; 139} 140 141} // namespace text 142} // namespace atf 143 144#endif // !defined(_ATF_CXX_TEXT_HPP_) 145