• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "native_text.hpp"
10 #include <iostream>
11 #include <boost/program_options.hpp>
12 #include "utils.hpp"
13 
14 #if QUICKBOOK_WIDE_PATHS || QUICKBOOK_WIDE_STREAMS
15 #include <windows.h>
16 #include <boost/scoped_array.hpp>
17 #endif
18 
19 namespace quickbook
20 {
21     namespace detail
22     {
23 // This is used for converting paths to UTF-8 on cygin.
24 // Might be better not to use a windows
25 #if QUICKBOOK_WIDE_PATHS || QUICKBOOK_WIDE_STREAMS
to_utf8(std::wstring const & x)26         std::string to_utf8(std::wstring const& x)
27         {
28             int buffer_count =
29                 WideCharToMultiByte(CP_UTF8, 0, x.c_str(), -1, 0, 0, 0, 0);
30 
31             if (!buffer_count)
32                 throw conversion_error(
33                     "Error converting wide string to utf-8.");
34 
35             boost::scoped_array<char> buffer(new char[buffer_count]);
36 
37             if (!WideCharToMultiByte(
38                     CP_UTF8, 0, x.c_str(), -1, buffer.get(), buffer_count, 0,
39                     0))
40                 throw conversion_error(
41                     "Error converting wide string to utf-8.");
42 
43             return std::string(buffer.get());
44         }
45 
from_utf8(quickbook::string_view text)46         std::wstring from_utf8(quickbook::string_view text)
47         {
48             std::string x(text.begin(), text.end());
49             int buffer_count =
50                 MultiByteToWideChar(CP_UTF8, 0, x.c_str(), -1, 0, 0);
51 
52             if (!buffer_count)
53                 throw conversion_error(
54                     "Error converting utf-8 to wide string.");
55 
56             boost::scoped_array<wchar_t> buffer(new wchar_t[buffer_count]);
57 
58             if (!MultiByteToWideChar(
59                     CP_UTF8, 0, x.c_str(), -1, buffer.get(), buffer_count))
60                 throw conversion_error(
61                     "Error converting utf-8 to wide string.");
62 
63             return std::wstring(buffer.get());
64         }
65 #endif
66     }
67 }
68