xref: /netbsd-src/external/bsd/kyua-cli/dist/engine/context.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1*6b3a42afSjmmv // Copyright 2011 Google Inc.
2*6b3a42afSjmmv // All rights reserved.
3*6b3a42afSjmmv //
4*6b3a42afSjmmv // Redistribution and use in source and binary forms, with or without
5*6b3a42afSjmmv // modification, are permitted provided that the following conditions are
6*6b3a42afSjmmv // met:
7*6b3a42afSjmmv //
8*6b3a42afSjmmv // * Redistributions of source code must retain the above copyright
9*6b3a42afSjmmv //   notice, this list of conditions and the following disclaimer.
10*6b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright
11*6b3a42afSjmmv //   notice, this list of conditions and the following disclaimer in the
12*6b3a42afSjmmv //   documentation and/or other materials provided with the distribution.
13*6b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*6b3a42afSjmmv //   may be used to endorse or promote products derived from this software
15*6b3a42afSjmmv //   without specific prior written permission.
16*6b3a42afSjmmv //
17*6b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*6b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*6b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*6b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*6b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*6b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*6b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*6b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*6b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*6b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*6b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*6b3a42afSjmmv 
29*6b3a42afSjmmv #include "engine/context.hpp"
30*6b3a42afSjmmv 
31*6b3a42afSjmmv #include "utils/env.hpp"
32*6b3a42afSjmmv #include "utils/format/macros.hpp"
33*6b3a42afSjmmv #include "utils/fs/operations.hpp"
34*6b3a42afSjmmv #include "utils/text/operations.ipp"
35*6b3a42afSjmmv 
36*6b3a42afSjmmv namespace fs = utils::fs;
37*6b3a42afSjmmv namespace text = utils::text;
38*6b3a42afSjmmv 
39*6b3a42afSjmmv 
40*6b3a42afSjmmv /// Internal implementation of a context.
41*6b3a42afSjmmv struct engine::context::impl {
42*6b3a42afSjmmv     /// The current working directory.
43*6b3a42afSjmmv     fs::path _cwd;
44*6b3a42afSjmmv 
45*6b3a42afSjmmv     /// The environment variables.
46*6b3a42afSjmmv     std::map< std::string, std::string > _env;
47*6b3a42afSjmmv 
48*6b3a42afSjmmv     /// Constructor.
49*6b3a42afSjmmv     ///
50*6b3a42afSjmmv     /// \param cwd_ The current working directory.
51*6b3a42afSjmmv     /// \param env_ The environment variables.
implengine::context::impl52*6b3a42afSjmmv     impl(const fs::path& cwd_,
53*6b3a42afSjmmv          const std::map< std::string, std::string >& env_) :
54*6b3a42afSjmmv         _cwd(cwd_),
55*6b3a42afSjmmv         _env(env_)
56*6b3a42afSjmmv     {
57*6b3a42afSjmmv     }
58*6b3a42afSjmmv 
59*6b3a42afSjmmv     /// Equality comparator.
60*6b3a42afSjmmv     ///
61*6b3a42afSjmmv     /// \param other The object to compare to.
62*6b3a42afSjmmv     ///
63*6b3a42afSjmmv     /// \return True if the two objects are equal; false otherwise.
64*6b3a42afSjmmv     bool
operator ==engine::context::impl65*6b3a42afSjmmv     operator==(const impl& other) const
66*6b3a42afSjmmv     {
67*6b3a42afSjmmv         return _cwd == other._cwd && _env == other._env;
68*6b3a42afSjmmv     }
69*6b3a42afSjmmv };
70*6b3a42afSjmmv 
71*6b3a42afSjmmv 
72*6b3a42afSjmmv /// Constructs a new context.
73*6b3a42afSjmmv ///
74*6b3a42afSjmmv /// \param cwd_ The current working directory.
75*6b3a42afSjmmv /// \param env_ The environment variables.
context(const fs::path & cwd_,const std::map<std::string,std::string> & env_)76*6b3a42afSjmmv engine::context::context(const fs::path& cwd_,
77*6b3a42afSjmmv                          const std::map< std::string, std::string >& env_) :
78*6b3a42afSjmmv     _pimpl(new impl(cwd_, env_))
79*6b3a42afSjmmv {
80*6b3a42afSjmmv }
81*6b3a42afSjmmv 
82*6b3a42afSjmmv 
83*6b3a42afSjmmv /// Destructor.
~context(void)84*6b3a42afSjmmv engine::context::~context(void)
85*6b3a42afSjmmv {
86*6b3a42afSjmmv }
87*6b3a42afSjmmv 
88*6b3a42afSjmmv 
89*6b3a42afSjmmv /// Constructs a new context based on the current environment.
90*6b3a42afSjmmv engine::context
current(void)91*6b3a42afSjmmv engine::context::current(void)
92*6b3a42afSjmmv {
93*6b3a42afSjmmv     return context(fs::current_path(), utils::getallenv());
94*6b3a42afSjmmv }
95*6b3a42afSjmmv 
96*6b3a42afSjmmv 
97*6b3a42afSjmmv /// Returns the current working directory of the context.
98*6b3a42afSjmmv ///
99*6b3a42afSjmmv /// \return A path.
100*6b3a42afSjmmv const fs::path&
cwd(void) const101*6b3a42afSjmmv engine::context::cwd(void) const
102*6b3a42afSjmmv {
103*6b3a42afSjmmv     return _pimpl->_cwd;
104*6b3a42afSjmmv }
105*6b3a42afSjmmv 
106*6b3a42afSjmmv 
107*6b3a42afSjmmv /// Returns the environment variables of the context.
108*6b3a42afSjmmv ///
109*6b3a42afSjmmv /// \return A variable name to variable value mapping.
110*6b3a42afSjmmv const std::map< std::string, std::string >&
env(void) const111*6b3a42afSjmmv engine::context::env(void) const
112*6b3a42afSjmmv {
113*6b3a42afSjmmv     return _pimpl->_env;
114*6b3a42afSjmmv }
115*6b3a42afSjmmv 
116*6b3a42afSjmmv 
117*6b3a42afSjmmv /// Equality comparator.
118*6b3a42afSjmmv ///
119*6b3a42afSjmmv /// \param other The other object to compare this one to.
120*6b3a42afSjmmv ///
121*6b3a42afSjmmv /// \return True if this object and other are equal; false otherwise.
122*6b3a42afSjmmv bool
operator ==(const context & other) const123*6b3a42afSjmmv engine::context::operator==(const context& other) const
124*6b3a42afSjmmv {
125*6b3a42afSjmmv     return *_pimpl == *other._pimpl;
126*6b3a42afSjmmv }
127*6b3a42afSjmmv 
128*6b3a42afSjmmv 
129*6b3a42afSjmmv /// Inequality comparator.
130*6b3a42afSjmmv ///
131*6b3a42afSjmmv /// \param other The other object to compare this one to.
132*6b3a42afSjmmv ///
133*6b3a42afSjmmv /// \return True if this object and other are different; false otherwise.
134*6b3a42afSjmmv bool
operator !=(const context & other) const135*6b3a42afSjmmv engine::context::operator!=(const context& other) const
136*6b3a42afSjmmv {
137*6b3a42afSjmmv     return !(*this == other);
138*6b3a42afSjmmv }
139*6b3a42afSjmmv 
140*6b3a42afSjmmv 
141*6b3a42afSjmmv /// Injects the object into a stream.
142*6b3a42afSjmmv ///
143*6b3a42afSjmmv /// \param output The stream into which to inject the object.
144*6b3a42afSjmmv /// \param object The object to format.
145*6b3a42afSjmmv ///
146*6b3a42afSjmmv /// \return The output stream.
147*6b3a42afSjmmv std::ostream&
operator <<(std::ostream & output,const context & object)148*6b3a42afSjmmv engine::operator<<(std::ostream& output, const context& object)
149*6b3a42afSjmmv {
150*6b3a42afSjmmv     output << F("context{cwd=%s, env=[")
151*6b3a42afSjmmv         % text::quote(object.cwd().str(), '\'');
152*6b3a42afSjmmv 
153*6b3a42afSjmmv     const std::map< std::string, std::string >& env = object.env();
154*6b3a42afSjmmv     bool first = true;
155*6b3a42afSjmmv     for (std::map< std::string, std::string >::const_iterator
156*6b3a42afSjmmv              iter = env.begin(); iter != env.end(); ++iter) {
157*6b3a42afSjmmv         if (!first)
158*6b3a42afSjmmv             output << ", ";
159*6b3a42afSjmmv         first = false;
160*6b3a42afSjmmv 
161*6b3a42afSjmmv         output << F("%s=%s") % (*iter).first
162*6b3a42afSjmmv             % text::quote((*iter).second, '\'');
163*6b3a42afSjmmv     }
164*6b3a42afSjmmv 
165*6b3a42afSjmmv     output << "]}";
166*6b3a42afSjmmv     return output;
167*6b3a42afSjmmv }
168