File indexing completed on 2025-01-18 09:57:24
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef FMT_OSTREAM_H_
0009 #define FMT_OSTREAM_H_
0010
0011 #include <fstream>
0012 #include <ostream>
0013 #if defined(_WIN32) && defined(__GLIBCXX__)
0014 # include <ext/stdio_filebuf.h>
0015 # include <ext/stdio_sync_filebuf.h>
0016 #elif defined(_WIN32) && defined(_LIBCPP_VERSION)
0017 # include <__std_stream>
0018 #endif
0019
0020 #include "format.h"
0021
0022 FMT_BEGIN_NAMESPACE
0023
0024 template <typename OutputIt, typename Char> class basic_printf_context;
0025
0026 namespace detail {
0027
0028
0029 template <typename T, typename Char, typename Enable = void>
0030 class is_streamable {
0031 private:
0032 template <typename U>
0033 static auto test(int)
0034 -> bool_constant<sizeof(std::declval<std::basic_ostream<Char>&>()
0035 << std::declval<U>()) != 0>;
0036
0037 template <typename> static auto test(...) -> std::false_type;
0038
0039 using result = decltype(test<T>(0));
0040
0041 public:
0042 is_streamable() = default;
0043
0044 static const bool value = result::value;
0045 };
0046
0047
0048
0049 template <typename T, typename Char>
0050 struct is_streamable<
0051 T, Char,
0052 enable_if_t<
0053 std::is_arithmetic<T>::value || std::is_array<T>::value ||
0054 std::is_pointer<T>::value || std::is_same<T, char8_type>::value ||
0055 std::is_convertible<T, fmt::basic_string_view<Char>>::value ||
0056 std::is_same<T, std_string_view<Char>>::value ||
0057 (std::is_convertible<T, int>::value && !std::is_enum<T>::value)>>
0058 : std::false_type {};
0059
0060
0061
0062 namespace {
0063 struct file_access_tag {};
0064 }
0065 template <class Tag, class BufType, FILE* BufType::*FileMemberPtr>
0066 class file_access {
0067 friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
0068 };
0069
0070 #if FMT_MSC_VERSION
0071 template class file_access<file_access_tag, std::filebuf,
0072 &std::filebuf::_Myfile>;
0073 auto get_file(std::filebuf&) -> FILE*;
0074 #elif defined(_WIN32) && defined(_LIBCPP_VERSION)
0075 template class file_access<file_access_tag, std::__stdoutbuf<char>,
0076 &std::__stdoutbuf<char>::__file_>;
0077 auto get_file(std::__stdoutbuf<char>&) -> FILE*;
0078 #endif
0079
0080 inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
0081 #if FMT_MSC_VERSION
0082 if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
0083 if (FILE* f = get_file(*buf)) return write_console(f, data);
0084 #elif defined(_WIN32) && defined(__GLIBCXX__)
0085 auto* rdbuf = os.rdbuf();
0086 FILE* c_file;
0087 if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
0088 c_file = fbuf->file();
0089 else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
0090 c_file = fbuf->file();
0091 else
0092 return false;
0093 if (c_file) return write_console(c_file, data);
0094 #elif defined(_WIN32) && defined(_LIBCPP_VERSION)
0095 if (auto* buf = dynamic_cast<std::__stdoutbuf<char>*>(os.rdbuf()))
0096 if (FILE* f = get_file(*buf)) return write_console(f, data);
0097 #else
0098 ignore_unused(os, data);
0099 #endif
0100 return false;
0101 }
0102 inline bool write_ostream_unicode(std::wostream&,
0103 fmt::basic_string_view<wchar_t>) {
0104 return false;
0105 }
0106
0107
0108
0109 template <typename Char>
0110 void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
0111 const Char* buf_data = buf.data();
0112 using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
0113 unsigned_streamsize size = buf.size();
0114 unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
0115 do {
0116 unsigned_streamsize n = size <= max_size ? size : max_size;
0117 os.write(buf_data, static_cast<std::streamsize>(n));
0118 buf_data += n;
0119 size -= n;
0120 } while (size != 0);
0121 }
0122
0123 template <typename Char, typename T>
0124 void format_value(buffer<Char>& buf, const T& value,
0125 locale_ref loc = locale_ref()) {
0126 auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
0127 auto&& output = std::basic_ostream<Char>(&format_buf);
0128 #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
0129 if (loc) output.imbue(loc.get<std::locale>());
0130 #endif
0131 output << value;
0132 output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
0133 }
0134
0135 template <typename T> struct streamed_view { const T& value; };
0136
0137 }
0138
0139
0140 template <typename Char>
0141 struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
0142 void set_debug_format() = delete;
0143
0144 template <typename T, typename OutputIt>
0145 auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
0146 -> OutputIt {
0147 auto buffer = basic_memory_buffer<Char>();
0148 format_value(buffer, value, ctx.locale());
0149 return formatter<basic_string_view<Char>, Char>::format(
0150 {buffer.data(), buffer.size()}, ctx);
0151 }
0152 };
0153
0154 using ostream_formatter = basic_ostream_formatter<char>;
0155
0156 template <typename T, typename Char>
0157 struct formatter<detail::streamed_view<T>, Char>
0158 : basic_ostream_formatter<Char> {
0159 template <typename OutputIt>
0160 auto format(detail::streamed_view<T> view,
0161 basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
0162 return basic_ostream_formatter<Char>::format(view.value, ctx);
0163 }
0164 };
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176 template <typename T>
0177 auto streamed(const T& value) -> detail::streamed_view<T> {
0178 return {value};
0179 }
0180
0181 namespace detail {
0182
0183
0184 template <typename T, typename Char>
0185 struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
0186 : basic_ostream_formatter<Char> {
0187 using basic_ostream_formatter<Char>::format;
0188 };
0189
0190 inline void vprint_directly(std::ostream& os, string_view format_str,
0191 format_args args) {
0192 auto buffer = memory_buffer();
0193 detail::vformat_to(buffer, format_str, args);
0194 detail::write_buffer(os, buffer);
0195 }
0196
0197 }
0198
0199 FMT_MODULE_EXPORT template <typename Char>
0200 void vprint(std::basic_ostream<Char>& os,
0201 basic_string_view<type_identity_t<Char>> format_str,
0202 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
0203 auto buffer = basic_memory_buffer<Char>();
0204 detail::vformat_to(buffer, format_str, args);
0205 if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return;
0206 detail::write_buffer(os, buffer);
0207 }
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 FMT_MODULE_EXPORT template <typename... T>
0219 void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
0220 const auto& vargs = fmt::make_format_args(args...);
0221 if (detail::is_utf8())
0222 vprint(os, fmt, vargs);
0223 else
0224 detail::vprint_directly(os, fmt, vargs);
0225 }
0226
0227 FMT_MODULE_EXPORT
0228 template <typename... Args>
0229 void print(std::wostream& os,
0230 basic_format_string<wchar_t, type_identity_t<Args>...> fmt,
0231 Args&&... args) {
0232 vprint(os, fmt, fmt::make_format_args<buffer_context<wchar_t>>(args...));
0233 }
0234
0235 FMT_END_NAMESPACE
0236
0237 #endif