STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

winstl_environment_variable.h

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_environment_variable.h
00003  *
00004  * Purpose:     Simple class that provides access to an environment variable.
00005  *
00006  * Created:     20th December 2002
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2002-2004, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00044 
00045 #ifndef WINSTL_INCL_H_WINSTL_ENVIRONMENT_VARIABLE
00046 #define WINSTL_INCL_H_WINSTL_ENVIRONMENT_VARIABLE
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define WINSTL_VER_H_WINSTL_ENVIRONMENT_VARIABLE_MAJOR     2
00050 # define WINSTL_VER_H_WINSTL_ENVIRONMENT_VARIABLE_MINOR     0
00051 # define WINSTL_VER_H_WINSTL_ENVIRONMENT_VARIABLE_REVISION  1
00052 # define WINSTL_VER_H_WINSTL_ENVIRONMENT_VARIABLE_EDIT      29
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Compatibility
00057  */
00058 
00059 /*
00060 [Incompatibilies-start]
00061 __STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
00062 [Incompatibilies-end]
00063  */
00064 
00065 /* 
00066  * Includes
00067  */
00068 
00069 #ifndef WINSTL_INCL_H_WINSTL
00070 # include "winstl.h"                        // Include the WinSTL root header
00071 #endif /* !WINSTL_INCL_H_WINSTL */
00072 
00073 #if defined(__STLSOFT_COMPILER_IS_MSVC) && \
00074     _MSC_VER < 1200
00075 # error winstl_environment_variable.h is not compatible with Visual C++ 5.0 or earlier
00076 #endif /* _MSC_VER < 1200 */
00077 
00078 #ifndef WINSTL_INCL_H_WINSTL_SYSTEM_VERSION
00079 # include "winstl_filesystem_traits.h"      // Include the WinSTL get_environment_variable
00080 #endif /* !WINSTL_INCL_H_WINSTL_SYSTEM_VERSION */
00081 #ifndef STLSOFT_INCL_H_STLSOFT_STRING_ACCESS
00082 # include "stlsoft_string_access.h"         // stlsoft::c_str_ptr
00083 #endif /* !STLSOFT_INCL_H_STLSOFT_STRING_ACCESS */
00084 #ifndef WINSTL_INCL_H_WINSTL_STRING_ACCESS
00085 # include "winstl_string_access.h"          // winstl::c_str_ptr
00086 #endif /* !WINSTL_INCL_H_WINSTL_STRING_ACCESS */
00087 #ifndef STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER
00088 # include "stlsoft_auto_buffer.h"           // stlsoft::auto_buffer
00089 #endif /* !STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER */
00090 #ifndef WINSTL_INCL_H_WINSTL_PROCESSHEAP_ALLOCATOR
00091 # include "winstl_processheap_allocator.h"  // processheap_allocator
00092 #endif /* WINSTL_INCL_H_WINSTL_PROCESSHEAP_ALLOCATOR */
00093 
00094 /* 
00095  * Namespace
00096  */
00097 
00098 #ifndef _WINSTL_NO_NAMESPACE
00099 # if defined(_STLSOFT_NO_NAMESPACE) || \
00100      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00101 /* There is no stlsoft namespace, so must define ::winstl */
00102 namespace winstl
00103 {
00104 # else
00105 /* Define stlsoft::winstl_project */
00106 
00107 namespace stlsoft
00108 {
00109 
00110 namespace winstl_project
00111 {
00112 
00113 # endif /* _STLSOFT_NO_NAMESPACE */
00114 #endif /* !_WINSTL_NO_NAMESPACE */
00115 
00116 #if !defined(__STLSOFT_COMPILER_IS_MWERKS)
00117 stlsoft_ns_using(c_str_ptr)
00118 #endif /* compiler */
00119 
00120 /* 
00121 
00124 
00128 
00133 
00134 /* 
00135  * basic_environment_variable
00136  *
00137  * This class converts a relative path to an absolute one, and effectively acts
00138  * as a C-string of its value.
00139  */
00140 
00145 template<   ss_typename_param_k C
00146 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00147         ,   ss_typename_param_k T = filesystem_traits<C>
00148 #else
00149         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00150 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00151         >
00152 class basic_environment_variable
00153 {
00154 public:
00156     typedef C                                   char_type;
00158     typedef T                                   traits_type;
00160     typedef basic_environment_variable<C, T>    class_type;
00162     typedef ss_size_t                           size_type;
00163 
00164 // Construction
00165 public:
00167     ss_explicit_k basic_environment_variable(char_type const *name)
00168         : m_buffer(1 + traits_type::get_environment_variable(name, 0, 0))
00169     {
00170         if( 0 == traits_type::get_environment_variable(name, m_buffer, m_buffer.size()) &&
00171             0 != m_buffer.size())
00172         {
00173             m_buffer[0] = 0;
00174         }
00175     }
00176 #ifdef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00177 
00178     template<ss_typename_param_k S>
00179     ss_explicit_k basic_environment_variable(S const &name)
00180         : m_buffer(1 + traits_type::get_environment_variable(c_str_ptr(name), 0, 0))
00181     {
00182         if( 0 == traits_type::get_environment_variable(c_str_ptr(name), m_buffer, m_buffer.size()) &&
00183             0 != m_buffer.size())
00184         {
00185             m_buffer[0] = 0;
00186         }
00187     }
00188 #endif /* __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
00189 
00190 // Conversions
00191 public:
00193     operator char_type const *() const
00194     {
00195         return m_buffer.data();
00196     }
00197 
00198 // Attributes
00199 public:
00201     size_type length() const
00202     {
00203         return m_buffer.size() - 1;
00204     }
00205 
00206 // Members
00207 private:
00208     typedef stlsoft_ns_qual(auto_buffer)<char_type, processheap_allocator<char_type> >  buffer_t;
00209 
00210     buffer_t    m_buffer;
00211 
00212 // Not to be implemented
00213 private:
00214     basic_environment_variable(basic_environment_variable const &);
00215     basic_environment_variable &operator =(basic_environment_variable const &);
00216 };
00217 
00218 /* 
00219  * Typedefs for commonly encountered types
00220  */
00221 
00223 typedef basic_environment_variable<ws_char_a_t, filesystem_traits<ws_char_a_t> >     environment_variable_a;
00225 typedef basic_environment_variable<ws_char_w_t, filesystem_traits<ws_char_w_t> >     environment_variable_w;
00227 typedef basic_environment_variable<TCHAR, filesystem_traits<TCHAR> >                 environment_variable;
00228 
00230 // Unit-testing
00231 
00232 #ifdef STLSOFT_UNITTEST
00233 
00234 namespace unittest
00235 {
00236     ss_bool_t test_winstl_environment_variable(unittest_reporter *r)
00237     {
00238         using stlsoft::unittest::unittest_initialiser;
00239 
00240         ss_bool_t               bSuccess    =   true;
00241 
00242         unittest_initialiser    init(r, "WinSTL", "environment_variable", __FILE__);
00243 
00244         typedef environment_variable_a  env_var_t;
00245 
00246         env_var_t   path("PATH");
00247 
00248         if(0 != strcmp(getenv("PATH"), path))
00249         {
00250             r->report("basic_environment_variable<char> failed ", __LINE__);
00251             bSuccess = false;
00252         }
00253 
00254         return bSuccess;
00255     }
00256 
00257     unittest_registrar    unittest_winstl_environment_variable(test_winstl_environment_variable);
00258 
00259 } // namespace unittest
00260 
00261 #endif /* STLSOFT_UNITTEST */
00262 
00263 /* 
00264 
00265 #ifndef _WINSTL_NO_NAMESPACE
00266 # if defined(_STLSOFT_NO_NAMESPACE) || \
00267      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00268 } // namespace winstl
00269 # else
00270 } // namespace winstl_project
00271 } // namespace stlsoft
00272 # endif /* _STLSOFT_NO_NAMESPACE */
00273 #endif /* !_WINSTL_NO_NAMESPACE */
00274 
00275 /* 
00276 
00277 #endif /* WINSTL_INCL_H_WINSTL_ENVIRONMENT_VARIABLE */
00278 
00279 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004