blocxx
EnvVars.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005 Novell, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * - Neither the name of Novell, Inc., nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc., OR THE
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
35 #ifndef BLOCXX_ENVVARS_HPP__
36 #define BLOCXX_ENVVARS_HPP__
37 
38 #include "blocxx/BLOCXX_config.h"
39 #include "blocxx/Map.hpp"
40 #include "blocxx/String.hpp"
41 #include "blocxx/Cstr.hpp"
42 
43 namespace BLOCXX_NAMESPACE
44 {
45 
49 class BLOCXX_COMMON_API EnvVars
50 {
51 public:
54 
56  {
58  E_EMPTY_ENVIRONMENT
59  };
60 
65  EnvVars(EEnvVarFlag flag=E_EMPTY_ENVIRONMENT);
82  template <class InputIterator>
83  inline EnvVars(EEnvVarFlag flag,
84  InputIterator first, InputIterator last)
85  : m_envMap()
86  , m_envp(0)
87  {
88  if( flag == E_CURRENT_ENVIRONMENT)
89  fillEnvMap(m_envMap);
90  for( ; first != last; ++first)
91  setVar(first->first.c_str(), first->second.c_str());
92  }
101  EnvVars(const char* const envp[]);
106  EnvVars(const EnvVars& arg);
110  ~EnvVars();
117  EnvVars& operator= (const EnvVars& arg);
127  const char* const* getenvp() const;
132  size_t size() const
133  {
134  return m_envMap.size();
135  }
146  String getValue(const String& key, const String& notFoundRetVal=String()) const;
147 
153  bool removeVar(const String& key);
154 
162  bool setVar(const String& keyValue);
163 
171  bool setVar(const String& key, const String& value);
172 
178  bool addVar(const String& name, const String& value);
179 
184  bool updateVar(const String& name, const String& value);
185 
190  const_iterator begin() const
191  {
192  return m_envMap.begin();
193  }
194 
199  const_iterator end() const
200  {
201  return m_envMap.end();
202  }
203 
204 private:
205  void deleteEnvp() const;
206  static void fillEnvMap(EnvMap& envMap);
207  static void fillEnvMap(const char* const envp[], EnvMap& envMap);
208 
210  mutable char** m_envp;
211 };
212 
213 namespace Cstr
214 {
215 
216 template <>
218 {
219  char const * const * sarr;
220 
221  CstrArr(EnvVars const & ev)
222  {
223  sarr = ev.getenvp();
224  }
225 };
226 
227 } // namespace Cstr
228 
229 
230 } // End of BLOCXX_NAMESPACE
231 
232 #endif // BLOCXX_ENVVARS_HPP__