Skip to content

Commit

Permalink
Improve support for non-POSIX platforms more
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 8, 2024
1 parent 486838f commit 92227c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
20 changes: 14 additions & 6 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1443,17 +1443,24 @@ template <typename T> struct span {
size_t size;
};

#ifdef _WIN32
inline void flockfile(FILE* f) { _lock_file(f); }
inline void funlockfile(FILE* f) { _unlock_file(f); }
inline int getc_unlocked(FILE* f) { return _fgetc_nolock(f); }
template <typename F> auto flockfile(F* f) -> decltype(_lock_file(f)) {
_lock_file(f);
}
template <typename F> auto funlockfile(F* f) -> decltype(_unlock_file(f)) {
_unlock_file(f);
}

#ifndef getc_unlocked
template <typename F> auto getc_unlocked(F* f) -> decltype(_fgetc_nolock(f)) {
return _fgetc_nolock(f);
}
#endif

template <typename F = FILE, typename Enable = void>
struct has_flockfile : std::false_type {};

template <typename F>
struct has_flockfile<F, void_t<decltype(flockfile(static_cast<F*>(nullptr)))>>
struct has_flockfile<F, void_t<decltype(flockfile(&std::declval<F&>()))>>
: std::true_type {};

// A FILE wrapper. F is FILE defined as a template parameter to make system API
Expand Down Expand Up @@ -1658,7 +1665,8 @@ class file_print_buffer<F, enable_if_t<has_flockfile<F>::value>>
~file_print_buffer() {
file_.advance_write_buffer(size());
bool flush = file_.needs_flush();
funlockfile(file_);
F* f = file_; // Make funlockfile depend on the template parameter F
funlockfile(f); // for the system API detection to work.
if (flush) fflush(file_);
}
};
Expand Down
5 changes: 4 additions & 1 deletion test/scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ class file_scan_buffer final : public scan_buffer {
flockfile(f);
fill();
}
~file_scan_buffer() { funlockfile(file_); }
~file_scan_buffer() {
FILE* f = file_;
funlockfile(f);
}
};
} // namespace detail

Expand Down

0 comments on commit 92227c7

Please sign in to comment.