17# include <ext/stdio_filebuf.h>
18# include <ext/stdio_sync_filebuf.h>
31struct file_access_tag {};
33template <
typename Tag,
typename BufType, FILE* BufType::*FileMemberPtr>
35 friend auto get_file(BufType& obj) -> FILE* {
return obj.*FileMemberPtr; }
39template class file_access<file_access_tag, std::filebuf,
40 &std::filebuf::_Myfile>;
41auto get_file(std::filebuf&) -> FILE*;
44inline auto write_ostream_unicode(std::ostream& os, fmt::string_view data)
48 if (
auto* buf =
dynamic_cast<std::filebuf*
>(os.rdbuf()))
52#elif defined(_WIN32) && defined(__GLIBCXX__)
53 auto* rdbuf = os.rdbuf();
54 if (
auto* sfbuf =
dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*
>(rdbuf))
56 else if (
auto* fbuf =
dynamic_cast<__gnu_cxx::stdio_filebuf<char>*
>(rdbuf))
61 ignore_unused(os, data, f);
68 return write_console(fd, data);
74inline auto write_ostream_unicode(std::wostream&,
75 fmt::basic_string_view<wchar_t>) ->
bool {
81template <
typename Char>
82void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
83 const Char* buf_data = buf.data();
84 using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
85 unsigned_streamsize size = buf.size();
86 unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
88 unsigned_streamsize n = size <= max_size ? size : max_size;
89 os.write(buf_data,
static_cast<std::streamsize
>(n));
95template <
typename Char,
typename T>
96void format_value(buffer<Char>& buf,
const T& value) {
97 auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
98 auto&& output = std::basic_ostream<Char>(&format_buf);
99#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
100 output.imbue(std::locale::classic());
103 output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
113template <
typename Char>
115 void set_debug_format() =
delete;
117 template <
typename T,
typename Context>
118 auto format(
const T& value, Context& ctx)
const ->
decltype(ctx.out()) {
120 detail::format_value(buffer, value);
122 {buffer.data(), buffer.size()}, ctx);
128template <
typename T,
typename Char>
131 template <
typename Context>
133 ->
decltype(ctx.out()) {
155inline void vprint_directly(std::ostream& os,
string_view format_str,
158 detail::vformat_to(buffer, format_str, args);
159 detail::write_buffer(os, buffer);
164FMT_EXPORT
template <
typename Char>
165void vprint(std::basic_ostream<Char>& os,
169 detail::vformat_to(buffer, format_str, args);
170 if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()}))
return;
171 detail::write_buffer(os, buffer);
183FMT_EXPORT
template <
typename... T>
185 const auto& vargs = fmt::make_format_args(args...);
186 if (detail::use_utf8())
187 vprint(os, fmt, vargs);
189 detail::vprint_directly(os, fmt, vargs);
193template <
typename... Args>
194void print(std::wostream& os,
197 vprint(os, fmt, fmt::make_format_args<buffered_context<wchar_t>>(args...));
200FMT_EXPORT
template <
typename... T>
202 fmt::print(os,
"{}\n", fmt::format(fmt, std::forward<T>(args)...));
206template <
typename... Args>
207void println(std::wostream& os,
210 print(os, L
"{}\n", fmt::format(fmt, std::forward<Args>(args)...));