Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c++ / text.cpp
blob226343d1f7690389d616e919b2c8a1a2f963ba01
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 #include <cctype>
31 #include <cstring>
33 extern "C" {
34 #include "atf-c/error.h"
35 #include "atf-c/text.h"
38 #include "atf-c++/exceptions.hpp"
39 #include "atf-c++/text.hpp"
41 namespace impl = atf::text;
42 #define IMPL_NAME "atf::text"
44 char*
45 impl::duplicate(const char* str)
47 char* copy = new char[std::strlen(str) + 1];
48 std::strcpy(copy, str);
49 return copy;
52 std::string
53 impl::to_lower(const std::string& str)
55 std::string lc;
56 for (std::string::const_iterator iter = str.begin(); iter != str.end();
57 iter++)
58 lc += std::tolower(*iter);
59 return lc;
62 std::vector< std::string >
63 impl::split(const std::string& str, const std::string& delim)
65 std::vector< std::string > words;
67 std::string::size_type pos = 0, newpos = 0;
68 while (pos < str.length() && newpos != std::string::npos) {
69 newpos = str.find(delim, pos);
70 if (newpos != pos)
71 words.push_back(str.substr(pos, newpos - pos));
72 pos = newpos + delim.length();
75 return words;
78 std::string
79 impl::trim(const std::string& str)
81 std::string::size_type pos1 = str.find_first_not_of(" \t");
82 std::string::size_type pos2 = str.find_last_not_of(" \t");
84 if (pos1 == std::string::npos && pos2 == std::string::npos)
85 return "";
86 else if (pos1 == std::string::npos)
87 return str.substr(0, str.length() - pos2);
88 else if (pos2 == std::string::npos)
89 return str.substr(pos1);
90 else
91 return str.substr(pos1, pos2 - pos1 + 1);
94 bool
95 impl::to_bool(const std::string& str)
97 bool b;
99 atf_error_t err = atf_text_to_bool(str.c_str(), &b);
100 if (atf_is_error(err))
101 throw_atf_error(err);
103 return b;