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)
47#if FMT_MSC_VERSION && FMT_USE_RTTI
48 if (
auto* buf =
dynamic_cast<std::filebuf*
>(os.rdbuf()))
52#elif defined(_WIN32) && defined(__GLIBCXX__) && FMT_USE_RTTI
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>
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);
121 return formatter<basic_string_view<Char>, Char>::format(
122 {buffer.data(), buffer.size()}, ctx);
128template <
typename T,
typename Char>
129struct formatter<detail::streamed_view<T>, Char>
131 template <
typename Context>
133 ->
decltype(ctx.out()) {
134 return basic_ostream_formatter<Char>::format(view.value, ctx);
153inline void vprint_directly(std::ostream& os, string_view format_str,
155 auto buffer = memory_buffer();
156 detail::vformat_to(
buffer, format_str, args);
157 detail::write_buffer(os,
buffer);
162FMT_EXPORT
template <
typename Char>
163void vprint(std::basic_ostream<Char>& os,
165 typename detail::vformat_args<Char>::type args) {
167 detail::vformat_to(buffer, format_str, args);
168 if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()}))
return;
169 detail::write_buffer(os, buffer);
179FMT_EXPORT
template <
typename... T>
180void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
181 const auto& vargs = fmt::make_format_args(args...);
182 if (detail::use_utf8())
183 vprint(os, fmt, vargs);
185 detail::vprint_directly(os, fmt, vargs);
189template <
typename... Args>
190void print(std::wostream& os,
193 vprint(os, fmt, fmt::make_format_args<buffered_context<wchar_t>>(args...));
196FMT_EXPORT
template <
typename... T>
197void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
198 fmt::print(os,
"{}\n", fmt::format(fmt, std::forward<T>(args)...));
202template <
typename... Args>
203void println(std::wostream& os,
206 print(os, L
"{}\n", fmt::format(fmt, std::forward<Args>(args)...));