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  

unixstl_environment_variable.h

Go to the documentation of this file.
00001 /* 
00002  * File:        unixstl_environment_variable.h
00003  *
00004  * Purpose:     Simple class that provides access to an environment variable.
00005  *
00006  * Created:     2nd November 2003
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2003-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 UNIXSTL_INCL_H_UNIXSTL_ENVIRONMENT_VARIABLE
00046 #define UNIXSTL_INCL_H_UNIXSTL_ENVIRONMENT_VARIABLE
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define UNIXSTL_VER_H_UNIXSTL_ENVIRONMENT_VARIABLE_MAJOR     2
00050 # define UNIXSTL_VER_H_UNIXSTL_ENVIRONMENT_VARIABLE_MINOR     0
00051 # define UNIXSTL_VER_H_UNIXSTL_ENVIRONMENT_VARIABLE_REVISION  1
00052 # define UNIXSTL_VER_H_UNIXSTL_ENVIRONMENT_VARIABLE_EDIT      27
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef UNIXSTL_INCL_H_UNIXSTL
00060 # include "unixstl.h"                        // Include the UNIXSTL root header
00061 #endif /* !UNIXSTL_INCL_H_UNIXSTL */
00062 #ifndef UNIXSTL_INCL_H_UNIXSTL_SYSTEM_VERSION
00063 # include "unixstl_filesystem_traits.h"      // Include the UNIXSTL get_environment_variable
00064 #endif /* !UNIXSTL_INCL_H_UNIXSTL_SYSTEM_VERSION */
00065 #ifndef STLSOFT_INCL_H_STLSOFT_STRING_ACCESS
00066 # include "stlsoft_string_access.h"         // stlsoft::c_str_ptr
00067 #endif /* !STLSOFT_INCL_H_STLSOFT_STRING_ACCESS */
00068 #ifndef UNIXSTL_INCL_H_UNIXSTL_STRING_ACCESS
00069 # include "unixstl_string_access.h"          // unixstl::c_str_ptr
00070 #endif /* !UNIXSTL_INCL_H_UNIXSTL_STRING_ACCESS */
00071 #ifndef STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER
00072 # include "stlsoft_auto_buffer.h"           // stlsoft::auto_buffer
00073 #endif /* !STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER */
00074 #ifndef STLSOFT_INCL_H_STLSOFT_MALLOC_ALLOCATOR
00075 # include "stlsoft_malloc_allocator.h"      // malloc_allocator
00076 #endif /* STLSOFT_INCL_H_STLSOFT_MALLOC_ALLOCATOR */
00077 
00078 /* 
00079  * Namespace
00080  */
00081 
00082 #ifndef _UNIXSTL_NO_NAMESPACE
00083 # if defined(_STLSOFT_NO_NAMESPACE) || \
00084      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00085 /* There is no stlsoft namespace, so must define ::unixstl */
00086 namespace unixstl
00087 {
00088 # else
00089 /* Define stlsoft::unixstl_project */
00090 
00091 namespace stlsoft
00092 {
00093 
00094 namespace unixstl_project
00095 {
00096 
00097 # endif /* _STLSOFT_NO_NAMESPACE */
00098 #endif /* !_UNIXSTL_NO_NAMESPACE */
00099 
00100 #if !defined(__STLSOFT_COMPILER_IS_GCC) && \
00101     __GNUC__ < 3
00102 stlsoft_ns_using(c_str_ptr)
00103 #endif /* !gcc or gcc >= 3 */
00104 
00105 /* 
00106 
00109 
00113 
00118 
00119 /* 
00120  * basic_environment_variable
00121  *
00122  * This class converts a relative path to an absolute one, and effectively acts
00123  * as a C-string of its value.
00124  */
00125 
00130 template<   ss_typename_param_k C
00131 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00132         ,   ss_typename_param_k T = filesystem_traits<C>
00133 #else
00134         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00135 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00136         >
00137 class basic_environment_variable
00138 {
00139 public:
00141     typedef C                                   char_type;
00143     typedef T                                   traits_type;
00145     typedef basic_environment_variable<C, T>    class_type;
00147     typedef us_size_t                           size_type;
00148 
00149 // Construction
00150 public:
00152     ss_explicit_k basic_environment_variable(char_type const *name)
00153         : m_buffer(1 + traits_type::get_environment_variable(name, 0, 0))
00154     {
00155         if( 0 == traits_type::get_environment_variable(name, m_buffer, m_buffer.size()) &&
00156             0 != m_buffer.size())
00157         {
00158             m_buffer[0] = 0;
00159         }
00160     }
00161 #ifdef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00162 
00163     template<ss_typename_param_k S>
00164     ss_explicit_k basic_environment_variable(S const &name)
00165         : m_buffer(1 + traits_type::get_environment_variable(c_str_ptr(name), 0, 0))
00166     {
00167         if( 0 == traits_type::get_environment_variable(c_str_ptr(name), m_buffer, m_buffer.size()) &&
00168             0 != m_buffer.size())
00169         {
00170             m_buffer[0] = 0;
00171         }
00172     }
00173 #endif /* __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
00174 
00175 // Conversions
00176 public:
00178     operator char_type const *() const
00179     {
00180         return m_buffer.data();
00181     }
00182 
00183 // Attributes
00184 public:
00186     size_type length() const
00187     {
00188         return m_buffer.size() - 1;
00189     }
00190 
00191 // Members
00192 private:
00193     typedef stlsoft_ns_qual(auto_buffer)<char_type, malloc_allocator<char_type> >  buffer_t;
00194 
00195     buffer_t    m_buffer;
00196 
00197 // Not to be implemented
00198 private:
00199     basic_environment_variable(basic_environment_variable const &);
00200     basic_environment_variable &operator =(basic_environment_variable const &);
00201 };
00202 
00203 /* 
00204  * Typedefs for commonly encountered types
00205  */
00206 
00208 typedef basic_environment_variable<us_char_a_t, filesystem_traits<us_char_a_t> >     environment_variable_a;
00210 typedef basic_environment_variable<us_char_w_t, filesystem_traits<us_char_w_t> >     environment_variable_w;
00211 
00212 /* 
00213  * Helper functions
00214  */
00215 
00216 #if !defined(__STLSOFT_COMPILER_IS_MSVC) || \
00217     _MSC_VER >= 1100
00218 
00221 template<ss_typename_param_k C>
00222 inline basic_environment_variable<C> make_environment_variable(C const *path)
00223 {
00224     return basic_environment_variable<C>(path);
00225 }
00226 
00227 #endif /* !(_MSC_VER < 1100) */
00228 
00230 // Unit-testing
00231 
00232 #ifdef STLSOFT_UNITTEST
00233 
00234 namespace unittest
00235 {
00236     ss_bool_t test_unixstl_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, "UNIXSTL", "environment_variable", __FILE__);
00243 
00244         typedef basic_environment_variable<char>    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_unixstl_environment_variable(test_unixstl_environment_variable);
00258 
00259 } // namespace unittest
00260 
00261 #endif /* STLSOFT_UNITTEST */
00262 
00263 /* 
00264 
00265 #ifndef _UNIXSTL_NO_NAMESPACE
00266 # if defined(_STLSOFT_NO_NAMESPACE) || \
00267      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00268 } // namespace unixstl
00269 # else
00270 } // namespace unixstl_project
00271 } // namespace stlsoft
00272 # endif /* _STLSOFT_NO_NAMESPACE */
00273 #endif /* !_UNIXSTL_NO_NAMESPACE */
00274 
00275 /* 
00276 
00277 #endif /* UNIXSTL_INCL_H_UNIXSTL_ENVIRONMENT_VARIABLE */
00278 
00279 /* 

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