File indexing completed on 2025-07-05 08:54:19
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef FMT_OSTREAM_H_
0009 #define FMT_OSTREAM_H_
0010
0011 #include <fstream> // std::filebuf
0012
0013 #ifdef _WIN32
0014 # ifdef __GLIBCXX__
0015 # include <ext/stdio_filebuf.h>
0016 # include <ext/stdio_sync_filebuf.h>
0017 # endif
0018 # include <io.h>
0019 #endif
0020
0021 #include "format.h"
0022
0023 FMT_BEGIN_NAMESPACE
0024 namespace detail {
0025
0026 template <typename Streambuf> class formatbuf : public Streambuf {
0027 private:
0028 using char_type = typename Streambuf::char_type;
0029 using streamsize = decltype(std::declval<Streambuf>().sputn(nullptr, 0));
0030 using int_type = typename Streambuf::int_type;
0031 using traits_type = typename Streambuf::traits_type;
0032
0033 buffer<char_type>& buffer_;
0034
0035 public:
0036 explicit formatbuf(buffer<char_type>& buf) : buffer_(buf) {}
0037
0038 protected:
0039
0040
0041
0042
0043
0044
0045 auto overflow(int_type ch) -> int_type override {
0046 if (!traits_type::eq_int_type(ch, traits_type::eof()))
0047 buffer_.push_back(static_cast<char_type>(ch));
0048 return ch;
0049 }
0050
0051 auto xsputn(const char_type* s, streamsize count) -> streamsize override {
0052 buffer_.append(s, s + count);
0053 return count;
0054 }
0055 };
0056
0057
0058
0059 namespace {
0060 struct file_access_tag {};
0061 }
0062 template <typename Tag, typename BufType, FILE* BufType::*FileMemberPtr>
0063 class file_access {
0064 friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
0065 };
0066
0067 #if FMT_MSC_VERSION
0068 template class file_access<file_access_tag, std::filebuf,
0069 &std::filebuf::_Myfile>;
0070 auto get_file(std::filebuf&) -> FILE*;
0071 #endif
0072
0073 inline auto write_ostream_unicode(std::ostream& os, fmt::string_view data)
0074 -> bool {
0075 FILE* f = nullptr;
0076 #if FMT_MSC_VERSION
0077 if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
0078 f = get_file(*buf);
0079 else
0080 return false;
0081 #elif defined(_WIN32) && defined(__GLIBCXX__)
0082 auto* rdbuf = os.rdbuf();
0083 if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
0084 f = sfbuf->file();
0085 else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
0086 f = fbuf->file();
0087 else
0088 return false;
0089 #else
0090 ignore_unused(os, data, f);
0091 #endif
0092 #ifdef _WIN32
0093 if (f) {
0094 int fd = _fileno(f);
0095 if (_isatty(fd)) {
0096 os.flush();
0097 return write_console(fd, data);
0098 }
0099 }
0100 #endif
0101 return false;
0102 }
0103 inline auto write_ostream_unicode(std::wostream&,
0104 fmt::basic_string_view<wchar_t>) -> bool {
0105 return false;
0106 }
0107
0108
0109
0110 template <typename Char>
0111 void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
0112 const Char* buf_data = buf.data();
0113 using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
0114 unsigned_streamsize size = buf.size();
0115 unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
0116 do {
0117 unsigned_streamsize n = size <= max_size ? size : max_size;
0118 os.write(buf_data, static_cast<std::streamsize>(n));
0119 buf_data += n;
0120 size -= n;
0121 } while (size != 0);
0122 }
0123
0124 template <typename Char, typename T>
0125 void format_value(buffer<Char>& buf, const T& value) {
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 output.imbue(std::locale::classic());
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 {
0136 const T& value;
0137 };
0138
0139 }
0140
0141
0142 template <typename Char>
0143 struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
0144 void set_debug_format() = delete;
0145
0146 template <typename T, typename OutputIt>
0147 auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
0148 -> OutputIt {
0149 auto buffer = basic_memory_buffer<Char>();
0150 detail::format_value(buffer, value);
0151 return formatter<basic_string_view<Char>, Char>::format(
0152 {buffer.data(), buffer.size()}, ctx);
0153 }
0154 };
0155
0156 using ostream_formatter = basic_ostream_formatter<char>;
0157
0158 template <typename T, typename Char>
0159 struct formatter<detail::streamed_view<T>, Char>
0160 : basic_ostream_formatter<Char> {
0161 template <typename OutputIt>
0162 auto format(detail::streamed_view<T> view,
0163 basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
0164 return basic_ostream_formatter<Char>::format(view.value, ctx);
0165 }
0166 };
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 template <typename T>
0179 constexpr auto streamed(const T& value) -> detail::streamed_view<T> {
0180 return {value};
0181 }
0182
0183 namespace detail {
0184
0185 inline void vprint_directly(std::ostream& os, string_view format_str,
0186 format_args args) {
0187 auto buffer = memory_buffer();
0188 detail::vformat_to(buffer, format_str, args);
0189 detail::write_buffer(os, buffer);
0190 }
0191
0192 }
0193
0194 FMT_EXPORT template <typename Char>
0195 void vprint(std::basic_ostream<Char>& os,
0196 basic_string_view<type_identity_t<Char>> format_str,
0197 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
0198 auto buffer = basic_memory_buffer<Char>();
0199 detail::vformat_to(buffer, format_str, args);
0200 if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return;
0201 detail::write_buffer(os, buffer);
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 FMT_EXPORT template <typename... T>
0214 void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
0215 const auto& vargs = fmt::make_format_args(args...);
0216 if (detail::is_utf8())
0217 vprint(os, fmt, vargs);
0218 else
0219 detail::vprint_directly(os, fmt, vargs);
0220 }
0221
0222 FMT_EXPORT
0223 template <typename... Args>
0224 void print(std::wostream& os,
0225 basic_format_string<wchar_t, type_identity_t<Args>...> fmt,
0226 Args&&... args) {
0227 vprint(os, fmt, fmt::make_format_args<buffer_context<wchar_t>>(args...));
0228 }
0229
0230 FMT_EXPORT template <typename... T>
0231 void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
0232 fmt::print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
0233 }
0234
0235 FMT_EXPORT
0236 template <typename... Args>
0237 void println(std::wostream& os,
0238 basic_format_string<wchar_t, type_identity_t<Args>...> fmt,
0239 Args&&... args) {
0240 print(os, L"{}\n", fmt::format(fmt, std::forward<Args>(args)...));
0241 }
0242
0243 FMT_END_NAMESPACE
0244
0245 #endif