refactoring
This commit is contained in:
parent
d9063baf22
commit
8dbae411f4
@ -73,9 +73,8 @@ FMT_FUNC void report_error(format_func func, int error_code,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A wrapper around fwrite that throws on error.
|
// A wrapper around fwrite that throws on error.
|
||||||
inline void fwrite_fully(const void* ptr, size_t size, size_t count,
|
inline void fwrite_fully(const void* ptr, size_t count, FILE* stream) {
|
||||||
FILE* stream) {
|
size_t written = std::fwrite(ptr, 1, count, stream);
|
||||||
size_t written = std::fwrite(ptr, size, count, stream);
|
|
||||||
if (written < count)
|
if (written < count)
|
||||||
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
|
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
|
||||||
}
|
}
|
||||||
@ -1433,13 +1432,11 @@ extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
|
|||||||
void*, const void*, dword, dword*, void*);
|
void*, const void*, dword, dword*, void*);
|
||||||
|
|
||||||
FMT_FUNC bool write_console(std::FILE* f, string_view text) {
|
FMT_FUNC bool write_console(std::FILE* f, string_view text) {
|
||||||
auto fd = _fileno(f);
|
int fd = _fileno(f);
|
||||||
if (!_isatty(fd)) return false;
|
if (!_isatty(fd)) return false;
|
||||||
auto u16 = utf8_to_utf16(text);
|
auto u16 = utf8_to_utf16(text);
|
||||||
auto written = dword();
|
|
||||||
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
|
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
|
||||||
static_cast<uint32_t>(u16.size()), &written,
|
static_cast<dword>(u16.size()), nullptr, nullptr) != 0;
|
||||||
nullptr) != 0;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1448,12 +1445,12 @@ FMT_FUNC bool write_console(std::FILE* f, string_view text) {
|
|||||||
FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) {
|
FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) {
|
||||||
auto buffer = memory_buffer();
|
auto buffer = memory_buffer();
|
||||||
detail::vformat_to(buffer, fmt, args);
|
detail::vformat_to(buffer, fmt, args);
|
||||||
fwrite_fully(buffer.data(), 1, buffer.size(), f);
|
fwrite_fully(buffer.data(), buffer.size(), f);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
FMT_FUNC void print(std::FILE* f, string_view text) {
|
FMT_FUNC void print(std::FILE* f, string_view text) {
|
||||||
if (!write_console(f, text)) fwrite_fully(text.data(), 1, text.size(), f);
|
if (!write_console(f, text)) fwrite_fully(text.data(), text.size(), f);
|
||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
|||||||
@ -38,24 +38,25 @@ auto get_file(std::filebuf&) -> FILE*;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
|
inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
|
||||||
|
FILE* c_file = nullptr;
|
||||||
#if FMT_MSC_VERSION
|
#if FMT_MSC_VERSION
|
||||||
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
|
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
|
||||||
if (FILE* f = get_file(*buf)) return write_console(f, data);
|
c_file = get_file(*buf);
|
||||||
#elif defined(_WIN32) && defined(__GLIBCXX__)
|
#elif defined(_WIN32) && defined(__GLIBCXX__)
|
||||||
auto* rdbuf = os.rdbuf();
|
auto* rdbuf = os.rdbuf();
|
||||||
FILE* c_file;
|
|
||||||
if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
|
if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
|
||||||
c_file = sfbuf->file();
|
c_file = sfbuf->file();
|
||||||
else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
|
else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
|
||||||
c_file = fbuf->file();
|
c_file = fbuf->file();
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
if (c_file) return write_console(c_file, data);
|
|
||||||
#else
|
#else
|
||||||
ignore_unused(os, data);
|
ignore_unused(os);
|
||||||
#endif
|
#endif
|
||||||
return false;
|
if (!c_file) return false;
|
||||||
|
return write_console(c_file, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool write_ostream_unicode(std::wostream&,
|
inline bool write_ostream_unicode(std::wostream&,
|
||||||
fmt::basic_string_view<wchar_t>) {
|
fmt::basic_string_view<wchar_t>) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user