xref: /src/contrib/kyua/model/context.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
108334c51SBrooks Davis // Copyright 2011 The Kyua Authors.
208334c51SBrooks Davis // All rights reserved.
308334c51SBrooks Davis //
408334c51SBrooks Davis // Redistribution and use in source and binary forms, with or without
508334c51SBrooks Davis // modification, are permitted provided that the following conditions are
608334c51SBrooks Davis // met:
708334c51SBrooks Davis //
808334c51SBrooks Davis // * Redistributions of source code must retain the above copyright
908334c51SBrooks Davis //   notice, this list of conditions and the following disclaimer.
1008334c51SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
1108334c51SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
1208334c51SBrooks Davis //   documentation and/or other materials provided with the distribution.
1308334c51SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
1408334c51SBrooks Davis //   may be used to endorse or promote products derived from this software
1508334c51SBrooks Davis //   without specific prior written permission.
1608334c51SBrooks Davis //
1708334c51SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1808334c51SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1908334c51SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2008334c51SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2108334c51SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2208334c51SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2308334c51SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2408334c51SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2508334c51SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2608334c51SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2708334c51SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2808334c51SBrooks Davis 
2908334c51SBrooks Davis #include "model/context.hpp"
3008334c51SBrooks Davis 
3108334c51SBrooks Davis #include "utils/format/macros.hpp"
3208334c51SBrooks Davis #include "utils/fs/path.hpp"
3308334c51SBrooks Davis #include "utils/noncopyable.hpp"
3408334c51SBrooks Davis #include "utils/text/operations.ipp"
3508334c51SBrooks Davis 
3608334c51SBrooks Davis namespace fs = utils::fs;
3708334c51SBrooks Davis namespace text = utils::text;
3808334c51SBrooks Davis 
3908334c51SBrooks Davis 
4008334c51SBrooks Davis /// Internal implementation of a context.
4108334c51SBrooks Davis struct model::context::impl : utils::noncopyable {
4208334c51SBrooks Davis     /// The current working directory.
4308334c51SBrooks Davis     fs::path _cwd;
4408334c51SBrooks Davis 
4508334c51SBrooks Davis     /// The environment variables.
4608334c51SBrooks Davis     std::map< std::string, std::string > _env;
4708334c51SBrooks Davis 
4808334c51SBrooks Davis     /// Constructor.
4908334c51SBrooks Davis     ///
5008334c51SBrooks Davis     /// \param cwd_ The current working directory.
5108334c51SBrooks Davis     /// \param env_ The environment variables.
implmodel::context::impl5208334c51SBrooks Davis     impl(const fs::path& cwd_,
5308334c51SBrooks Davis          const std::map< std::string, std::string >& env_) :
5408334c51SBrooks Davis         _cwd(cwd_),
5508334c51SBrooks Davis         _env(env_)
5608334c51SBrooks Davis     {
5708334c51SBrooks Davis     }
5808334c51SBrooks Davis 
5908334c51SBrooks Davis     /// Equality comparator.
6008334c51SBrooks Davis     ///
6108334c51SBrooks Davis     /// \param other The object to compare to.
6208334c51SBrooks Davis     ///
6308334c51SBrooks Davis     /// \return True if the two objects are equal; false otherwise.
6408334c51SBrooks Davis     bool
operator ==model::context::impl6508334c51SBrooks Davis     operator==(const impl& other) const
6608334c51SBrooks Davis     {
6708334c51SBrooks Davis         return _cwd == other._cwd && _env == other._env;
6808334c51SBrooks Davis     }
6908334c51SBrooks Davis };
7008334c51SBrooks Davis 
7108334c51SBrooks Davis 
7208334c51SBrooks Davis /// Constructs a new context.
7308334c51SBrooks Davis ///
7408334c51SBrooks Davis /// \param cwd_ The current working directory.
7508334c51SBrooks Davis /// \param env_ The environment variables.
context(const fs::path & cwd_,const std::map<std::string,std::string> & env_)7608334c51SBrooks Davis model::context::context(const fs::path& cwd_,
7708334c51SBrooks Davis                          const std::map< std::string, std::string >& env_) :
7808334c51SBrooks Davis     _pimpl(new impl(cwd_, env_))
7908334c51SBrooks Davis {
8008334c51SBrooks Davis }
8108334c51SBrooks Davis 
8208334c51SBrooks Davis 
8308334c51SBrooks Davis /// Destructor.
~context(void)8408334c51SBrooks Davis model::context::~context(void)
8508334c51SBrooks Davis {
8608334c51SBrooks Davis }
8708334c51SBrooks Davis 
8808334c51SBrooks Davis 
8908334c51SBrooks Davis /// Returns the current working directory of the context.
9008334c51SBrooks Davis ///
9108334c51SBrooks Davis /// \return A path.
9208334c51SBrooks Davis const fs::path&
cwd(void) const9308334c51SBrooks Davis model::context::cwd(void) const
9408334c51SBrooks Davis {
9508334c51SBrooks Davis     return _pimpl->_cwd;
9608334c51SBrooks Davis }
9708334c51SBrooks Davis 
9808334c51SBrooks Davis 
9908334c51SBrooks Davis /// Returns the environment variables of the context.
10008334c51SBrooks Davis ///
10108334c51SBrooks Davis /// \return A variable name to variable value mapping.
10208334c51SBrooks Davis const std::map< std::string, std::string >&
env(void) const10308334c51SBrooks Davis model::context::env(void) const
10408334c51SBrooks Davis {
10508334c51SBrooks Davis     return _pimpl->_env;
10608334c51SBrooks Davis }
10708334c51SBrooks Davis 
10808334c51SBrooks Davis 
10908334c51SBrooks Davis /// Equality comparator.
11008334c51SBrooks Davis ///
11108334c51SBrooks Davis /// \param other The other object to compare this one to.
11208334c51SBrooks Davis ///
11308334c51SBrooks Davis /// \return True if this object and other are equal; false otherwise.
11408334c51SBrooks Davis bool
operator ==(const context & other) const11508334c51SBrooks Davis model::context::operator==(const context& other) const
11608334c51SBrooks Davis {
11708334c51SBrooks Davis     return *_pimpl == *other._pimpl;
11808334c51SBrooks Davis }
11908334c51SBrooks Davis 
12008334c51SBrooks Davis 
12108334c51SBrooks Davis /// Inequality comparator.
12208334c51SBrooks Davis ///
12308334c51SBrooks Davis /// \param other The other object to compare this one to.
12408334c51SBrooks Davis ///
12508334c51SBrooks Davis /// \return True if this object and other are different; false otherwise.
12608334c51SBrooks Davis bool
operator !=(const context & other) const12708334c51SBrooks Davis model::context::operator!=(const context& other) const
12808334c51SBrooks Davis {
12908334c51SBrooks Davis     return !(*this == other);
13008334c51SBrooks Davis }
13108334c51SBrooks Davis 
13208334c51SBrooks Davis 
13308334c51SBrooks Davis /// Injects the object into a stream.
13408334c51SBrooks Davis ///
13508334c51SBrooks Davis /// \param output The stream into which to inject the object.
13608334c51SBrooks Davis /// \param object The object to format.
13708334c51SBrooks Davis ///
13808334c51SBrooks Davis /// \return The output stream.
13908334c51SBrooks Davis std::ostream&
operator <<(std::ostream & output,const context & object)14008334c51SBrooks Davis model::operator<<(std::ostream& output, const context& object)
14108334c51SBrooks Davis {
14208334c51SBrooks Davis     output << F("context{cwd=%s, env=[")
14308334c51SBrooks Davis         % text::quote(object.cwd().str(), '\'');
14408334c51SBrooks Davis 
14508334c51SBrooks Davis     const std::map< std::string, std::string >& env = object.env();
14608334c51SBrooks Davis     bool first = true;
14708334c51SBrooks Davis     for (std::map< std::string, std::string >::const_iterator
14808334c51SBrooks Davis              iter = env.begin(); iter != env.end(); ++iter) {
14908334c51SBrooks Davis         if (!first)
15008334c51SBrooks Davis             output << ", ";
15108334c51SBrooks Davis         first = false;
15208334c51SBrooks Davis 
15308334c51SBrooks Davis         output << F("%s=%s") % (*iter).first
15408334c51SBrooks Davis             % text::quote((*iter).second, '\'');
15508334c51SBrooks Davis     }
15608334c51SBrooks Davis 
15708334c51SBrooks Davis     output << "]}";
15808334c51SBrooks Davis     return output;
15908334c51SBrooks Davis }
160