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 
00047 #ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS
00048 #define WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS
00049 
00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00051 # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_MAJOR      2
00052 # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_MINOR      3
00053 # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_REVISION   8
00054 # define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_FUNCTIONS_EDIT       52
00055 #endif 
00056 
00057 
00058 
00059 
00060 
00061 #ifndef WINSTL_INCL_WINSTL_H_WINSTL
00062 # include <winstl/winstl.h>
00063 #endif 
00064 
00065 #ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
00066 # include <winstl/filesystem/filesystem_traits.hpp>
00067 #endif 
00068 #ifndef WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS
00069 # include <winstl/error/exceptions.hpp>
00070 #endif 
00071 #ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
00072 # include <winstl/memory/processheap_allocator.hpp>
00073 #endif 
00074 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
00075 # include <stlsoft/memory/auto_buffer.hpp>
00076 #endif 
00077 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
00078 # include <stlsoft/shims/access/string.hpp>
00079 #endif 
00080 #ifndef STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE
00081 # include <stlsoft/smartptr/scoped_handle.hpp>
00082 #endif 
00083 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS
00084 # include <stlsoft/string/string_traits.hpp>
00085 #endif 
00086 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS
00087 # include <stlsoft/string/tokeniser_functions.hpp> 
00088 #endif 
00089 
00090 #ifdef STLSOFT_UNITTEST
00091 # include <stlsoft/string/simple_string.hpp>
00092 #endif // STLSOFT_UNITTEST
00093 
00094 
00095 
00096 
00097 
00098 #ifndef _WINSTL_NO_NAMESPACE
00099 # if defined(_STLSOFT_NO_NAMESPACE) || \
00100      defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00101 
00102 namespace winstl
00103 {
00104 # else
00105 
00106 
00107 namespace stlsoft
00108 {
00109 
00110 namespace winstl_project
00111 {
00112 
00113 # endif 
00114 #endif 
00115 
00116 
00117 
00118 
00119 
00120 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00121 template<   ss_typename_param_k S1
00122         ,   ss_typename_param_k S2
00123         >
00124 inline ws_uint64_t load_text_file_impl(S1 const& fileName, S2 &contents);
00125 #endif 
00126 
00167 template<   ss_typename_param_k S1
00168         ,   ss_typename_param_k S2
00169         >
00170 inline ws_uint64_t load_text_file(S1 const& fileName, S2 &contents)
00171 {
00172     return load_text_file_impl<S1, S2>(fileName, contents);
00173 }
00174 
00175 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00176 template<   ss_typename_param_k S1
00177         ,   ss_typename_param_k S2
00178         >
00179 inline ws_uint64_t load_text_file_impl(S1 const& fileName, S2 &contents)
00180 {
00181     typedef string_traits<S1>                               string_traits_t;
00182 
00183     STLSOFT_STATIC_ASSERT(sizeof(string_traits_t));     
00184 
00185     typedef string_traits<S2>                               string_traits2_t;
00186 
00187     STLSOFT_STATIC_ASSERT(sizeof(string_traits2_t));    
00188 
00189     typedef ss_typename_type_k string_traits_t::char_type   C;
00190 
00191     STLSOFT_STATIC_ASSERT(sizeof(C));                   
00192 
00193     typedef ss_typename_type_k string_traits2_t::char_type  char_2_type;
00194 
00195     STLSOFT_STATIC_ASSERT(sizeof(char_2_type));         
00196 
00197     typedef filesystem_traits<C>                            filesys_traits_t;
00198 
00199     STLSOFT_STATIC_ASSERT(sizeof(filesys_traits_t));    
00200 
00201     scoped_handle<HANDLE>   h(  filesys_traits_t::create_file(  stlsoft_ns_qual(c_str_ptr)(fileName)
00202                                                             ,   GENERIC_READ
00203                                                             ,   FILE_SHARE_READ
00204                                                             ,   NULL
00205                                                             ,   OPEN_EXISTING
00206                                                             ,   0
00207                                                             ,   NULL)
00208                             ,   (void (STLSOFT_CDECL *)(HANDLE))&filesys_traits_t::close_handle 
00209                             ,   INVALID_HANDLE_VALUE);
00210 
00211     if(INVALID_HANDLE_VALUE == h.get())
00212     {
00213         STLSOFT_THROW_X(windows_exception("File does not exist", ::GetLastError()));
00214     }
00215 
00216     ws_uint64_t             size    =   filesys_traits_t::get_file_size(h.get());
00217 
00218     if( 0 != size &&
00219         static_cast<ws_uint64_t>(~0) != size)
00220     {
00221         if(size > 0xFFFFFFFF)
00222         {
00223             STLSOFT_THROW_X(winstl_ns_qual_std(out_of_range)("Cannot read in files larger than 4GB"));
00224         }
00225         else
00226         {
00227 
00228 
00229             typedef ::stlsoft::auto_buffer_old< char_2_type
00230                                             ,   processheap_allocator<char_2_type>
00231                                             ,   1024
00232                                             >                   buffer_t;
00233 
00234             buffer_t    buffer(static_cast<ss_typename_type_k buffer_t::size_type>(size));
00235             DWORD       dw;
00236 
00237             if(!::ReadFile(h.get(), &buffer[0], buffer.size(), &dw, NULL))
00238             {
00239                 STLSOFT_THROW_X(windows_exception("Read operation failed", ::GetLastError()));
00240             }
00241             else
00242             {
00243                 contents.assign(&buffer[0], dw);
00244 
00245                 return size;
00246             }
00247         }
00248     }
00249 
00250     return 0;
00251 }
00252 
00253 #if !defined(STLSOFT_COMPILER_IS_DMC) && \
00254     !defined(STLSOFT_COMPILER_IS_MWERKS) && \
00255     (   !defined(STLSOFT_COMPILER_IS_MSVC) || \
00256         _MSC_VER != 1300)
00257 
00258 template<   ss_typename_param_k S2
00259         >
00260 inline ws_uint64_t load_text_file(ws_char_a_t const* fileName, S2 &contents)
00261 {
00262     return load_text_file_impl<ws_char_a_t const*, S2>(fileName, contents);
00263 }
00264 
00265 template<   ss_typename_param_k S2
00266         >
00267 inline ws_uint64_t load_text_file(ws_char_w_t const* fileName, S2 &contents)
00268 {
00269     return load_text_file_impl<ws_char_w_t const*, S2>(fileName, contents);
00270 }
00271 
00272 #if 0
00273 template<   ss_typename_param_k S2
00274         >
00275 inline ws_uint64_t load_text_file(char fileName[], S2 &contents)
00276 {
00277     return load_text_file_impl<ws_char_a_t const*, S2>(fileName, contents);
00278 }
00279 
00280 template<   ss_typename_param_k S2
00281         >
00282 inline ws_uint64_t load_text_file(ws_char_w_t *fileName, S2 &contents)
00283 {
00284     return load_text_file_impl<ws_char_w_t const*, S2>(fileName, contents);
00285 }
00286 #endif 
00287 
00288 #endif 
00289 
00290 #endif 
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 
00308 #if 0
00309 template<   ss_typename_param_k S
00310         >
00311 struct trim_trailing_carriage_return
00312 {
00313 public:
00314     S operator ()(S const& s)
00315     {
00316         ss_size_t   len =   stlsoft_ns_qual(c_str_len)(s);
00317 
00318         if( len > 0 &&
00319             '\r' == s[len])
00320         {
00321             return s;
00322         }
00323 
00324         return S(stlsoft_ns_qual(c_str_ptr)(s), len - 1);
00325     }
00326 };
00327 #endif 
00328 
00329 template<   ss_typename_param_k CH
00330         ,   ss_typename_param_k C
00331         >
00332 void readlines_impl(CH const* p, ss_size_t len, C &container)
00333 {
00334     typedef CH                                  char_t;
00335     typedef ss_typename_type_k C::value_type    value_t;
00336 
00337     char_t const*   p0  =   p;
00338     char_t const*   p1  =   p0;
00339     char_t const*   end =   p + len;
00340 
00341     while(end != stlsoft_ns_qual(find_next_token)(p0, p1, end, static_cast<char_t>('\n')))
00342     {
00343         if( p1 > p0 &&
00344             '\r' == p1[-1])
00345         {
00346             --p1;
00347         }
00348 
00349         container.push_back(value_t(p0, static_cast<ws_size_t>(p1 - p0)));
00350 
00351         if('\r' == *p1)
00352         {
00353             ++p1;
00354         }
00355     }
00356 }
00357 
00402 template<   ss_typename_param_k S
00403         ,   ss_typename_param_k C
00404         >
00405 C &readlines(S const& fileName, C &container)
00406 {
00407     S   contents;
00408     S   delim;
00409 
00410     
00411     delim.append(1, '\n');
00412 
00413     load_text_file(fileName, contents);
00414 
00415 #if 0
00416     stlsoft::string_tokeniser<  S 
00417                             ,   S
00418                             ,   stlsoft::string_tokeniser_ignore_blanks<false>
00419                             >           tokens(contents, delim);
00420 
00421     std::transform(tokens.begin(), tokens.end(), std::back_inserter(container), trim_trailing_carriage_return< C::value_type>());
00422 #else 
00423 
00424     readlines_impl(contents.c_str(), contents.size(), container);
00425 #endif 
00426 
00427     return container;
00428 }
00429 
00430 
00431 
00432 
00433 
00434 
00435 #ifdef STLSOFT_UNITTEST
00436 # include "./unittest/file_functions_unittest_.h"
00437 #endif 
00438 
00439 
00440 
00441 #ifndef _WINSTL_NO_NAMESPACE
00442 # if defined(_STLSOFT_NO_NAMESPACE) || \
00443      defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00444 } 
00445 # else
00446 } 
00447 } 
00448 # endif 
00449 
00450 #endif 
00451 
00452 
00453 
00454 #endif 
00455 
00456