You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: unixstl/system/environment_variable.hpp
  3. *
  4. * Purpose: Simple class that provides access to an environment variable.
  5. *
  6. * Created: 2nd November 2003
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file unixstl/system/environment_variable.hpp
  40. *
  41. * \brief [C++ only] Definition of the unixstl::basic_environment_variable
  42. * class template
  43. * (\ref group__library__system "System" Library).
  44. */
  45. #ifndef UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_ENVIRONMENT_VARIABLE
  46. #define UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_ENVIRONMENT_VARIABLE
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_ENVIRONMENT_VARIABLE_MAJOR 4
  49. # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_ENVIRONMENT_VARIABLE_MINOR 2
  50. # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_ENVIRONMENT_VARIABLE_REVISION 1
  51. # define UNIXSTL_VER_UNIXSTL_SYSTEM_HPP_ENVIRONMENT_VARIABLE_EDIT 65
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
  57. # include <unixstl/unixstl.h>
  58. #endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
  59. #ifndef UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS
  60. # include <unixstl/system/system_traits.hpp> // for get_environment_variable()
  61. #endif /* !UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_SYSTEM_TRAITS */
  62. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  63. # include <stlsoft/shims/access/string.hpp>
  64. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  65. #ifndef UNIXSTL_INCL_UNIXSTL_SHIMS_ACCESS_HPP_STRING
  66. # include <unixstl/shims/access/string.hpp>
  67. #endif /* !UNIXSTL_INCL_UNIXSTL_SHIMS_ACCESS_HPP_STRING */
  68. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
  69. # include <stlsoft/memory/auto_buffer.hpp>
  70. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
  71. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
  72. # include <stlsoft/memory/allocator_selector.hpp>
  73. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Namespace
  76. */
  77. #ifndef _UNIXSTL_NO_NAMESPACE
  78. # if defined(_STLSOFT_NO_NAMESPACE) || \
  79. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  80. /* There is no stlsoft namespace, so must define ::unixstl */
  81. namespace unixstl
  82. {
  83. # else
  84. /* Define stlsoft::unixstl_project */
  85. namespace stlsoft
  86. {
  87. namespace unixstl_project
  88. {
  89. # endif /* _STLSOFT_NO_NAMESPACE */
  90. #endif /* !_UNIXSTL_NO_NAMESPACE */
  91. /* /////////////////////////////////////////////////////////////////////////
  92. * basic_environment_variable
  93. *
  94. * This class converts a relative path to an absolute one, and effectively acts
  95. * as a C-string of its value.
  96. */
  97. /** \brief Represents an environment variable
  98. *
  99. * \ingroup group__library__system
  100. *
  101. * \param C The character type
  102. * \param T The traits type. Defaults to system_traits<C>. On translators that do not support default template arguments, it must be explicitly stipulated
  103. * \param A The allocator class. Defaults to stlsoft::allocator_selector<C>::allocator_type. On translators that do not support default template arguments, it is not part of the template parameter list.
  104. */
  105. template< ss_typename_param_k C
  106. #ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
  107. , ss_typename_param_k T = system_traits<C>
  108. , ss_typename_param_k A = ss_typename_type_def_k stlsoft_ns_qual(allocator_selector)<C>::allocator_type
  109. #else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  110. , ss_typename_param_k T /* = system_traits<C> */
  111. #endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  112. >
  113. class basic_environment_variable
  114. {
  115. public:
  116. /// The char type
  117. typedef C char_type;
  118. /// The traits type
  119. typedef T traits_type;
  120. #ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
  121. /// The allocator type
  122. typedef A allocator_type;
  123. /// The current parameterisation of the type
  124. typedef basic_environment_variable<C, T, A> class_type;
  125. #else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  126. /// The current parameterisation of the type
  127. typedef basic_environment_variable<C, T> class_type;
  128. #endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  129. /// The size type
  130. typedef us_size_t size_type;
  131. /// The difference type
  132. typedef us_ptrdiff_t difference_type;
  133. // Construction
  134. public:
  135. /// Create an instance representing the given environment variable
  136. ss_explicit_k basic_environment_variable(char_type const* name)
  137. : m_buffer(1 + traits_type::get_environment_variable(name, 0, 0))
  138. {
  139. if( 0 == traits_type::get_environment_variable(name, &m_buffer[0], m_buffer.size()) &&
  140. 0 != m_buffer.size())
  141. {
  142. m_buffer[0] = 0;
  143. }
  144. }
  145. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  146. /// Create an instance representing the given environment variable
  147. template<ss_typename_param_k S>
  148. ss_explicit_k basic_environment_variable(S const& name)
  149. : m_buffer(1 + traits_type::get_environment_variable(stlsoft_ns_qual(c_str_ptr)(name), 0, 0))
  150. {
  151. if( 0 == traits_type::get_environment_variable(stlsoft_ns_qual(c_str_ptr)(name), &m_buffer[0], m_buffer.size()) &&
  152. 0 != m_buffer.size())
  153. {
  154. m_buffer[0] = 0;
  155. }
  156. }
  157. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  158. // Conversions
  159. public:
  160. /// Implicit conversion to a non-mutable (const) pointer to the variable
  161. operator char_type const* () const
  162. {
  163. return m_buffer.data();
  164. }
  165. // Attributes
  166. public:
  167. /// Returns the length of the variable
  168. size_type length() const
  169. {
  170. return m_buffer.size() - 1;
  171. }
  172. // Members
  173. private:
  174. typedef stlsoft_ns_qual(auto_buffer_old)< char_type
  175. #ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
  176. , allocator_type
  177. #else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  178. , malloc_allocator<char_type>
  179. #endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  180. > buffer_type;
  181. buffer_type m_buffer;
  182. // Not to be implemented
  183. private:
  184. basic_environment_variable(basic_environment_variable const&);
  185. basic_environment_variable& operator =(basic_environment_variable const&);
  186. };
  187. /* /////////////////////////////////////////////////////////////////////////
  188. * Typedefs for commonly encountered types
  189. */
  190. /** \brief Specialisation of the basic_environment_variable template for the ANSI character type \c char
  191. *
  192. * \ingroup group__library__system
  193. */
  194. typedef basic_environment_variable<us_char_a_t, system_traits<us_char_a_t> > environment_variable_a;
  195. /** \brief Specialisation of the basic_environment_variable template for the Unicode character type \c wchar_t
  196. *
  197. * \ingroup group__library__system
  198. */
  199. typedef basic_environment_variable<us_char_w_t, system_traits<us_char_w_t> > environment_variable_w;
  200. /** \brief Specialisation of the basic_environment_variable template for the 'default' character type \c char
  201. *
  202. * \ingroup group__library__system
  203. */
  204. typedef basic_environment_variable<char, system_traits<char> > environment_variable;
  205. /* /////////////////////////////////////////////////////////////////////////
  206. * Helper functions
  207. */
  208. #if !defined(STLSOFT_COMPILER_IS_MSVC) || \
  209. _MSC_VER >= 1100
  210. /** \brief This helper function makes an environment variable without needing to
  211. * qualify the template parameter.
  212. *
  213. * \ingroup group__library__system
  214. */
  215. template<ss_typename_param_k C>
  216. inline basic_environment_variable<C> make_environment_variable(C const* path)
  217. {
  218. return basic_environment_variable<C>(path);
  219. }
  220. #endif /* compiler */
  221. ////////////////////////////////////////////////////////////////////////////
  222. // Unit-testing
  223. #ifdef STLSOFT_UNITTEST
  224. # include "./unittest/environment_variable_unittest_.h"
  225. #endif /* STLSOFT_UNITTEST */
  226. /* ////////////////////////////////////////////////////////////////////// */
  227. #ifndef _UNIXSTL_NO_NAMESPACE
  228. # if defined(_STLSOFT_NO_NAMESPACE) || \
  229. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  230. } // namespace unixstl
  231. # else
  232. } // namespace unixstl_project
  233. } // namespace stlsoft
  234. # endif /* _STLSOFT_NO_NAMESPACE */
  235. #endif /* !_UNIXSTL_NO_NAMESPACE */
  236. /* ////////////////////////////////////////////////////////////////////// */
  237. #endif /* UNIXSTL_INCL_UNIXSTL_SYSTEM_HPP_ENVIRONMENT_VARIABLE */
  238. /* ///////////////////////////// end of file //////////////////////////// */