1240116Smarcel//
2240116Smarcel// Automated Testing Framework (atf)
3240116Smarcel//
4240116Smarcel// Copyright (c) 2007 The NetBSD Foundation, Inc.
5240116Smarcel// All rights reserved.
6240116Smarcel//
7240116Smarcel// Redistribution and use in source and binary forms, with or without
8240116Smarcel// modification, are permitted provided that the following conditions
9240116Smarcel// are met:
10240116Smarcel// 1. Redistributions of source code must retain the above copyright
11240116Smarcel//    notice, this list of conditions and the following disclaimer.
12240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel//    notice, this list of conditions and the following disclaimer in the
14240116Smarcel//    documentation and/or other materials provided with the distribution.
15240116Smarcel//
16240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel//
29240116Smarcel
30240116Smarcel#if !defined(_ATF_CXX_TEXT_HPP_)
31240116Smarcel#define _ATF_CXX_TEXT_HPP_
32240116Smarcel
33240116Smarcelextern "C" {
34240116Smarcel#include <stdint.h>
35240116Smarcel}
36240116Smarcel
37240116Smarcel#include <sstream>
38240116Smarcel#include <stdexcept>
39240116Smarcel#include <string>
40240116Smarcel#include <vector>
41240116Smarcel
42240116Smarcelnamespace atf {
43240116Smarcelnamespace text {
44240116Smarcel
45240116Smarcel//!
46240116Smarcel//! \brief Duplicates a C string using the new[] allocator.
47240116Smarcel//!
48240116Smarcel//! Replaces the functionality of strdup by using the new[] allocator and
49240116Smarcel//! thus allowing the resulting memory to be managed by utils::auto_array.
50240116Smarcel//!
51240116Smarcelchar* duplicate(const char*);
52240116Smarcel
53240116Smarcel//!
54240116Smarcel//! \brief Joins multiple words into a string.
55240116Smarcel//!
56240116Smarcel//! Joins a list of words into a string, separating them using the provided
57240116Smarcel//! separator.  Empty words are not omitted.
58240116Smarcel//!
59240116Smarceltemplate< class T >
60240116Smarcelstd::string
61240116Smarceljoin(const T& words, const std::string& separator)
62240116Smarcel{
63240116Smarcel    std::string str;
64240116Smarcel
65240116Smarcel    typename T::const_iterator iter = words.begin();
66240116Smarcel    bool done = iter == words.end();
67240116Smarcel    while (!done) {
68240116Smarcel        str += *iter;
69240116Smarcel        iter++;
70240116Smarcel        if (iter != words.end())
71240116Smarcel            str += separator;
72240116Smarcel        else
73240116Smarcel            done = true;
74240116Smarcel    }
75240116Smarcel
76240116Smarcel    return str;
77240116Smarcel}
78240116Smarcel
79240116Smarcel//!
80240116Smarcel//! \brief Checks if the string matches a regular expression.
81240116Smarcel//!
82240116Smarcelbool match(const std::string&, const std::string&);
83240116Smarcel
84240116Smarcel//!
85240116Smarcel//! \brief Splits a string into words.
86240116Smarcel//!
87240116Smarcel//! Splits the given string into multiple words, all separated by the
88240116Smarcel//! given delimiter.  Multiple occurrences of the same delimiter are
89240116Smarcel//! not condensed so that rejoining the words later on using the same
90240116Smarcel//! delimiter results in the original string.
91240116Smarcel//!
92240116Smarcelstd::vector< std::string > split(const std::string&, const std::string&);
93240116Smarcel
94240116Smarcel//!
95240116Smarcel//! \brief Removes whitespace from the beginning and end of a string.
96240116Smarcel//!
97240116Smarcelstd::string trim(const std::string&);
98240116Smarcel
99240116Smarcel//!
100240116Smarcel//! \brief Converts a string to a boolean value.
101240116Smarcel//!
102240116Smarcelbool to_bool(const std::string&);
103240116Smarcel
104240116Smarcel//!
105240116Smarcel//! \brief Converts the given string to a bytes size.
106240116Smarcel//!
107240116Smarcelint64_t to_bytes(std::string);
108240116Smarcel
109240116Smarcel//!
110240116Smarcel//! \brief Changes the case of a string to lowercase.
111240116Smarcel//!
112240116Smarcel//! Returns a new string that is a lowercased version of the original
113240116Smarcel//! one.
114240116Smarcel//!
115240116Smarcelstd::string to_lower(const std::string&);
116240116Smarcel
117240116Smarcel//!
118240116Smarcel//! \brief Converts the given object to a string.
119240116Smarcel//!
120240116Smarcel//! Returns a string with the representation of the given object.  There
121240116Smarcel//! must exist an operator<< method for that object.
122240116Smarcel//!
123240116Smarceltemplate< class T >
124240116Smarcelstd::string
125240116Smarcelto_string(const T& ob)
126240116Smarcel{
127240116Smarcel    std::ostringstream ss;
128240116Smarcel    ss << ob;
129240116Smarcel    return ss.str();
130240116Smarcel}
131240116Smarcel
132240116Smarcel//!
133240116Smarcel//! \brief Converts the given string to another type.
134240116Smarcel//!
135240116Smarcel//! Attempts to convert the given string to the requested type.  Throws
136240116Smarcel//! an exception if the conversion failed.
137240116Smarcel//!
138240116Smarceltemplate< class T >
139240116SmarcelT
140240116Smarcelto_type(const std::string& str)
141240116Smarcel{
142240116Smarcel    std::istringstream ss(str);
143240116Smarcel    T value;
144240116Smarcel    ss >> value;
145240116Smarcel    if (!ss.eof() || (ss.eof() && (ss.fail() || ss.bad())))
146240116Smarcel        throw std::runtime_error("Cannot convert string to requested type");
147240116Smarcel    return value;
148240116Smarcel}
149240116Smarcel
150240116Smarcel} // namespace text
151240116Smarcel} // namespace atf
152240116Smarcel
153240116Smarcel#endif // !defined(_ATF_CXX_TEXT_HPP_)
154