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