1 //  filesystem windows_file_codecvt.cpp  -----------------------------------------//
2 
3 //  Copyright Beman Dawes 2009
4 
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See http://www.boost.org/LICENSE_1_0.txt
7 
8 //  Library home page: http://www.boost.org/libs/filesystem
9 
10 //--------------------------------------------------------------------------------------//
11 
12 // Include Boost.Predef first so that windows.h is guaranteed to be not included
13 #include <boost/predef/os/windows.h>
14 #if BOOST_OS_WINDOWS
15 #include <boost/winapi/config.hpp>
16 #endif
17 
18 #ifndef BOOST_SYSTEM_NO_DEPRECATED
19 # define BOOST_SYSTEM_NO_DEPRECATED
20 #endif
21 
22 #include <boost/filesystem/config.hpp>
23 #include <cwchar>  // for mbstate_t
24 
25 #ifdef BOOST_WINDOWS_API
26 
27 #include "windows_file_codecvt.hpp"
28 
29 // Versions of MinGW prior to GCC 4.6 requires this
30 #ifndef WINVER
31 # define WINVER 0x0500
32 #endif
33 
34 #include <windows.h>
35 
do_in(std::mbstate_t &,const char * from,const char * from_end,const char * & from_next,wchar_t * to,wchar_t * to_end,wchar_t * & to_next) const36   std::codecvt_base::result windows_file_codecvt::do_in(
37     std::mbstate_t &,
38     const char* from, const char* from_end, const char*& from_next,
39     wchar_t* to, wchar_t* to_end, wchar_t*& to_next) const
40   {
41     UINT codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
42 
43     int count;
44     if ((count = ::MultiByteToWideChar(codepage, MB_PRECOMPOSED, from,
45       static_cast<int>(from_end - from), to, static_cast<int>(to_end - to))) == 0)
46     {
47       return error;  // conversion failed
48     }
49 
50     from_next = from_end;
51     to_next = to + count;
52     *to_next = L'\0';
53     return ok;
54  }
55 
do_out(std::mbstate_t &,const wchar_t * from,const wchar_t * from_end,const wchar_t * & from_next,char * to,char * to_end,char * & to_next) const56   std::codecvt_base::result windows_file_codecvt::do_out(
57     std::mbstate_t &,
58     const wchar_t* from, const wchar_t* from_end, const wchar_t*  & from_next,
59     char* to, char* to_end, char* & to_next) const
60   {
61     UINT codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
62 
63     int count;
64     if ((count = ::WideCharToMultiByte(codepage, WC_NO_BEST_FIT_CHARS, from,
65       static_cast<int>(from_end - from), to, static_cast<int>(to_end - to), 0, 0)) == 0)
66     {
67       return error;  // conversion failed
68     }
69 
70     from_next = from_end;
71     to_next = to + count;
72     *to_next = '\0';
73     return ok;
74   }
75 
76   # endif  // BOOST_WINDOWS_API
77 
78