blocxx

EnvVars.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Novell, Inc., nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc., OR THE 
00022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00027 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00028 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00035 #ifndef BLOCXX_ENVVARS_HPP__
00036 #define BLOCXX_ENVVARS_HPP__
00037 
00038 #include "blocxx/BLOCXX_config.h"
00039 #include "blocxx/Map.hpp"
00040 #include "blocxx/String.hpp"
00041 #include "blocxx/Cstr.hpp"
00042 
00043 namespace BLOCXX_NAMESPACE
00044 {
00045 
00049 class BLOCXX_COMMON_API EnvVars
00050 {
00051 public:
00052    typedef Map<String, String>   EnvMap;
00053    typedef EnvMap::const_iterator   const_iterator;
00054 
00055    enum EEnvVarFlag
00056    {
00057       E_CURRENT_ENVIRONMENT,
00058       E_EMPTY_ENVIRONMENT
00059    };
00060 
00065    EnvVars(EEnvVarFlag flag=E_EMPTY_ENVIRONMENT);
00082    template <class InputIterator>
00083    inline EnvVars(EEnvVarFlag flag,
00084                   InputIterator first, InputIterator last)
00085       : m_envMap()
00086       , m_envp(0)
00087    {
00088       if( flag == E_CURRENT_ENVIRONMENT)
00089          fillEnvMap(m_envMap);
00090       for( ; first != last; ++first)
00091          setVar(first->first.c_str(), first->second.c_str());
00092    }
00101    EnvVars(const char* const envp[]);
00106    EnvVars(const EnvVars& arg);
00110    ~EnvVars();
00117    EnvVars& operator= (const EnvVars& arg);
00127    const char* const* getenvp() const;
00132    size_t size() const
00133    {
00134       return m_envMap.size();
00135    }
00146    String getValue(const String& key, const String& notFoundRetVal=String()) const;
00147 
00153    bool removeVar(const String& key);
00154 
00162    bool setVar(const String& keyValue);
00163 
00171    bool setVar(const String& key, const String& value);
00172 
00178    bool addVar(const String& name, const String& value);
00179 
00184    bool updateVar(const String& name, const String& value);
00185 
00190    const_iterator begin() const
00191    {
00192       return m_envMap.begin();
00193    }
00194 
00199    const_iterator end() const
00200    {
00201       return m_envMap.end();
00202    }
00203 
00204 private:
00205    void deleteEnvp() const;
00206    static void fillEnvMap(EnvMap& envMap);
00207    static void fillEnvMap(const char* const envp[], EnvMap& envMap);
00208 
00209    EnvMap m_envMap;
00210    mutable char** m_envp;
00211 };
00212 
00213 namespace Cstr
00214 {
00215 
00216 template <>
00217 struct CstrArr<EnvVars>
00218 {
00219    char const * const * sarr;
00220 
00221    CstrArr(EnvVars const & ev)
00222    {
00223       sarr = ev.getenvp();
00224    }
00225 };
00226 
00227 } // namespace Cstr
00228 
00229 
00230 }  // End of BLOCXX_NAMESPACE
00231 
00232 #endif   // BLOCXX_ENVVARS_HPP__