| 1 | // path.cpp ----------------------------------------------------------------// |
|---|
| 2 | |
|---|
| 3 | // Copyright 2005 Beman Dawes |
|---|
| 4 | // Use, modification, and distribution is subject to the Boost Software |
|---|
| 5 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy |
|---|
| 6 | // at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | |
|---|
| 8 | // See library home page at http://www.boost.org/libs/filesystem |
|---|
| 9 | |
|---|
| 10 | //----------------------------------------------------------------------------// |
|---|
| 11 | |
|---|
| 12 | // define BOOST_FILESYSTEM_SOURCE so that <boost/filesystem/config.hpp> knows |
|---|
| 13 | // the library is being built (possibly exporting rather than importing code) |
|---|
| 14 | #define BOOST_FILESYSTEM_SOURCE |
|---|
| 15 | |
|---|
| 16 | #include <boost/filesystem/config.hpp> |
|---|
| 17 | |
|---|
| 18 | #ifndef BOOST_FILESYSTEM_NARROW_ONLY |
|---|
| 19 | |
|---|
| 20 | #include <boost/filesystem/path.hpp> |
|---|
| 21 | #include <boost/scoped_array.hpp> |
|---|
| 22 | |
|---|
| 23 | #include <locale> |
|---|
| 24 | #include <cerrno> |
|---|
| 25 | |
|---|
| 26 | namespace |
|---|
| 27 | { |
|---|
| 28 | // std::locale construction can throw (if LC_MESSAGES is wrong, for example), |
|---|
| 29 | // so a static at function scope is used to ensure that exceptions can be |
|---|
| 30 | // caught. (A previous version was at namespace scope, so initialization |
|---|
| 31 | // occurred before main(), preventing exceptions from being caught.) |
|---|
| 32 | |
|---|
| 33 | std::locale & loc() |
|---|
| 34 | { |
|---|
| 35 | // ISO C calls this "the locale-specific native environment": |
|---|
| 36 | static std::locale lc(""); |
|---|
| 37 | return lc; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | const std::codecvt<wchar_t, char, std::mbstate_t> *& |
|---|
| 41 | converter() |
|---|
| 42 | { |
|---|
| 43 | static const std::codecvt<wchar_t, char, std::mbstate_t> * |
|---|
| 44 | cvtr( |
|---|
| 45 | &std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t> > |
|---|
| 46 | ( loc() ) ); |
|---|
| 47 | return cvtr; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | bool locked(false); |
|---|
| 51 | } // unnamed namespace |
|---|
| 52 | |
|---|
| 53 | namespace boost |
|---|
| 54 | { |
|---|
| 55 | namespace filesystem |
|---|
| 56 | { |
|---|
| 57 | bool wpath_traits::imbue( const std::locale & new_loc, const std::nothrow_t & ) |
|---|
| 58 | { |
|---|
| 59 | if ( locked ) return false; |
|---|
| 60 | locked = true; |
|---|
| 61 | loc() = new_loc; |
|---|
| 62 | converter() = &std::use_facet |
|---|
| 63 | <std::codecvt<wchar_t, char, std::mbstate_t> >( loc() ); |
|---|
| 64 | return true; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void wpath_traits::imbue( const std::locale & new_loc ) |
|---|
| 68 | { |
|---|
| 69 | if ( locked ) boost::throw_exception( filesystem_wpath_error( |
|---|
| 70 | "boost::filesystem::wpath_traits::imbue() after lockdown", 0 ) ); |
|---|
| 71 | imbue( new_loc, std::nothrow ); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | # ifdef BOOST_POSIX_API |
|---|
| 75 | |
|---|
| 76 | // Because this is POSIX only code, we don't have to worry about ABI issues |
|---|
| 77 | // described in http://www.boost.org/more/separate_compilation.html |
|---|
| 78 | |
|---|
| 79 | wpath_traits::external_string_type |
|---|
| 80 | wpath_traits::to_external( const wpath & ph, |
|---|
| 81 | const internal_string_type & src ) |
|---|
| 82 | { |
|---|
| 83 | locked = true; |
|---|
| 84 | std::size_t work_size( converter()->max_length() * (src.size()+1) ); |
|---|
| 85 | boost::scoped_array<char> work( new char[ work_size ] ); |
|---|
| 86 | std::mbstate_t state; |
|---|
| 87 | const internal_string_type::value_type * from_next; |
|---|
| 88 | external_string_type::value_type * to_next; |
|---|
| 89 | if ( converter()->out( |
|---|
| 90 | state, src.c_str(), src.c_str()+src.size(), from_next, work.get(), |
|---|
| 91 | work.get()+work_size, to_next ) != std::codecvt_base::ok ) |
|---|
| 92 | boost::throw_exception( boost::filesystem::filesystem_wpath_error( |
|---|
| 93 | "boost::filesystem::wpath::to_external conversion error", |
|---|
| 94 | ph, EINVAL ) ); |
|---|
| 95 | *to_next = '\0'; |
|---|
| 96 | return external_string_type( work.get() ); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | wpath_traits::internal_string_type |
|---|
| 100 | wpath_traits::to_internal( const external_string_type & src ) |
|---|
| 101 | { |
|---|
| 102 | locked = true; |
|---|
| 103 | std::size_t work_size( src.size()+1 ); |
|---|
| 104 | boost::scoped_array<wchar_t> work( new wchar_t[ work_size ] ); |
|---|
| 105 | std::mbstate_t state; |
|---|
| 106 | const external_string_type::value_type * from_next; |
|---|
| 107 | internal_string_type::value_type * to_next; |
|---|
| 108 | if ( converter()->in( |
|---|
| 109 | state, src.c_str(), src.c_str()+src.size(), from_next, work.get(), |
|---|
| 110 | work.get()+work_size, to_next ) != std::codecvt_base::ok ) |
|---|
| 111 | boost::throw_exception( boost::filesystem::filesystem_wpath_error( |
|---|
| 112 | "boost::filesystem::wpath::to_internal conversion error", EINVAL ) ); |
|---|
| 113 | *to_next = L'\0'; |
|---|
| 114 | return internal_string_type( work.get() ); |
|---|
| 115 | } |
|---|
| 116 | # endif // BOOST_POSIX_API |
|---|
| 117 | |
|---|
| 118 | } // namespace filesystem |
|---|
| 119 | } // namespace boost |
|---|
| 120 | |
|---|
| 121 | #endif // ifndef BOOST_FILESYSTEM_NARROW_ONLY |
|---|