Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c++ / atffile.cpp
blob625c2d9c4fb78ae6c11acdcde1c03022197c8581
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 <fstream>
32 #include "atf-c++/atffile.hpp"
33 #include "atf-c++/exceptions.hpp"
34 #include "atf-c++/expand.hpp"
35 #include "atf-c++/formats.hpp"
37 // ------------------------------------------------------------------------
38 // The "reader" helper class.
39 // ------------------------------------------------------------------------
41 class reader : public atf::formats::atf_atffile_reader {
42 const atf::fs::directory& m_dir;
43 atf::tests::vars_map m_conf, m_props;
44 std::vector< std::string > m_tps;
46 void
47 got_tp(const std::string& name, bool isglob)
49 if (isglob) {
50 std::vector< std::string > ms =
51 atf::expand::expand_glob(name, m_dir.names());
52 // Cannot use m_tps.insert(iterator, begin, end) here because it
53 // does not work under Solaris.
54 for (std::vector< std::string >::const_iterator iter = ms.begin();
55 iter != ms.end(); iter++)
56 m_tps.push_back(*iter);
57 } else {
58 if (m_dir.find(name) == m_dir.end())
59 throw atf::not_found_error< atf::fs::path >
60 ("Cannot locate the " + name + " file",
61 atf::fs::path(name));
62 m_tps.push_back(name);
66 void
67 got_prop(const std::string& name, const std::string& val)
69 m_props[name] = val;
72 void
73 got_conf(const std::string& var, const std::string& val)
75 m_conf[var] = val;
78 public:
79 reader(std::istream& is, const atf::fs::directory& dir) :
80 atf::formats::atf_atffile_reader(is),
81 m_dir(dir)
85 const atf::tests::vars_map&
86 conf(void)
87 const
89 return m_conf;
92 const atf::tests::vars_map&
93 props(void)
94 const
96 return m_props;
99 const std::vector< std::string >&
100 tps(void)
101 const
103 return m_tps;
107 // ------------------------------------------------------------------------
108 // The "atffile" class.
109 // ------------------------------------------------------------------------
111 atf::atffile::atffile(const atf::fs::path& filename)
113 // Scan the directory where the atffile lives in to gather a list of
114 // all possible test programs in it.
115 fs::directory dir(filename.branch_path());
116 dir.erase(filename.leaf_name());
117 fs::directory::iterator iter = dir.begin();
118 while (iter != dir.end()) {
119 const std::string& name = (*iter).first;
120 const fs::file_info& fi = (*iter).second;
122 // Discard hidden files and non-executable ones so that they are
123 // not candidates for glob matching.
124 if (name[0] == '.' || (!fi.is_owner_executable() &&
125 !fi.is_group_executable()))
126 dir.erase(iter++);
127 else
128 iter++;
131 // Parse the atffile.
132 std::ifstream is(filename.c_str());
133 if (!is)
134 throw atf::not_found_error< fs::path >
135 ("Cannot open Atffile", filename);
136 reader r(is, dir);
137 r.read();
138 is.close();
140 // Update the atffile with the data accumulated in the reader.
141 m_conf = r.conf();
142 m_props = r.props();
143 m_tps = r.tps();
145 // Sanity checks.
146 if (m_props.find("test-suite") == m_props.end())
147 throw std::runtime_error("Undefined property `test-suite'");
150 const std::vector< std::string >&
151 atf::atffile::tps(void)
152 const
154 return m_tps;
157 const atf::tests::vars_map&
158 atf::atffile::conf(void)
159 const
161 return m_conf;
164 const atf::tests::vars_map&
165 atf::atffile::props(void)
166 const
168 return m_props;