Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c++ / text.hpp
blobd1fae19b70f03615868762e1e44c66e1e4450ace
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008 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.
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.
30 #if !defined(_ATF_CXX_TEXT_HPP_)
31 #define _ATF_CXX_TEXT_HPP_
33 #include <sstream>
34 #include <stdexcept>
35 #include <string>
36 #include <vector>
38 namespace atf {
39 namespace text {
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 //!
47 char* duplicate(const char*);
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 //!
55 template< class T >
56 std::string
57 join(const T& words, const std::string& separator)
59 std::string str;
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;
72 return str;
75 //!
76 //! \brief Splits a string into words.
77 //!
78 //! Splits the given string into multiple words, all separated by the
79 //! given delimiter. Multiple occurrences of the same delimiter are
80 //! not condensed so that rejoining the words later on using the same
81 //! delimiter results in the original string.
82 //!
83 std::vector< std::string > split(const std::string&, const std::string&);
85 //!
86 //! \brief Removes whitespace from the beginning and end of a string.
87 //!
88 std::string trim(const std::string&);
90 //!
91 //! \brief Converts a string to a boolean value.
92 //!
93 bool to_bool(const std::string&);
95 //!
96 //! \brief Changes the case of a string to lowercase.
97 //!
98 //! Returns a new string that is a lowercased version of the original
99 //! one.
101 std::string to_lower(const std::string&);
104 //! \brief Converts the given object to a string.
106 //! Returns a string with the representation of the given object. There
107 //! must exist an operator<< method for that object.
109 template< class T >
110 std::string
111 to_string(const T& ob)
113 std::ostringstream ss;
114 ss << ob;
115 return ss.str();
119 //! \brief Converts the given string to another type.
121 //! Attempts to convert the given string to the requested type. Throws
122 //! an exception if the conversion failed.
124 template< class T >
126 to_type(const std::string& str)
128 std::istringstream ss(str);
129 T value;
130 ss >> value;
131 if (!ss.eof() || (!ss.eof() && !ss.good()))
132 throw std::runtime_error("Cannot convert string to requested type");
133 return value;
136 } // namespace text
137 } // namespace atf
139 #endif // !defined(_ATF_CXX_TEXT_HPP_)