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.

274 lines
9.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/filesystem/absolute_path.hpp
  3. *
  4. * Purpose: Simple class that converts a relative path to an absolute one.
  5. *
  6. * Created: 20th December 2002
  7. * Updated: 12th August 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2010, 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 winstl/filesystem/absolute_path.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::basic_absolute_path class template
  42. * (\ref group__library__filesystem "File System" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_ABSOLUTE_PATH
  45. #define WINSTL_INCL_WINSTL_FILESYSTEM_HPP_ABSOLUTE_PATH
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_ABSOLUTE_PATH_MAJOR 4
  48. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_ABSOLUTE_PATH_MINOR 3
  49. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_ABSOLUTE_PATH_REVISION 1
  50. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_ABSOLUTE_PATH_EDIT 68
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  56. # include <winstl/winstl.h>
  57. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  58. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SPECIAL_STRING_INSTANCE
  59. # include <stlsoft/string/special_string_instance.hpp>
  60. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SPECIAL_STRING_INSTANCE */
  61. #ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
  62. # include <winstl/filesystem/filesystem_traits.hpp>
  63. #endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
  64. #ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
  65. # include <winstl/memory/processheap_allocator.hpp>
  66. #endif /* !WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR */
  67. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  68. # include <stlsoft/shims/access/string.hpp>
  69. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  70. #ifndef WINSTL_INCL_WINSTL_SHIMS_ACCESS_HPP_STRING
  71. # include <winstl/shims/access/string.hpp>
  72. #endif /* !WINSTL_INCL_WINSTL_SHIMS_ACCESS_HPP_STRING */
  73. #if defined(STLSOFT_COMPILER_IS_BORLAND)
  74. // Borland is a bit of a thicky, and requires a (valid) spin_mutex_type
  75. # include <winstl/synch/spin_mutex.hpp>
  76. #endif /* compiler */
  77. /* /////////////////////////////////////////////////////////////////////////
  78. * Namespace
  79. */
  80. #ifndef _WINSTL_NO_NAMESPACE
  81. # if defined(_STLSOFT_NO_NAMESPACE) || \
  82. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  83. /* There is no stlsoft namespace, so must define ::winstl */
  84. namespace winstl
  85. {
  86. # else
  87. /* Define stlsoft::winstl_project */
  88. namespace stlsoft
  89. {
  90. namespace winstl_project
  91. {
  92. # endif /* _STLSOFT_NO_NAMESPACE */
  93. #endif /* !_WINSTL_NO_NAMESPACE */
  94. /* /////////////////////////////////////////////////////////////////////////
  95. * Classes
  96. */
  97. /** \brief \ref group__pattern__special_string_instance "Special String Instance"
  98. * policy template for eliciting the <b>absolute form</b> of a given path.
  99. *
  100. * \ingroup group__library__system
  101. */
  102. template <ss_typename_param_k C>
  103. struct abspath_policy
  104. {
  105. /// \name Member Types
  106. /// @{
  107. public:
  108. typedef C char_type;
  109. typedef char_type const* argument_0_type;
  110. typedef processheap_allocator<C> allocator_type;
  111. typedef ws_size_t size_type;
  112. typedef size_type (*pfn_type)(argument_0_type, char_type *, size_type);
  113. #if defined(STLSOFT_COMPILER_IS_BORLAND)
  114. // Borland is a bit of a thicky, and requires a (valid) spin_mutex_type
  115. typedef winstl::spin_mutex spin_mutex_type;
  116. typedef winstl::atomic_int_t atomic_int_type;
  117. #endif /* compiler */
  118. /// @}
  119. /// \name Member Constants
  120. /// @{
  121. public:
  122. enum { internalBufferSize = 128 };
  123. enum { allowImplicitConversion = 1 };
  124. enum { sharedState = 0 };
  125. /// @}
  126. /// \name Operations
  127. /// @{
  128. public:
  129. static pfn_type get_fn()
  130. {
  131. return winstl::filesystem_traits<char_type>::get_full_path_name;
  132. }
  133. /// @}
  134. };
  135. /* /////////////////////////////////////////////////////////////////////////
  136. * basic_absolute_path
  137. *
  138. * This class converts a relative path to an absolute one, and effectively acts
  139. * as a C-string of its value.
  140. */
  141. /** \brief Converts a relative path to an absolute path
  142. *
  143. * \ingroup group__library__filesystem
  144. *
  145. * \param C The character type
  146. * \param T The traits type. On translators that support default template
  147. * arguments, this defaults to filesystem_traits<C>
  148. */
  149. template <ss_typename_param_k C>
  150. class basic_absolute_path
  151. : public stlsoft_ns_qual(special_string_instance_1)<abspath_policy<C> >
  152. {
  153. /// \name Member Types
  154. /// @{
  155. private:
  156. typedef stlsoft_ns_qual(special_string_instance_1)<abspath_policy<C> > parent_class_type;
  157. public:
  158. /// \brief The char type
  159. typedef ss_typename_type_k parent_class_type::char_type char_type;
  160. /// \brief The current parameterisation of the type
  161. typedef basic_absolute_path<C> class_type;
  162. /// \brief The size type
  163. typedef ss_typename_type_k parent_class_type::size_type size_type;
  164. /// @}
  165. /// \name Construction
  166. /// @{
  167. public:
  168. /// \brief Constructs an absolute path from \c path
  169. ss_explicit_k basic_absolute_path(char_type const* path)
  170. : parent_class_type(path)
  171. {}
  172. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
  173. /// \brief Constructs an absolute path from \c path
  174. template<ss_typename_param_k S>
  175. ss_explicit_k basic_absolute_path(S const& path)
  176. : parent_class_type(stlsoft_ns_qual(c_str_ptr)(path))
  177. {}
  178. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  179. /// @}
  180. /** \brief @{
  181. *
  182. * \ingroup group__library__filesystem
  183. */
  184. private:
  185. basic_absolute_path(class_type const&);
  186. class_type& operator =(class_type const&);
  187. /// @}
  188. };
  189. /* /////////////////////////////////////////////////////////////////////////
  190. * Typedefs for commonly encountered types
  191. */
  192. /** \brief Specialisation of the basic_absolute_path template for the ANSI character type \c char
  193. *
  194. * \ingroup group__library__filesystem
  195. */
  196. typedef basic_absolute_path<ws_char_a_t> absolute_path_a;
  197. /** \brief Specialisation of the basic_absolute_path template for the Unicode character type \c wchar_t
  198. *
  199. * \ingroup group__library__filesystem
  200. */
  201. typedef basic_absolute_path<ws_char_w_t> absolute_path_w;
  202. /** \brief Specialisation of the basic_absolute_path template for the Win32 character type \c TCHAR
  203. *
  204. * \ingroup group__library__filesystem
  205. */
  206. typedef basic_absolute_path<TCHAR> absolute_path;
  207. /* /////////////////////////////////////////////////////////////////////////
  208. * Helper functions
  209. */
  210. #if !defined(STLSOFT_COMPILER_IS_MSVC) || \
  211. _MSC_VER >= 1100
  212. /** \brief This \ref group__pattern__creator_function "creator function"
  213. * makes an absolute path variable without needing to qualify the template
  214. * parameter.
  215. */
  216. template<ss_typename_param_k C>
  217. inline basic_absolute_path<C> make_absolute_path(C const* path)
  218. {
  219. return basic_absolute_path<C>(path);
  220. }
  221. #endif /* compiler */
  222. ////////////////////////////////////////////////////////////////////////////
  223. // Unit-testing
  224. #ifdef STLSOFT_UNITTEST
  225. # include "./unittest/absolute_path_unittest_.h"
  226. #endif /* STLSOFT_UNITTEST */
  227. /* ////////////////////////////////////////////////////////////////////// */
  228. #ifndef _WINSTL_NO_NAMESPACE
  229. # if defined(_STLSOFT_NO_NAMESPACE) || \
  230. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  231. } // namespace winstl
  232. # else
  233. } // namespace winstl_project
  234. } // namespace stlsoft
  235. # endif /* _STLSOFT_NO_NAMESPACE */
  236. #endif /* !_WINSTL_NO_NAMESPACE */
  237. /* ////////////////////////////////////////////////////////////////////// */
  238. #endif /* WINSTL_INCL_WINSTL_FILESYSTEM_HPP_ABSOLUTE_PATH */
  239. /* ///////////////////////////// end of file //////////////////////////// */