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.

301 lines
10 KiB

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