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.

500 lines
17 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: platformstl/filesystem/readdir_sequence.hpp
  3. *
  4. * Purpose: Platform header for the readdir_sequence components.
  5. *
  6. * Created: 29th April 2006
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2006-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 platformstl/filesystem/readdir_sequence.hpp
  40. *
  41. * \brief [C++ only] Definition of the platformstl::readdir_sequence
  42. * type
  43. * (\ref group__library__filesystem "File System" Library).
  44. */
  45. #ifndef PLATFORMSTL_INCL_PLATFORMSTL_FILESYSTEM_HPP_READDIR_SEQUENCE
  46. #define PLATFORMSTL_INCL_PLATFORMSTL_FILESYSTEM_HPP_READDIR_SEQUENCE
  47. /* File version */
  48. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  49. # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_MAJOR 2
  50. # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_MINOR 2
  51. # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_REVISION 2
  52. # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_READDIR_SEQUENCE_EDIT 18
  53. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  54. /* /////////////////////////////////////////////////////////////////////////
  55. * Includes
  56. */
  57. #ifndef PLATFORMSTL_INCL_PLATFORMSTL_HPP_PLATFORMSTL
  58. # include <platformstl/platformstl.hpp>
  59. #endif /* !PLATFORMSTL_INCL_PLATFORMSTL_HPP_PLATFORMSTL */
  60. #if defined(PLATFORMSTL_OS_IS_UNIX)
  61. # ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE
  62. # include <unixstl/filesystem/readdir_sequence.hpp>
  63. # endif /* !UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_READDIR_SEQUENCE */
  64. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  65. # ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FINDFILE_SEQUENCE
  66. # include <winstl/filesystem/findfile_sequence.hpp>
  67. # endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FINDFILE_SEQUENCE */
  68. # ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
  69. # include <winstl/filesystem/filesystem_traits.hpp>
  70. # endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
  71. #else /* ? operating system */
  72. # error Operating system not discriminated
  73. #endif /* operating system */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Namespace
  76. */
  77. #if defined(_STLSOFT_NO_NAMESPACE) || \
  78. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  79. /* There is no stlsoft namespace, so must define ::platformstl */
  80. namespace platformstl
  81. {
  82. #else
  83. /* Define stlsoft::platformstl_project */
  84. namespace stlsoft
  85. {
  86. namespace platformstl_project
  87. {
  88. #endif /* _STLSOFT_NO_NAMESPACE */
  89. /* ////////////////////////////////////////////////////////////////////// */
  90. #if defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  91. /** \brief STL-like readonly sequence based on directory contents.
  92. *
  93. * \ingroup group__library__filesystem
  94. *
  95. * The class is only defined in the \link ::platformstl platformstl\endlink
  96. * when the platform is not UNIX, in which case it uses the services
  97. * of another file-system enumeration type, e.g.
  98. * winstl::basic_findfile_sequence. For UNIX compilation, it
  99. * resolves to unixstl::readdir_sequence, relying on
  100. * \ref section__principle__conformance__intersecting_conformance "Intersecting Conformance"
  101. * of the resolved platform-specific types.
  102. */
  103. class readdir_sequence
  104. {};
  105. #elif defined(PLATFORMSTL_OS_IS_UNIX)
  106. # ifdef _UNIXSTL_NO_NAMESPACE
  107. using ::readdir_sequence;
  108. # else /* ? _UNIXSTL_NO_NAMESPACE */
  109. using ::unixstl::readdir_sequence;
  110. # endif /* _UNIXSTL_NO_NAMESPACE */
  111. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  112. class readdir_sequence
  113. {
  114. /// \name Member Types
  115. /// @{
  116. public:
  117. // This class
  118. typedef readdir_sequence class_type;
  119. private:
  120. typedef winstl_ns_qual(ws_char_a_t) char_type;
  121. typedef winstl_ns_qual(basic_findfile_sequence)<char_type> underlying_sequence_type;
  122. typedef winstl_ns_qual(filesystem_traits)<char_type> traits_type;
  123. public:
  124. typedef underlying_sequence_type::const_iterator underlying_sequence_const_iterator_type;
  125. typedef underlying_sequence_type::value_type underlying_sequence_value_type;
  126. public:
  127. // The size type
  128. typedef winstl_ns_qual(ws_size_t) size_type;
  129. // The non-mutating (const) iterator type
  130. class const_iterator;
  131. // The value type
  132. typedef char_type const* value_type;
  133. /// The flags type
  134. typedef winstl_ns_qual(ws_int_t) flags_type;
  135. /// The Boolean type
  136. typedef winstl_ns_qual(ws_bool_t) bool_type;
  137. public:
  138. /// @}
  139. /// \name Member Constants
  140. /// @{
  141. public:
  142. enum search_flags
  143. {
  144. includeDots = 0x0008 /*!< Requests that dots directories be included in the returned sequence */
  145. , directories = 0x0010 /*!< Causes the search to include directories */
  146. , files = 0x0020 /*!< Causes the search to include files */
  147. , fullPath = 0x0100 /*!< Each file entry is presented as a full path relative to the search directory. */
  148. , absolutePath = 0x0200 /*!< The search directory is converted to an absolute path. */
  149. };
  150. /// @}
  151. /// \name Construction
  152. /// @{
  153. public:
  154. /// \brief Constructs a sequence according to the given criteria
  155. ///
  156. /// The constructor initialises a readdir_sequence instance on the given
  157. /// directory with the given flags.
  158. ///
  159. /// \param directory The directory whose contents are to be searched
  160. /// \param flags Flags to alter the behaviour of the search
  161. ///
  162. /// \note The \c flags parameter defaults to <code>directories | files</code> because
  163. /// this reflects the default behaviour of \c readdir(), and also because it is the
  164. /// most efficient.
  165. template <ss_typename_param_k S>
  166. ss_explicit_k readdir_sequence(S const& directory)
  167. : m_ffs(stlsoft_ns_qual(c_str_ptr)(directory), "*.*", translate_flags_(directories | files))
  168. , m_flags(validate_flags_(directories | files))
  169. {}
  170. template <ss_typename_param_k S>
  171. readdir_sequence(S const& directory, flags_type flags)
  172. : m_ffs(stlsoft_ns_qual(c_str_ptr)(directory), "*.*", translate_flags_(flags))
  173. , m_flags(validate_flags_(flags))
  174. {}
  175. template <ss_typename_param_k S>
  176. readdir_sequence(S const& directory, search_flags flags)
  177. : m_ffs(stlsoft_ns_qual(c_str_ptr)(directory), "*.*", translate_flags_(flags))
  178. , m_flags(validate_flags_(flags))
  179. {}
  180. /// @}
  181. /// \name Iteration
  182. /// @{
  183. public:
  184. /// Begins the iteration
  185. ///
  186. /// \return An iterator representing the start of the sequence
  187. const_iterator begin() const;
  188. /// Ends the iteration
  189. ///
  190. /// \return An iterator representing the end of the sequence
  191. const_iterator end() const;
  192. /// @}
  193. /// \name Attributes
  194. /// @{
  195. public:
  196. /// \brief Indicates whether the search sequence is empty
  197. bool_type empty() const;
  198. /// \brief The search directory
  199. ///
  200. /// \note The value returned by this method always has a trailing path name separator, so
  201. /// you can safely concatenate this with the value returned by the iterator's operator *()
  202. /// with minimal fuss.
  203. char_type const *get_directory() const;
  204. /// \brief The flags used by the sequence
  205. ///
  206. /// \note This value is the value used by the sequence, which may, as a result of the
  207. /// determination of defaults, be different from those specified in its constructor. In
  208. /// other words, if <code>includeDots</code> is specified, this function
  209. /// will return <code>includeDots | directories | files</code>
  210. flags_type get_flags() const;
  211. /// @}
  212. /// \name Implementation
  213. /// @{
  214. private:
  215. /// \brief Ensures that the flags are correct
  216. static flags_type validate_flags_(flags_type flags);
  217. /// \brief Translates the flags to the underlying sequence
  218. static flags_type translate_flags_(flags_type flags);
  219. /// @}
  220. /// \name Members
  221. /// @{
  222. private:
  223. const underlying_sequence_type m_ffs;
  224. const flags_type m_flags;
  225. /// @}
  226. /// \name Not to be implemented
  227. /// @{
  228. private:
  229. readdir_sequence(class_type const&);
  230. class_type& operator =(class_type const&);
  231. /// @}
  232. };
  233. /// \brief Iterator for readdir_sequence class
  234. ///
  235. /// This class performs as a non-mutating iterator (aka const iterator) for the
  236. /// readdir_sequence class.
  237. class readdir_sequence::const_iterator
  238. : public stlsoft_ns_qual(iterator_base)<platformstl_ns_qual_std(input_iterator_tag)
  239. , readdir_sequence::value_type
  240. , ss_ptrdiff_t
  241. , void
  242. , readdir_sequence::value_type
  243. >
  244. {
  245. /// \name Members
  246. /// @{
  247. public:
  248. /// The class type
  249. typedef const_iterator class_type;
  250. /// The value type
  251. typedef readdir_sequence::value_type value_type;
  252. /// The flags type
  253. typedef readdir_sequence::flags_type flags_type;
  254. /// @}
  255. /// \name Construction
  256. /// @{
  257. private:
  258. friend class readdir_sequence;
  259. /// Construct an instance and begin a sequence iteration on the given dir.
  260. const_iterator(underlying_sequence_const_iterator_type it, flags_type flags)
  261. : m_it(it)
  262. , m_flags(flags)
  263. {}
  264. public:
  265. /// Default constructor
  266. const_iterator()
  267. : m_it()
  268. , m_flags(0)
  269. {}
  270. /// Copy constructor
  271. const_iterator(class_type const& rhs)
  272. : m_it(rhs.m_it)
  273. , m_flags(rhs.m_flags)
  274. {}
  275. /// Release the search handle
  276. ~const_iterator() stlsoft_throw_0()
  277. {}
  278. /// Copy assignment operator
  279. class_type const& operator =(class_type const& rhs)
  280. {
  281. m_it = rhs.m_it;
  282. m_flags = rhs.m_flags;
  283. return *this;
  284. }
  285. /// @}
  286. /// \name Input Iterator Methods
  287. /// @{
  288. public:
  289. /// Returns the value representative
  290. value_type operator *() const
  291. {
  292. remove_const(m_value) = *m_it;
  293. return (fullPath & m_flags) ? m_value.get_path() : m_value.get_filename();
  294. }
  295. /// Moves the iteration on to the next point in the sequence, or end() if
  296. /// the sequence is exhausted
  297. class_type& operator ++()
  298. {
  299. ++m_it;
  300. return *this;
  301. }
  302. /// Post-increment form of operator ++().
  303. ///
  304. /// \note Because this version uses a temporary on which to call the
  305. /// pre-increment form it is thereby less efficient, and should not be used
  306. /// except where post-increment semantics are required.
  307. class_type operator ++(int)
  308. {
  309. class_type r(*this);
  310. operator ++();
  311. return r;
  312. }
  313. /// Compares \c this for equality with \c rhs
  314. ///
  315. /// \param rhs The instance against which to test
  316. /// \retval true if the iterators are equivalent
  317. /// \retval false if the iterators are not equivalent
  318. bool equal(class_type const& rhs) const
  319. {
  320. return m_it == rhs.m_it;
  321. }
  322. /// @}
  323. /// \name Members
  324. /// @{
  325. private:
  326. underlying_sequence_const_iterator_type m_it;
  327. underlying_sequence_value_type m_value;
  328. flags_type m_flags;
  329. /// @}
  330. };
  331. inline /* static */ readdir_sequence::flags_type readdir_sequence::validate_flags_(readdir_sequence::flags_type flags)
  332. {
  333. const flags_type validFlags = 0
  334. | includeDots
  335. | directories
  336. | files
  337. | fullPath
  338. | absolutePath
  339. | 0;
  340. WINSTL_MESSAGE_ASSERT("Specification of unrecognised/unsupported flags", flags == (flags & validFlags));
  341. STLSOFT_SUPPRESS_UNUSED(validFlags);
  342. if(0 == (flags & (directories | files)))
  343. {
  344. flags |= (directories | files);
  345. }
  346. return flags;
  347. }
  348. inline /* static */ readdir_sequence::flags_type readdir_sequence::translate_flags_(readdir_sequence::flags_type flags)
  349. {
  350. flags_type translatedFlags = 0;
  351. if(underlying_sequence_type::includeDots & flags)
  352. {
  353. translatedFlags |= underlying_sequence_type::includeDots;
  354. }
  355. if(directories & flags)
  356. {
  357. translatedFlags |= underlying_sequence_type::directories;
  358. }
  359. if(files & flags)
  360. {
  361. translatedFlags |= underlying_sequence_type::files;
  362. }
  363. if(fullPath == ((fullPath | absolutePath) & flags))
  364. {
  365. translatedFlags |= underlying_sequence_type::relativePath;
  366. }
  367. return translatedFlags;
  368. }
  369. inline readdir_sequence::const_iterator readdir_sequence::begin() const
  370. {
  371. return const_iterator(m_ffs.begin(), m_flags);
  372. }
  373. inline readdir_sequence::const_iterator readdir_sequence::end() const
  374. {
  375. return const_iterator(m_ffs.end(), m_flags);
  376. }
  377. inline readdir_sequence::bool_type readdir_sequence::empty() const
  378. {
  379. return m_ffs.empty();
  380. }
  381. inline readdir_sequence::char_type const* readdir_sequence::get_directory() const
  382. {
  383. return m_ffs.get_directory();
  384. }
  385. inline readdir_sequence::flags_type readdir_sequence::get_flags() const
  386. {
  387. return m_flags;
  388. }
  389. inline ss_bool_t operator ==( readdir_sequence::const_iterator const& lhs
  390. , readdir_sequence::const_iterator const& rhs)
  391. {
  392. return lhs.equal(rhs);
  393. }
  394. inline ss_bool_t operator !=( readdir_sequence::const_iterator const& lhs
  395. , readdir_sequence::const_iterator const& rhs)
  396. {
  397. return !lhs.equal(rhs);
  398. }
  399. #else /* ? operating system */
  400. # error Operating system not discriminated
  401. #endif /* operating system */
  402. /* ////////////////////////////////////////////////////////////////////// */
  403. #if defined(_STLSOFT_NO_NAMESPACE) || \
  404. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  405. } // namespace platformstl
  406. #else
  407. } // namespace platformstl_project
  408. } // namespace stlsoft
  409. #endif /* _STLSOFT_NO_NAMESPACE */
  410. #if defined(PLATFORMSTL_OS_IS_WINDOWS) && \
  411. ( defined(STLSOFT_COMPILER_IS_BORLAND) || \
  412. defined(STLSOFT_COMPILER_IS_DMC))
  413. inline int operator ==( platformstl::readdir_sequence::const_iterator const& lhs
  414. , platformstl::readdir_sequence::const_iterator const& rhs)
  415. {
  416. return lhs.equal(rhs);
  417. }
  418. inline int operator !=( platformstl::readdir_sequence::const_iterator const& lhs
  419. , platformstl::readdir_sequence::const_iterator const& rhs)
  420. {
  421. return !lhs.equal(rhs);
  422. }
  423. #endif /* WIN32 && compiler */
  424. /* ////////////////////////////////////////////////////////////////////// */
  425. #endif /* !PLATFORMSTL_INCL_PLATFORMSTL_FILESYSTEM_HPP_READDIR_SEQUENCE */
  426. /* ///////////////////////////// end of file //////////////////////////// */