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.

497 lines
16 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/filesystem/file_functions.hpp
  3. *
  4. * Purpose: Helper functions for file handling
  5. *
  6. * Created: 1st January 2005
  7. * Updated: 4th July 2012
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2012, 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/file_functions.hpp
  40. *
  41. * \brief [C++ only] Helper functions for (text) file handling
  42. * (\ref group__library__filesystem "File System" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS
  45. #define WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_MAJOR 2
  48. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_MINOR 3
  49. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_REVISION 8
  50. # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_EDIT 54
  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 WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
  59. # include <winstl/filesystem/filesystem_traits.hpp>
  60. #endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
  61. #ifndef WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS
  62. # include <winstl/error/exceptions.hpp>
  63. #endif /* !WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS */
  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_MEMORY_HPP_AUTO_BUFFER
  68. # include <stlsoft/memory/auto_buffer.hpp>
  69. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
  70. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  71. # include <stlsoft/shims/access/string.hpp>
  72. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  73. #ifndef STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE
  74. # include <stlsoft/smartptr/scoped_handle.hpp>
  75. #endif /* !STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE */
  76. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS
  77. # include <stlsoft/string/string_traits.hpp>
  78. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS */
  79. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS
  80. # include <stlsoft/string/tokeniser_functions.hpp> // for find_next_token
  81. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS */
  82. #ifdef STLSOFT_UNITTEST
  83. # include <stlsoft/string/simple_string.hpp>
  84. #endif // STLSOFT_UNITTEST
  85. /* /////////////////////////////////////////////////////////////////////////
  86. * Namespace
  87. */
  88. #ifndef _WINSTL_NO_NAMESPACE
  89. # if defined(_STLSOFT_NO_NAMESPACE) || \
  90. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  91. /* There is no stlsoft namespace, so must define ::winstl */
  92. namespace winstl
  93. {
  94. # else
  95. /* Define stlsoft::winstl_project */
  96. namespace stlsoft
  97. {
  98. namespace winstl_project
  99. {
  100. # endif /* _STLSOFT_NO_NAMESPACE */
  101. #endif /* !_WINSTL_NO_NAMESPACE */
  102. /* /////////////////////////////////////////////////////////////////////////
  103. * Functions
  104. */
  105. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  106. template< ss_typename_param_k S1
  107. , ss_typename_param_k S2
  108. >
  109. inline
  110. ws_uint64_t
  111. load_text_file_impl(
  112. S1 const& fileName
  113. , S2& contents
  114. );
  115. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  116. /** \brief Loads a text file into a string
  117. *
  118. * \ingroup group__library__filesystem
  119. *
  120. * \param fileName The name/path of the text file to load. Can be
  121. * nul-terminated C-style string, or a string object
  122. * \param contents A reference to a string instance into which the contents
  123. * will be loaded (with the assign() method)
  124. *
  125. * \return The number of bytes read from the file
  126. *
  127. \code
  128. std::string contents;
  129. winstl::uint64_t numBytes = winstl::load_text_file("mytextfile.ext", contents);
  130. \endcode
  131. *
  132. * \remarks The character type of the text file is assumed (and controlled)
  133. * to be that of the \c contents parameter. For example, if \c contents is
  134. * of type \c std::wstring then the file will be processed as if it
  135. * contains \c wchar_t.
  136. *
  137. * \note When used with a compiler that does not support partial template
  138. * specialisation, the use of string types for which explicit
  139. * specialisations are not defined will fail. Hence, using
  140. * <code>stlsoft::simple_string</code> (which is the specialisation
  141. * <code>stlsoft::basic_simple_string&lt;char></code>) will succeed
  142. * because a specialisation of <code>stlsoft::string_traits</code> exists
  143. * for that type. The same applies for
  144. * <code>stlsoft::simple_wstring</code>, <code>std::string</code> and
  145. * <code>std::wstring</code>. However, if you attempt to use a
  146. * specialisation of a string class template for which an explicit
  147. * specialisation of <code>stlsoft::string_traits</code> does not exist
  148. * then you will experience a compile-time error in the implementation
  149. * of <code>winstl::load_text_file_impl()</code>. To correct this, you
  150. * must either provide an explicit specialisation of
  151. * <code>stlsoft::string_traits</code> for your type, or use a type for
  152. * which a specialisation of <code>stlsoft::string_traits</code> does
  153. * exist.<br><br>This problem does not occur for compilers that support
  154. * partial template specialisation.
  155. */
  156. template< ss_typename_param_k S1
  157. , ss_typename_param_k S2
  158. >
  159. inline
  160. ws_uint64_t
  161. load_text_file(
  162. S1 const& fileName
  163. , S2& contents
  164. )
  165. {
  166. return load_text_file_impl<S1, S2>(fileName, contents);
  167. }
  168. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  169. template< ss_typename_param_k S1
  170. , ss_typename_param_k S2
  171. >
  172. inline
  173. ws_uint64_t
  174. load_text_file_impl(
  175. S1 const& fileName
  176. , S2& contents
  177. )
  178. {
  179. typedef string_traits<S1> string_traits_t;
  180. STLSOFT_STATIC_ASSERT(sizeof(string_traits_t)); // Fires if S1 does not have a traits specialisation defined
  181. typedef string_traits<S2> string_traits2_t;
  182. STLSOFT_STATIC_ASSERT(sizeof(string_traits2_t)); // Fires if S2 does not have a traits specialisation defined
  183. typedef ss_typename_type_k string_traits_t::char_type C;
  184. STLSOFT_STATIC_ASSERT(sizeof(C)); // Fires if the traits is not correctly defined
  185. typedef ss_typename_type_k string_traits2_t::char_type char_2_type;
  186. STLSOFT_STATIC_ASSERT(sizeof(char_2_type)); // Fires if the traits is not correctly defined
  187. typedef filesystem_traits<C> filesys_traits_t;
  188. STLSOFT_STATIC_ASSERT(sizeof(filesys_traits_t)); // Fires if no corresponding filesystem_traits defined
  189. scoped_handle<HANDLE> h( filesys_traits_t::create_file( stlsoft_ns_qual(c_str_ptr)(fileName)
  190. , GENERIC_READ
  191. , FILE_SHARE_READ
  192. , NULL
  193. , OPEN_EXISTING
  194. , 0
  195. , NULL)
  196. , (void (STLSOFT_CDECL *)(HANDLE))&filesys_traits_t::close_handle // This cast required by VC++ 5
  197. , INVALID_HANDLE_VALUE);
  198. if(INVALID_HANDLE_VALUE == h.get())
  199. {
  200. STLSOFT_THROW_X(windows_exception("File does not exist", ::GetLastError()));
  201. }
  202. ws_uint64_t size = filesys_traits_t::get_file_size(h.get());
  203. if( 0 != size &&
  204. static_cast<ws_uint64_t>(~0) != size)
  205. {
  206. if(size > 0xFFFFFFFF)
  207. {
  208. STLSOFT_THROW_X(winstl_ns_qual_std(out_of_range)("Cannot read in files larger than 4GB"));
  209. }
  210. else
  211. {
  212. // TODO: Catch the out-of-memory exception and translate to a std::out_of_range()
  213. typedef ::stlsoft::auto_buffer_old< char_2_type
  214. , processheap_allocator<char_2_type>
  215. , 1024
  216. > buffer_t;
  217. buffer_t buffer(static_cast<ss_typename_type_k buffer_t::size_type>(size));
  218. DWORD dw;
  219. if(!::ReadFile(h.get(), &buffer[0], buffer.size(), &dw, NULL))
  220. {
  221. STLSOFT_THROW_X(windows_exception("Read operation failed", ::GetLastError()));
  222. }
  223. else
  224. {
  225. contents.assign(&buffer[0], dw);
  226. return size;
  227. }
  228. }
  229. }
  230. return 0;
  231. }
  232. #if !defined(STLSOFT_COMPILER_IS_DMC) && \
  233. !defined(STLSOFT_COMPILER_IS_MWERKS) && \
  234. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  235. _MSC_VER != 1300)
  236. template< ss_typename_param_k S2
  237. >
  238. inline
  239. ws_uint64_t
  240. load_text_file(
  241. ws_char_a_t const* fileName
  242. , S2& contents
  243. )
  244. {
  245. return load_text_file_impl<ws_char_a_t const*, S2>(fileName, contents);
  246. }
  247. template< ss_typename_param_k S2
  248. >
  249. inline
  250. ws_uint64_t
  251. load_text_file(
  252. ws_char_w_t const* fileName
  253. , S2& contents
  254. )
  255. {
  256. return load_text_file_impl<ws_char_w_t const*, S2>(fileName, contents);
  257. }
  258. #if 0
  259. template< ss_typename_param_k S2
  260. >
  261. inline
  262. ws_uint64_t
  263. load_text_file(
  264. char const fileName[]
  265. , S2& contents
  266. )
  267. {
  268. return load_text_file_impl<ws_char_a_t const*, S2>(fileName, contents);
  269. }
  270. template< ss_typename_param_k S2
  271. >
  272. inline
  273. ws_uint64_t
  274. load_text_file(
  275. ws_char_w_t* fileName
  276. , S2& contents
  277. )
  278. {
  279. return load_text_file_impl<ws_char_w_t const*, S2>(fileName, contents);
  280. }
  281. #endif /* 0 */
  282. #endif /* compiler */
  283. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  284. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  285. #if 0
  286. template< ss_typename_param_k S
  287. >
  288. struct trim_trailing_carriage_return
  289. {
  290. public:
  291. S operator ()(S const& s)
  292. {
  293. ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  294. if( len > 0 &&
  295. '\r' == s[len])
  296. {
  297. return s;
  298. }
  299. return S(stlsoft_ns_qual(c_str_ptr)(s), len - 1);
  300. }
  301. };
  302. #endif /* 0 */
  303. template< ss_typename_param_k CH
  304. , ss_typename_param_k C
  305. >
  306. void readlines_impl(CH const* p, ss_size_t len, C &container)
  307. {
  308. typedef CH char_t;
  309. typedef ss_typename_type_k C::value_type value_t;
  310. char_t const* p0 = p;
  311. char_t const* p1 = p0;
  312. char_t const* end = p + len;
  313. while(end != stlsoft_ns_qual(find_next_token)(p0, p1, end, static_cast<char_t>('\n')))
  314. {
  315. if( p1 > p0 &&
  316. '\r' == p1[-1])
  317. {
  318. --p1;
  319. }
  320. container.push_back(value_t(p0, static_cast<ws_size_t>(p1 - p0)));
  321. if('\r' == *p1)
  322. {
  323. ++p1;
  324. }
  325. }
  326. }
  327. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  328. /** \brief Reads the lines of a text-file into a sequence container
  329. *
  330. * \ingroup group__library__filesystem
  331. *
  332. * \param fileName The name of the text-file to load
  333. * \param container Reference to the sequence container to which each line
  334. * read from \c fileName will be appended (via its push_back() method)
  335. *
  336. * \returns The \c container reference
  337. *
  338. \code
  339. std::vector<std::string> lines;
  340. winstl::readlines("mytextfile.ext", lines);
  341. \endcode
  342. *
  343. *
  344. * \remarks The container type's <code>value_type</code> must provide
  345. * a two-parameter constructor whose parameters types are
  346. * <code>char_type const*</code> (where <code>char_type</code> is the
  347. * <code>value_type</code> of the string type <code>S</code>) and
  348. * <code>size_t</code>, indicating the pointer to the beginning and the
  349. * length of the C-style string that represents the line read from the
  350. * file denoted by <code>fileName</code>.
  351. *
  352. * \note When used with a compiler that does not support partial template
  353. * specialisation, the use of string types for which explicit
  354. * specialisations are not defined will fail. Hence, using
  355. * <code>stlsoft::simple_string</code> (which is the specialisation
  356. * <code>stlsoft::basic_simple_string&lt;char></code>) will succeed
  357. * because a specialisation of <code>stlsoft::string_traits</code> exists
  358. * for that type. The same applies for
  359. * <code>stlsoft::simple_wstring</code>, <code>std::string</code> and
  360. * <code>std::wstring</code>. However, if you attempt to use a
  361. * specialisation of a string class template for which an explicit
  362. * specialisation of <code>stlsoft::string_traits</code> does not exist
  363. * then you will experience a compile-time error in the implementation
  364. * of <code>winstl::load_text_file_impl()</code>. To correct this, you
  365. * must either provide an explicit specialisation of
  366. * <code>stlsoft::string_traits</code> for your type, or use a type for
  367. * which a specialisation of <code>stlsoft::string_traits</code> does
  368. * exist.<br><br>This problem does not occur for compilers that support
  369. * partial template specialisation.
  370. */
  371. template< ss_typename_param_k S
  372. , ss_typename_param_k C
  373. >
  374. C&
  375. readlines(
  376. S const& fileName
  377. , C& container
  378. )
  379. {
  380. S contents;
  381. S delim;
  382. // NOTE: doing these as characters skips the issue of ANSI vs Unicode
  383. delim.append(1, '\n');
  384. load_text_file(fileName, contents);
  385. #if 0
  386. stlsoft::string_tokeniser< S /* stlsoft::basic_string_view<ss_typename_type_k stlsoft::string_traits<S>::char_type> */
  387. , S
  388. , stlsoft::string_tokeniser_ignore_blanks<false>
  389. > tokens(contents, delim);
  390. std::transform(tokens.begin(), tokens.end(), std::back_inserter(container), trim_trailing_carriage_return</* ss_typename_type_k */ C::value_type>());
  391. #else /* ? 0 */
  392. readlines_impl(contents.c_str(), contents.size(), container);
  393. #endif /* 0 */
  394. return container;
  395. }
  396. /* /////////////////////////////////////////////////////////////////////////
  397. * Unit-testing
  398. */
  399. #ifdef STLSOFT_UNITTEST
  400. # include "./unittest/file_functions_unittest_.h"
  401. #endif /* STLSOFT_UNITTEST */
  402. /* ////////////////////////////////////////////////////////////////////// */
  403. #ifndef _WINSTL_NO_NAMESPACE
  404. # if defined(_STLSOFT_NO_NAMESPACE) || \
  405. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  406. } // namespace winstl
  407. # else
  408. } // namespace winstl_project
  409. } // namespace stlsoft
  410. # endif /* _STLSOFT_NO_NAMESPACE */
  411. #endif /* !_WINSTL_NO_NAMESPACE */
  412. /* ////////////////////////////////////////////////////////////////////// */
  413. #endif /* !WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS */
  414. /* ///////////////////////////// end of file //////////////////////////// */