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 
00042 namespace BLOCXX_NAMESPACE
00043 {
00044 
00048 class BLOCXX_COMMON_API EnvVars
00049 {
00050 public:
00051    typedef Map<String, String>   EnvMap;
00052    typedef EnvMap::const_iterator   const_iterator;
00053 
00054    enum EEnvVarFlag
00055    {
00056       E_CURRENT_ENVIRONMENT,
00057       E_EMPTY_ENVIRONMENT
00058    };
00059 
00064    EnvVars(EEnvVarFlag flag=E_EMPTY_ENVIRONMENT);
00065 
00082    template <class InputIterator>
00083    EnvVars(EEnvVarFlag flag, InputIterator first, InputIterator last);
00084 
00092    EnvVars(const char* const envp[]);
00097    EnvVars(const EnvVars& arg);
00101    ~EnvVars();
00108    EnvVars& operator= (const EnvVars& arg);
00116    const char* const* getenvp() const;
00121    size_t size() const
00122    {
00123       return m_envMap.size();
00124    }
00134    String getValue(const String& key, const String& notFoundRetVal=String()) const;
00135 
00141    bool removeVar(const String& key);
00142 
00150    bool setVar(const String& keyValue);
00151 
00159    bool setVar(const String& key, const String& value);
00160 
00166    bool addVar(const String& name, const String& value);
00167 
00172    bool updateVar(const String& name, const String& value);
00173 
00178    const_iterator begin() const
00179    {
00180       return m_envMap.begin();
00181    }
00182 
00187    const_iterator end() const
00188    {
00189       return m_envMap.end();
00190    }
00191 
00192 private:
00193    void deleteEnvp() const;
00194    static void fillEnvMap(EnvMap& envMap);
00195    static void fillEnvMap(const char* const envp[], EnvMap& envMap);
00196 
00197    EnvMap m_envMap;
00198    mutable char** m_envp;
00199 };
00200 
00201 template <class InputIterator>
00202 inline EnvVars::EnvVars(EnvVars::EEnvVarFlag flag,
00203                         InputIterator first, InputIterator last)
00204    : m_envMap()
00205    , m_envp(0)
00206 {
00207    if( flag == E_CURRENT_ENVIRONMENT)
00208    {
00209       fillEnvMap(m_envMap);
00210    }
00211    for( ; first != last; ++first)
00212    {
00213       setVar(first->first.c_str(), first->second.c_str());
00214    }
00215 }
00216 
00217 }  // End of BLOCXX_NAMESPACE
00218 
00219 #endif   // BLOCXX_ENVVARS_HPP__

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6