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// define BOOST_FILESYSTEM_SOURCE so that <boost/system/config.hpp> knows
13// the library is being built (possibly exporting rather than importing code)
14#define BOOST_FILESYSTEM_SOURCE
15
16#ifndef BOOST_SYSTEM_NO_DEPRECATED
17# define BOOST_SYSTEM_NO_DEPRECATED
18#endif
19
20#include <boost/filesystem/config.hpp>
21#include <cwchar> // for mbstate_t
22
23#ifdef BOOST_WINDOWS_API
24
25#include "windows_file_codecvt.hpp"
26
27// Versions of MinGW prior to GCC 4.6 requires this
28#ifndef WINVER
29# define WINVER 0x0500
30#endif
31
32#include <windows.h>
33
34 std::codecvt_base::result windows_file_codecvt::do_in(
35 std::mbstate_t &,
36 const char* from, const char* from_end, const char*& from_next,
37 wchar_t* to, wchar_t* to_end, wchar_t*& to_next) const
38 {
39 UINT codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
40
41 int count;
42 if ((count = ::MultiByteToWideChar(codepage, MB_PRECOMPOSED, from,
43 static_cast<int>(from_end - from), to, static_cast<int>(to_end - to))) == 0)
44 {
45 return error; // conversion failed
46 }
47
48 from_next = from_end;
49 to_next = to + count;
50 *to_next = L'\0';
51 return ok;
52 }
53
54 std::codecvt_base::result windows_file_codecvt::do_out(
55 std::mbstate_t &,
56 const wchar_t* from, const wchar_t* from_end, const wchar_t* & from_next,
57 char* to, char* to_end, char* & to_next) const
58 {
59 UINT codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
60
61 int count;
62 if ((count = ::WideCharToMultiByte(codepage, WC_NO_BEST_FIT_CHARS, from,
63 static_cast<int>(from_end - from), to, static_cast<int>(to_end - to), 0, 0)) == 0)
64 {
65 return error; // conversion failed
66 }
67
68 from_next = from_end;
69 to_next = to + count;
70 *to_next = '\0';
71 return ok;
72 }
73
74 # endif // BOOST_WINDOWS_API
75
76