]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/quickbook/src/native_text.cpp
1 /*=============================================================================
2 Copyright (c) 2009 Daniel James
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 =============================================================================*/
9 #include <boost/program_options.hpp>
11 #include "native_text.hpp"
15 #if QUICKBOOK_WIDE_PATHS || QUICKBOOK_WIDE_STREAMS
16 #include <boost/scoped_ptr.hpp>
22 #if QUICKBOOK_CYGWIN_PATHS
23 #include <boost/scoped_array.hpp>
24 #include <boost/program_options/errors.hpp>
25 #include <sys/cygwin.h>
31 bool ms_errors
= false;
34 void set_ms_errors(bool x
) {
38 // This is used for converting paths to UTF-8 on cygin.
39 // Might be better not to use a windows
40 #if QUICKBOOK_WIDE_PATHS || QUICKBOOK_WIDE_STREAMS
42 std::string
to_utf8(std::wstring
const& x
)
44 int buffer_count
= WideCharToMultiByte(CP_UTF8
, 0, x
.c_str(), -1, 0, 0, 0, 0);
47 throw conversion_error("Error converting wide string to utf-8.");
49 boost::scoped_ptr
<char> buffer(new char[buffer_count
]);
51 if (!WideCharToMultiByte(CP_UTF8
, 0, x
.c_str(), -1, buffer
.get(), buffer_count
, 0, 0))
52 throw conversion_error("Error converting wide string to utf-8.");
54 return std::string(buffer
.get());
57 std::wstring
from_utf8(boost::string_ref text
)
59 std::string
x(text
.begin(), text
.end());
60 int buffer_count
= MultiByteToWideChar(CP_UTF8
, 0, x
.c_str(), -1, 0, 0);
63 throw conversion_error("Error converting utf-8 to wide string.");
65 boost::scoped_ptr
<wchar_t> buffer(new wchar_t[buffer_count
]);
67 if (!MultiByteToWideChar(CP_UTF8
, 0, x
.c_str(), -1, buffer
.get(), buffer_count
))
68 throw conversion_error("Error converting utf-8 to wide string.");
70 return std::wstring(buffer
.get());
75 #if QUICKBOOK_WIDE_PATHS
76 std::string
command_line_to_utf8(command_line_string
const& x
)
81 std::string
command_line_to_utf8(command_line_string
const& x
)
87 #if QUICKBOOK_WIDE_PATHS
88 fs::path
generic_to_path(boost::string_ref x
)
90 return fs::path(from_utf8(x
));
93 std::string
path_to_generic(fs::path
const& x
)
95 return to_utf8(x
.generic_wstring());
98 fs::path
generic_to_path(boost::string_ref x
)
100 return fs::path(x
.begin(), x
.end());
103 std::string
path_to_generic(fs::path
const& x
)
105 return x
.generic_string();
111 #if QUICKBOOK_CYGWIN_PATHS
112 fs::path
command_line_to_path(command_line_string
const& path
)
114 cygwin_conv_path_t flags
= CCP_POSIX_TO_WIN_W
| CCP_RELATIVE
;
116 ssize_t size
= cygwin_conv_path(flags
, path
.c_str(), NULL
, 0);
119 throw conversion_error("Error converting cygwin path to windows.");
121 boost::scoped_array
<char> result(new char[size
]);
122 void* ptr
= result
.get();
124 if(cygwin_conv_path(flags
, path
.c_str(), ptr
, size
))
125 throw conversion_error("Error converting cygwin path to windows.");
127 return fs::path(static_cast<wchar_t*>(ptr
));
130 ostream::string
path_to_stream(fs::path
const& path
)
132 cygwin_conv_path_t flags
= CCP_WIN_W_TO_POSIX
| CCP_RELATIVE
;
134 ssize_t size
= cygwin_conv_path(flags
, path
.native().c_str(), NULL
, 0);
137 throw conversion_error("Error converting windows path to cygwin.");
139 boost::scoped_array
<char> result(new char[size
]);
141 if(cygwin_conv_path(flags
, path
.native().c_str(), result
.get(), size
))
142 throw conversion_error("Error converting windows path to cygwin.");
144 return std::string(result
.get());
147 fs::path
command_line_to_path(command_line_string
const& path
)
149 return fs::path(path
);
152 #if QUICKBOOK_WIDE_PATHS && !QUICKBOOK_WIDE_STREAMS
153 ostream::string
path_to_stream(fs::path
const& path
)
155 return path
.string();
158 ostream::string
path_to_stream(fs::path
const& path
)
160 return path
.native();
164 #endif // QUICKBOOK_CYGWIN_PATHS
166 #if QUICKBOOK_WIDE_STREAMS
168 void initialise_output()
170 if (_isatty(_fileno(stdout
))) _setmode(_fileno(stdout
), _O_U16TEXT
);
171 if (_isatty(_fileno(stderr
))) _setmode(_fileno(stderr
), _O_U16TEXT
);
174 void write_utf8(ostream::base_ostream
& out
, boost::string_ref x
)
181 static ostream
x(std::wcout
);
187 inline ostream
& error_stream()
189 static ostream
x(std::wcerr
);
196 void initialise_output()
200 void write_utf8(ostream::base_ostream
& out
, boost::string_ref x
)
207 static ostream
x(std::cout
);
213 inline ostream
& error_stream()
215 static ostream
x(std::clog
);
224 return error_stream() << "Error: ";
227 ostream
& outerr(fs::path
const& file
, int line
)
232 return error_stream() << path_to_stream(file
) << "(" << line
<< "): error: ";
234 return error_stream() << path_to_stream(file
) << ":" << line
<< ": error: ";
238 return error_stream() << path_to_stream(file
) << ": error: ";
242 ostream
& outerr(file_ptr
const& f
, string_iterator pos
)
244 return outerr(f
->path
, f
->position_of(pos
).line
);
247 ostream
& outwarn(fs::path
const& file
, int line
)
252 return error_stream() << path_to_stream(file
) << "(" << line
<< "): warning: ";
254 return error_stream() << path_to_stream(file
) << ":" << line
<< ": warning: ";
258 return error_stream() << path_to_stream(file
) << ": warning: ";
262 ostream
& outwarn(file_ptr
const& f
, string_iterator pos
)
264 return outwarn(f
->path
, f
->position_of(pos
).line
);
267 ostream
& ostream::operator<<(char c
) {
268 assert(c
> 0 && c
<= 127);
273 inline bool check_ascii(char const* x
) {
274 for(;*x
;++x
) if(*x
<= 0 || *x
> 127) return false;
278 ostream
& ostream::operator<<(char const* x
) {
279 assert(check_ascii(x
));
284 ostream
& ostream::operator<<(std::string
const& x
) {
289 ostream
& ostream::operator<<(boost::string_ref x
) {
294 ostream
& ostream::operator<<(int x
) {
299 ostream
& ostream::operator<<(unsigned int x
) {
304 ostream
& ostream::operator<<(long x
) {
309 ostream
& ostream::operator<<(unsigned long x
) {
314 #if !defined(BOOST_NO_LONG_LONG)
315 ostream
& ostream::operator<<(long long x
) {
320 ostream
& ostream::operator<<(unsigned long long x
) {
326 ostream
& ostream::operator<<(fs::path
const& x
) {
327 base
<< path_to_stream(x
);
331 ostream
& ostream::operator<<(base_ostream
& (*x
)(base_ostream
&)) {
336 ostream
& ostream::operator<<(base_ios
& (*x
)(base_ios
&)) {