]>
Commit | Line | Data |
---|---|---|
b32b8144 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 "stream.hpp" | |
b32b8144 | 10 | #include "files.hpp" |
11fdf7f2 | 11 | #include "path.hpp" |
b32b8144 FG |
12 | |
13 | #if QUICKBOOK_WIDE_PATHS || QUICKBOOK_WIDE_STREAMS | |
b32b8144 | 14 | #include <fcntl.h> |
11fdf7f2 | 15 | #include <io.h> |
b32b8144 FG |
16 | #endif |
17 | ||
11fdf7f2 TL |
18 | namespace quickbook |
19 | { | |
20 | namespace detail | |
21 | { | |
22 | namespace | |
23 | { | |
24 | bool ms_errors = false; | |
25 | } | |
b32b8144 | 26 | |
11fdf7f2 | 27 | void set_ms_errors(bool x) { ms_errors = x; } |
b32b8144 FG |
28 | |
29 | #if QUICKBOOK_WIDE_STREAMS | |
30 | ||
11fdf7f2 TL |
31 | void initialise_output() |
32 | { | |
33 | if (_isatty(_fileno(stdout))) _setmode(_fileno(stdout), _O_U16TEXT); | |
34 | if (_isatty(_fileno(stderr))) _setmode(_fileno(stderr), _O_U16TEXT); | |
35 | } | |
b32b8144 | 36 | |
11fdf7f2 TL |
37 | void write_utf8(ostream::base_ostream& out, quickbook::string_view x) |
38 | { | |
39 | out << from_utf8(x); | |
40 | } | |
b32b8144 | 41 | |
11fdf7f2 | 42 | ostream& out() |
b32b8144 | 43 | { |
11fdf7f2 | 44 | static ostream x(std::wcout); |
b32b8144 FG |
45 | return x; |
46 | } | |
b32b8144 | 47 | |
11fdf7f2 TL |
48 | namespace |
49 | { | |
50 | inline ostream& error_stream() | |
51 | { | |
52 | static ostream x(std::wcerr); | |
53 | return x; | |
54 | } | |
55 | } | |
b32b8144 | 56 | |
11fdf7f2 | 57 | #else |
b32b8144 | 58 | |
11fdf7f2 | 59 | void initialise_output() {} |
b32b8144 | 60 | |
11fdf7f2 TL |
61 | void write_utf8(ostream::base_ostream& out, quickbook::string_view x) |
62 | { | |
63 | out << x; | |
64 | } | |
b32b8144 | 65 | |
11fdf7f2 | 66 | ostream& out() |
b32b8144 | 67 | { |
11fdf7f2 | 68 | static ostream x(std::cout); |
b32b8144 FG |
69 | return x; |
70 | } | |
11fdf7f2 TL |
71 | |
72 | namespace | |
73 | { | |
74 | inline ostream& error_stream() | |
75 | { | |
76 | static ostream x(std::clog); | |
77 | return x; | |
78 | } | |
79 | } | |
b32b8144 FG |
80 | |
81 | #endif | |
82 | ||
11fdf7f2 | 83 | ostream& outerr() { return error_stream() << "Error: "; } |
b32b8144 | 84 | |
11fdf7f2 | 85 | ostream& outerr(fs::path const& file, std::ptrdiff_t line) |
b32b8144 | 86 | { |
11fdf7f2 TL |
87 | if (line >= 0) { |
88 | if (ms_errors) | |
89 | return error_stream() << path_to_stream(file) << "(" << line | |
90 | << "): error: "; | |
91 | else | |
92 | return error_stream() << path_to_stream(file) << ":" << line | |
93 | << ": error: "; | |
94 | } | |
95 | else { | |
96 | return error_stream() << path_to_stream(file) << ": error: "; | |
97 | } | |
b32b8144 | 98 | } |
11fdf7f2 TL |
99 | |
100 | ostream& outerr(file_ptr const& f, string_iterator pos) | |
b32b8144 | 101 | { |
11fdf7f2 | 102 | return outerr(f->path, f->position_of(pos).line); |
b32b8144 | 103 | } |
b32b8144 | 104 | |
11fdf7f2 | 105 | ostream& outwarn(fs::path const& file, std::ptrdiff_t line) |
b32b8144 | 106 | { |
11fdf7f2 TL |
107 | if (line >= 0) { |
108 | if (ms_errors) | |
109 | return error_stream() << path_to_stream(file) << "(" << line | |
110 | << "): warning: "; | |
111 | else | |
112 | return error_stream() << path_to_stream(file) << ":" << line | |
113 | << ": warning: "; | |
114 | } | |
115 | else { | |
116 | return error_stream() << path_to_stream(file) << ": warning: "; | |
117 | } | |
b32b8144 | 118 | } |
11fdf7f2 TL |
119 | |
120 | ostream& outwarn(file_ptr const& f, string_iterator pos) | |
b32b8144 | 121 | { |
11fdf7f2 | 122 | return outwarn(f->path, f->position_of(pos).line); |
b32b8144 | 123 | } |
b32b8144 | 124 | |
11fdf7f2 TL |
125 | ostream& ostream::operator<<(char c) |
126 | { | |
127 | assert(c && !(c & 0x80)); | |
128 | base << c; | |
129 | return *this; | |
130 | } | |
b32b8144 | 131 | |
11fdf7f2 TL |
132 | inline bool check_ascii(char const* x) |
133 | { | |
134 | for (; *x; ++x) | |
135 | if (*x & 0x80) return false; | |
136 | return true; | |
137 | } | |
b32b8144 | 138 | |
11fdf7f2 TL |
139 | ostream& ostream::operator<<(char const* x) |
140 | { | |
141 | assert(check_ascii(x)); | |
142 | base << x; | |
143 | return *this; | |
144 | } | |
b32b8144 | 145 | |
11fdf7f2 TL |
146 | ostream& ostream::operator<<(std::string const& x) |
147 | { | |
148 | write_utf8(base, x); | |
149 | return *this; | |
150 | } | |
b32b8144 | 151 | |
11fdf7f2 TL |
152 | ostream& ostream::operator<<(quickbook::string_view x) |
153 | { | |
154 | write_utf8(base, x); | |
155 | return *this; | |
156 | } | |
b32b8144 | 157 | |
11fdf7f2 TL |
158 | ostream& ostream::operator<<(int x) |
159 | { | |
160 | base << x; | |
161 | return *this; | |
162 | } | |
b32b8144 | 163 | |
11fdf7f2 TL |
164 | ostream& ostream::operator<<(unsigned int x) |
165 | { | |
166 | base << x; | |
167 | return *this; | |
168 | } | |
b32b8144 | 169 | |
11fdf7f2 TL |
170 | ostream& ostream::operator<<(long x) |
171 | { | |
172 | base << x; | |
173 | return *this; | |
174 | } | |
b32b8144 | 175 | |
11fdf7f2 TL |
176 | ostream& ostream::operator<<(unsigned long x) |
177 | { | |
178 | base << x; | |
179 | return *this; | |
180 | } | |
b32b8144 FG |
181 | |
182 | #if !defined(BOOST_NO_LONG_LONG) | |
11fdf7f2 TL |
183 | ostream& ostream::operator<<(boost::long_long_type x) |
184 | { | |
185 | base << x; | |
186 | return *this; | |
187 | } | |
b32b8144 | 188 | |
11fdf7f2 TL |
189 | ostream& ostream::operator<<(boost::ulong_long_type x) |
190 | { | |
191 | base << x; | |
192 | return *this; | |
193 | } | |
b32b8144 FG |
194 | #endif |
195 | ||
11fdf7f2 TL |
196 | ostream& ostream::operator<<(fs::path const& x) |
197 | { | |
198 | base << path_to_stream(x); | |
199 | return *this; | |
200 | } | |
b32b8144 | 201 | |
11fdf7f2 TL |
202 | ostream& ostream::operator<<(base_ostream& (*x)(base_ostream&)) |
203 | { | |
204 | base << x; | |
205 | return *this; | |
206 | } | |
b32b8144 | 207 | |
11fdf7f2 TL |
208 | ostream& ostream::operator<<(base_ios& (*x)(base_ios&)) |
209 | { | |
210 | base << x; | |
211 | return *this; | |
212 | } | |
b32b8144 | 213 | } |
11fdf7f2 | 214 | } |