00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef PLATFORMSTL_INCL_PLATFORMSTL_FILESYSTEM_HPP_FILE_LINES
00043 #define PLATFORMSTL_INCL_PLATFORMSTL_FILESYSTEM_HPP_FILE_LINES
00044
00045
00046 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00047 # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_FILE_LINES_MAJOR 1
00048 # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_FILE_LINES_MINOR 4
00049 # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_FILE_LINES_REVISION 1
00050 # define PLATFORMSTL_VER_PLATFORMSTL_FILESYSTEM_HPP_FILE_LINES_EDIT 18
00051 #endif
00052
00064
00065
00066
00067
00068 #ifndef PLATFORMSTL_INCL_PLATFORMSTL_HPP_PLATFORMSTL
00069 # include <platformstl/platformstl.hpp>
00070 #endif
00071 #ifndef PLATFORMSTL_INCL_PLATFORMSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE
00072 # include <platformstl/filesystem/memory_mapped_file.hpp>
00073 #endif
00074 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST
00075 # include <stlsoft/conversion/truncation_cast.hpp>
00076 #endif
00077 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
00078 # include <stlsoft/shims/access/string/fwd.h>
00079 #endif
00080 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TOKENISER
00081 # include <stlsoft/string/string_tokeniser.hpp>
00082 #endif
00083 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW
00084 # include <stlsoft/string/string_view.hpp>
00085 #endif
00086 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
00087 # include <stlsoft/string/simple_string.hpp>
00088 #endif
00089
00090 #ifndef STLSOFT_INCL_ALGORITHM
00091 # define STLSOFT_INCL_ALGORITHM
00092 # include <algorithm>
00093 #endif
00094 #ifndef STLSOFT_INCL_VECTOR
00095 # define STLSOFT_INCL_VECTOR
00096 # include <vector>
00097 #endif
00098
00099 #ifdef STLSOFT_UNITTEST
00100 # include <platformstl/filesystem/file_path_buffer.hpp>
00101 #endif
00102
00103
00104
00105
00106
00107 #if defined(_STLSOFT_NO_NAMESPACE) || \
00108 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00109
00110 namespace platformstl
00111 {
00112 #else
00113
00114
00115 namespace stlsoft
00116 {
00117
00118 namespace platformstl_project
00119 {
00120 #endif
00121
00122
00123
00124
00125
00128 template< typename C
00129 , typename V = stlsoft::basic_string_view<C>
00130 , typename B = stlsoft::basic_simple_string<C>
00131 >
00132 class basic_file_lines
00133 {
00136 public:
00137 typedef basic_file_lines<C, V, B> class_type;
00138 typedef C char_type;
00139 private:
00140 typedef B base_string_type_;
00141 typedef std::vector<V> strings_type_;
00142 public:
00143 typedef ss_typename_type_k strings_type_::value_type value_type;
00144 typedef ss_typename_type_k strings_type_::size_type size_type;
00145 typedef ss_typename_type_k strings_type_::const_iterator const_iterator;
00146 typedef ss_typename_type_k strings_type_::const_reference const_reference;
00147 typedef ss_bool_t bool_type;
00149
00152 public:
00153 template <typename S>
00154 basic_file_lines(S const& path)
00155 : m_mmf(NULL)
00156 , m_contents()
00157 , m_strings()
00158 {
00159 m_mmf = (new memory_mapped_file(stlsoft_ns_qual(c_str_ptr)(path)));
00160
00161 #ifdef STLSOFT_CF_THROW_BAD_ALLOC
00162 STLSOFT_ASSERT(NULL != m_mmf);
00163 #else
00164 if(NULL != m_mmf)
00165 #endif
00166 {
00167 memory_mapped_file& mmf = *m_mmf;
00168
00169 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00170 m_strings.reserve(stlsoft_ns_qual(truncation_cast)<size_type>(mmf.size() / 40));
00171 #else
00172 m_strings.reserve(static_cast<size_type>(mmf.size() / 40));
00173 #endif
00174
00175 #if defined(PLATFORMSTL_OS_IS_WINDOWS) || \
00176 ( defined(PLATFORMSTL_OS_IS_UNIX) && \
00177 defined(_WIN32))
00178
00179 typedef stlsoft::string_tokeniser<
00180 value_type
00181 , value_type
00182 , stlsoft::skip_blank_tokens<false>
00183 , value_type
00184 > tokeniser_t_;
00185
00186 static const char_type sep[] = { '\r', '\n', '\0' };
00187
00188 #elif defined(PLATFORMSTL_OS_IS_UNIX)
00189
00190 typedef stlsoft::string_tokeniser<
00191 value_type
00192 , char
00193 , stlsoft::skip_blank_tokens<false>
00194 , value_type
00195 > tokeniser_t_;
00196
00197 static const char_type sep = '\n';
00198
00199 #else
00200 # error Platform not discriminated
00201 #endif
00202
00203 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00204 m_contents.assign(static_cast<C const*>(mmf.memory()), stlsoft_ns_qual(truncation_cast)<size_type>(mmf.size()));
00205 #else
00206 m_contents.assign(static_cast<C const*>(mmf.memory()), static_cast<size_type>(mmf.size()));
00207 #endif
00208
00209 tokeniser_t_ lines(m_contents.data(), m_contents.size(), sep);
00210
00211 std::copy(lines.begin(), lines.end(), std::back_inserter(m_strings));
00212
00213
00214
00215
00216
00217
00218 if( m_strings.empty() ||
00219 m_strings.front().data() != static_cast<C const*>(mmf.memory()))
00220 {
00221 delete m_mmf;
00222 m_mmf = NULL;
00223 }
00224 }
00225 }
00226
00227 ~basic_file_lines() stlsoft_throw_0()
00228 {
00229 delete m_mmf;
00230 }
00232
00235 public:
00237 size_type size() const
00238 {
00239 return m_strings.size();
00240 }
00241
00243 bool_type empty() const
00244 {
00245 return 0u == size();
00246 }
00247
00251 const_reference operator [](size_type index) const
00252 {
00253 STLSOFT_MESSAGE_ASSERT("index out of range", index < size());
00254
00255 return m_strings[index];
00256 }
00257
00261 const_iterator begin() const
00262 {
00263 return m_strings.begin();
00264 }
00265
00269 const_iterator end() const
00270 {
00271 return m_strings.end();
00272 }
00274
00277 public:
00278 #if !defined(STLSOFT_COMPILER_IS_DMC)
00279 template< ss_typename_param_k V2
00280 , ss_typename_param_k B2
00281 >
00282 bool_type equal(basic_file_lines<char_type, V2, B2> const& rhs) const
00283 #else
00284 bool_type equal(class_type const& rhs) const
00285 #endif
00286 {
00287 if(size() != rhs.size())
00288 {
00289 return false;
00290 }
00291
00292 return stlsoft_ns_qual_std(equal)(begin(), end(), rhs.begin());
00293 }
00294
00296
00299 private:
00300 memory_mapped_file* m_mmf;
00301 base_string_type_ m_contents;
00302 strings_type_ m_strings;
00304
00307 private:
00308 basic_file_lines(class_type const& );
00309 class_type& operator =(class_type const& );
00311 };
00312
00313
00314
00315
00316
00321 typedef basic_file_lines<ss_char_a_t> file_lines_a;
00322
00327 typedef basic_file_lines<ss_char_w_t> file_lines_w;
00328
00329 #ifdef TCHAR
00330
00334 typedef basic_file_lines<TCHAR> file_lines;
00335 #else
00336 typedef file_lines_a file_lines;
00337 #endif
00338
00343 typedef file_lines_w wfile_lines;
00344
00345
00346
00347
00348
00349 #if !defined(STLSOFT_COMPILER_IS_DMC)
00350 template< ss_typename_param_k C
00351 , ss_typename_param_k V1
00352 , ss_typename_param_k B1
00353 , ss_typename_param_k V2
00354 , ss_typename_param_k B2
00355 >
00356 inline ss_typename_type_ret_k basic_file_lines<C, V1, B1>::bool_type operator ==(basic_file_lines<C, V1, B1> const& lhs, basic_file_lines<C, V2, B2> const& rhs)
00357 #else
00358 template< ss_typename_param_k C
00359 , ss_typename_param_k V
00360 , ss_typename_param_k B
00361 >
00362 inline ss_typename_type_ret_k basic_file_lines<C, V, B>::bool_type operator ==(basic_file_lines<C, V, B> const& lhs, basic_file_lines<C, V, B> const& rhs)
00363 #endif
00364 {
00365 return lhs.equal(rhs);
00366 }
00367
00368 #if !defined(STLSOFT_COMPILER_IS_DMC)
00369 template< ss_typename_param_k C
00370 , ss_typename_param_k V1
00371 , ss_typename_param_k B1
00372 , ss_typename_param_k V2
00373 , ss_typename_param_k B2
00374 >
00375 inline ss_typename_type_ret_k basic_file_lines<C, V1, B1>::bool_type operator !=(basic_file_lines<C, V1, B1> const& lhs, basic_file_lines<C, V2, B2> const& rhs)
00376 #else
00377 template< ss_typename_param_k C
00378 , ss_typename_param_k V
00379 , ss_typename_param_k B
00380 >
00381 inline ss_typename_type_ret_k basic_file_lines<C, V, B>::bool_type operator !=(basic_file_lines<C, V, B> const& lhs, basic_file_lines<C, V, B> const& rhs)
00382 #endif
00383 {
00384 return !lhs.equal(rhs);
00385 }
00386
00387
00388
00389
00390
00391 #ifdef STLSOFT_UNITTEST
00392 # include "./unittest/file_lines_unittest_.h"
00393 #endif
00394
00395
00396
00397 #if defined(_STLSOFT_NO_NAMESPACE) || \
00398 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00399 }
00400 #else
00401 }
00402 }
00403 #endif
00404
00405
00406
00407 #endif
00408
00409