1204591Sluigi// 2204591Sluigi// Automated Testing Framework (atf) 3204591Sluigi// 4204591Sluigi// Copyright (c) 2007 The NetBSD Foundation, Inc. 5204591Sluigi// All rights reserved. 6204591Sluigi// 7204591Sluigi// Redistribution and use in source and binary forms, with or without 8204591Sluigi// modification, are permitted provided that the following conditions 9204591Sluigi// are met: 10204591Sluigi// 1. Redistributions of source code must retain the above copyright 11204591Sluigi// notice, this list of conditions and the following disclaimer. 12204591Sluigi// 2. Redistributions in binary form must reproduce the above copyright 13204591Sluigi// notice, this list of conditions and the following disclaimer in the 14204591Sluigi// documentation and/or other materials provided with the distribution. 15204591Sluigi// 16204591Sluigi// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17204591Sluigi// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18204591Sluigi// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19204591Sluigi// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20204591Sluigi// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21204591Sluigi// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22204591Sluigi// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23204591Sluigi// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24204591Sluigi// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25204591Sluigi// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26204591Sluigi// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27204591Sluigi// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28204591Sluigi// 29204591Sluigi 30204591Sluigi#if !defined(_ATF_CXX_TEXT_HPP_) 31204591Sluigi#define _ATF_CXX_TEXT_HPP_ 32204591Sluigi 33204591Sluigiextern "C" { 34204591Sluigi#include <stdint.h> 35204591Sluigi} 36204591Sluigi 37204591Sluigi#include <sstream> 38204591Sluigi#include <stdexcept> 39204591Sluigi#include <string> 40204591Sluigi#include <vector> 41204591Sluigi 42204591Sluiginamespace atf { 43240494Sglebiusnamespace text { 44240494Sglebius 45240494Sglebius//! 46204591Sluigi//! \brief Duplicates a C string using the new[] allocator. 47204591Sluigi//! 48204591Sluigi//! Replaces the functionality of strdup by using the new[] allocator and 49204591Sluigi//! thus allowing the resulting memory to be managed by utils::auto_array. 50204591Sluigi//! 51204591Sluigichar* duplicate(const char*); 52204591Sluigi 53204591Sluigi//! 54204591Sluigi//! \brief Joins multiple words into a string. 55204591Sluigi//! 56204591Sluigi//! Joins a list of words into a string, separating them using the provided 57204591Sluigi//! separator. Empty words are not omitted. 58204591Sluigi//! 59204591Sluigitemplate< class T > 60204591Sluigistd::string 61204591Sluigijoin(const T& words, const std::string& separator) 62204591Sluigi{ 63204591Sluigi std::string str; 64204591Sluigi 65204591Sluigi typename T::const_iterator iter = words.begin(); 66204591Sluigi bool done = iter == words.end(); 67204591Sluigi while (!done) { 68204591Sluigi str += *iter; 69204591Sluigi iter++; 70204591Sluigi if (iter != words.end()) 71204591Sluigi str += separator; 72204591Sluigi else 73204591Sluigi done = true; 74204591Sluigi } 75204591Sluigi 76204591Sluigi return str; 77204591Sluigi} 78204591Sluigi 79204591Sluigi//! 80204591Sluigi//! \brief Checks if the string matches a regular expression. 81204591Sluigi//! 82204591Sluigibool match(const std::string&, const std::string&); 83204591Sluigi 84204591Sluigi//! 85204591Sluigi//! \brief Splits a string into words. 86204591Sluigi//! 87204591Sluigi//! Splits the given string into multiple words, all separated by the 88204591Sluigi//! given delimiter. Multiple occurrences of the same delimiter are 89204591Sluigi//! not condensed so that rejoining the words later on using the same 90204591Sluigi//! delimiter results in the original string. 91204591Sluigi//! 92204591Sluigistd::vector< std::string > split(const std::string&, const std::string&); 93204591Sluigi 94204591Sluigi//! 95204591Sluigi//! \brief Removes whitespace from the beginning and end of a string. 96204591Sluigi//! 97204591Sluigistd::string trim(const std::string&); 98204591Sluigi 99204591Sluigi//! 100204591Sluigi//! \brief Converts a string to a boolean value. 101204591Sluigi//! 102204591Sluigibool to_bool(const std::string&); 103204591Sluigi 104204591Sluigi//! 105204591Sluigi//! \brief Converts the given string to a bytes size. 106204591Sluigi//! 107204591Sluigiint64_t to_bytes(std::string); 108204591Sluigi 109204591Sluigi//! 110204591Sluigi//! \brief Changes the case of a string to lowercase. 111204591Sluigi//! 112204591Sluigi//! Returns a new string that is a lowercased version of the original 113204591Sluigi//! one. 114204591Sluigi//! 115204591Sluigistd::string to_lower(const std::string&); 116204591Sluigi 117204591Sluigi//! 118204591Sluigi//! \brief Converts the given object to a string. 119204591Sluigi//! 120204591Sluigi//! 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