]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2009 Daniel James | |
3 | ||
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 at | |
6 | http://www.boost.org/LICENSE_1_0.txt) | |
7 | =============================================================================*/ | |
8 | ||
9 | #include <boost/program_options.hpp> | |
10 | #include <iostream> | |
11 | #include "native_text.hpp" | |
12 | #include "utils.hpp" | |
7c673cae FG |
13 | |
14 | #if QUICKBOOK_WIDE_PATHS || QUICKBOOK_WIDE_STREAMS | |
15 | #include <boost/scoped_ptr.hpp> | |
16 | #include <windows.h> | |
7c673cae FG |
17 | #endif |
18 | ||
19 | namespace quickbook { | |
20 | namespace detail { | |
7c673cae FG |
21 | // This is used for converting paths to UTF-8 on cygin. |
22 | // Might be better not to use a windows | |
23 | #if QUICKBOOK_WIDE_PATHS || QUICKBOOK_WIDE_STREAMS | |
b32b8144 | 24 | std::string to_utf8(std::wstring const& x) |
7c673cae | 25 | { |
b32b8144 | 26 | int buffer_count = WideCharToMultiByte(CP_UTF8, 0, x.c_str(), -1, 0, 0, 0, 0); |
7c673cae | 27 | |
b32b8144 FG |
28 | if (!buffer_count) |
29 | throw conversion_error("Error converting wide string to utf-8."); | |
7c673cae | 30 | |
b32b8144 | 31 | boost::scoped_ptr<char> buffer(new char[buffer_count]); |
7c673cae | 32 | |
b32b8144 FG |
33 | if (!WideCharToMultiByte(CP_UTF8, 0, x.c_str(), -1, buffer.get(), buffer_count, 0, 0)) |
34 | throw conversion_error("Error converting wide string to utf-8."); | |
35 | ||
36 | return std::string(buffer.get()); | |
7c673cae FG |
37 | } |
38 | ||
b32b8144 | 39 | std::wstring from_utf8(quickbook::string_view text) |
7c673cae | 40 | { |
b32b8144 FG |
41 | std::string x(text.begin(), text.end()); |
42 | int buffer_count = MultiByteToWideChar(CP_UTF8, 0, x.c_str(), -1, 0, 0); | |
43 | ||
44 | if (!buffer_count) | |
45 | throw conversion_error("Error converting utf-8 to wide string."); | |
7c673cae | 46 | |
b32b8144 | 47 | boost::scoped_ptr<wchar_t> buffer(new wchar_t[buffer_count]); |
7c673cae | 48 | |
b32b8144 FG |
49 | if (!MultiByteToWideChar(CP_UTF8, 0, x.c_str(), -1, buffer.get(), buffer_count)) |
50 | throw conversion_error("Error converting utf-8 to wide string."); | |
7c673cae | 51 | |
b32b8144 | 52 | return std::wstring(buffer.get()); |
7c673cae FG |
53 | } |
54 | #endif | |
7c673cae | 55 | }} |