Skip to content

Commit 8dbae41

Browse files
refactoring
1 parent d9063ba commit 8dbae41

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

include/fmt/format-inl.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ FMT_FUNC void report_error(format_func func, int error_code,
7373
}
7474

7575
// A wrapper around fwrite that throws on error.
76-
inline void fwrite_fully(const void* ptr, size_t size, size_t count,
77-
FILE* stream) {
78-
size_t written = std::fwrite(ptr, size, count, stream);
76+
inline void fwrite_fully(const void* ptr, size_t count, FILE* stream) {
77+
size_t written = std::fwrite(ptr, 1, count, stream);
7978
if (written < count)
8079
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
8180
}
@@ -1433,13 +1432,11 @@ extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
14331432
void*, const void*, dword, dword*, void*);
14341433

14351434
FMT_FUNC bool write_console(std::FILE* f, string_view text) {
1436-
auto fd = _fileno(f);
1435+
int fd = _fileno(f);
14371436
if (!_isatty(fd)) return false;
14381437
auto u16 = utf8_to_utf16(text);
1439-
auto written = dword();
14401438
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
1441-
static_cast<uint32_t>(u16.size()), &written,
1442-
nullptr) != 0;
1439+
static_cast<dword>(u16.size()), nullptr, nullptr) != 0;
14431440
}
14441441
#endif
14451442

@@ -1448,12 +1445,12 @@ FMT_FUNC bool write_console(std::FILE* f, string_view text) {
14481445
FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) {
14491446
auto buffer = memory_buffer();
14501447
detail::vformat_to(buffer, fmt, args);
1451-
fwrite_fully(buffer.data(), 1, buffer.size(), f);
1448+
fwrite_fully(buffer.data(), buffer.size(), f);
14521449
}
14531450
#endif
14541451

14551452
FMT_FUNC void print(std::FILE* f, string_view text) {
1456-
if (!write_console(f, text)) fwrite_fully(text.data(), 1, text.size(), f);
1453+
if (!write_console(f, text)) fwrite_fully(text.data(), text.size(), f);
14571454
}
14581455
} // namespace detail
14591456

include/fmt/ostream.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,25 @@ auto get_file(std::filebuf&) -> FILE*;
3838
#endif
3939

4040
inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
41+
FILE* c_file = nullptr;
4142
#if FMT_MSC_VERSION
4243
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
43-
if (FILE* f = get_file(*buf)) return write_console(f, data);
44+
c_file = get_file(*buf);
4445
#elif defined(_WIN32) && defined(__GLIBCXX__)
4546
auto* rdbuf = os.rdbuf();
46-
FILE* c_file;
4747
if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
4848
c_file = sfbuf->file();
4949
else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
5050
c_file = fbuf->file();
5151
else
5252
return false;
53-
if (c_file) return write_console(c_file, data);
5453
#else
55-
ignore_unused(os, data);
54+
ignore_unused(os);
5655
#endif
57-
return false;
56+
if (!c_file) return false;
57+
return write_console(c_file, data);
5858
}
59+
5960
inline bool write_ostream_unicode(std::wostream&,
6061
fmt::basic_string_view<wchar_t>) {
6162
return false;

0 commit comments

Comments
 (0)