This commit is contained in:
Dimitrij Mijoski 2022-07-23 04:09:22 +02:00
parent 7a94e60988
commit 5fd371ebe1
3 changed files with 18 additions and 32 deletions

View File

@ -1479,7 +1479,7 @@ using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
void*, const void*, dword, dword*, void*);
FMT_FUNC bool write_console_on_windows(std::FILE* f, string_view text) {
FMT_FUNC bool write_console(std::FILE* f, string_view text) {
auto fd = _fileno(f);
if (_isatty(fd)) {
detail::utf8_to_utf16 u16(string_view(text.data(), text.size()));
@ -1500,7 +1500,7 @@ FMT_FUNC bool write_console_on_windows(std::FILE* f, string_view text) {
FMT_FUNC void print(std::FILE* f, string_view text) {
#ifdef _WIN32
if (write_console_on_windows(f, text)) return;
if (write_console(f, text)) return;
#endif
detail::fwrite_fully(text.data(), 1, text.size(), f);
}

View File

@ -922,7 +922,7 @@ struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
namespace detail {
#ifdef _WIN32
FMT_API bool write_console_on_windows(std::FILE* f, string_view text);
FMT_API bool write_console(std::FILE* f, string_view text);
#endif
FMT_API void print(std::FILE*, string_view);
}

View File

@ -85,22 +85,11 @@ template class filebuf_access<filebuf_access_tag,
decltype(&filebuf_type::_Myfile),
&filebuf_type::_Myfile>;
inline bool write_ostream_msvc_unicode(std::ostream& os,
fmt::string_view data) {
#ifdef _WIN32
inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
#if FMT_MSC_VERSION
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
if (FILE* f = get_file(*buf)) return write_console_on_windows(f, data);
#endif
return false;
}
inline bool write_ostream_msvc_unicode(std::wostream&,
fmt::basic_string_view<wchar_t>) {
return false;
}
#if defined(_WIN32) && defined(__GLIBCXX__)
inline bool write_ostream_gcc_mingw_unicode(std::ostream& os,
fmt::string_view data) {
if (FILE* f = get_file(*buf)) return write_console(f, data);
#elif defined(_WIN32) && defined(__GLIBCXX__)
auto* rdbuf = os.rdbuf();
FILE* c_file;
if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
@ -109,26 +98,23 @@ inline bool write_ostream_gcc_mingw_unicode(std::ostream& os,
c_file = fbuf->file();
else
return false;
if (c_file) return write_console_on_windows(c_file, data);
return false;
}
inline bool write_ostream_gcc_mingw_unicode(std::wostream&,
fmt::basic_string_view<wchar_t>) {
return false;
}
if (c_file) return write_console(c_file, data);
#else
(void)os; // suppress warning
(void)data;
#endif
return false;
}
inline bool write_ostream_unicode(std::wostream&,
fmt::basic_string_view<wchar_t>) {
return false;
}
// Write the content of buf to os.
// It is a separate function rather than a part of vprint to simplify testing.
template <typename Char>
void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
// TODO: check detail::is_utf8(). Here or bellow in print or in vprint?
#if FMT_MSC_VERSION
if (write_ostream_msvc_unicode(os, {buf.data(), buf.size()})) return;
#elif defined(_WIN32) && defined(__GLIBCXX__)
if (write_ostream_gcc_mingw_unicode(os, {buf.data(), buf.size()})) return;
#endif
if (write_ostream_unicode(os, {buf.data(), buf.size()})) return;
const Char* buf_data = buf.data();
using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
unsigned_streamsize size = buf.size();