From eef6dbafbf2953b4aaf004edbce6a9fe84c9c52a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 25 Dec 2023 09:22:29 -0800 Subject: [PATCH] Refactor file layer in scan --- test/scan.h | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/test/scan.h b/test/scan.h index f81709a5..9b6f5d6b 100644 --- a/test/scan.h +++ b/test/scan.h @@ -152,6 +152,12 @@ class string_scan_buffer : public scan_buffer { : scan_buffer(s.begin(), s.end(), true) {} }; +#ifdef _WIN32 +void flockfile(FILE* f) { _lock_file(f); } +void funlockfile(FILE* f) { _unlock_file(f); } +int getc_unlocked(FILE *f) { return _fgetc_nolock(f); } +#endif + // A FILE wrapper. F is FILE defined as a template parameter to make // system-specific API detection work. template class file_base { @@ -164,7 +170,7 @@ template class file_base { // Reads a code unit from the stream. auto get() -> int { - int result = getc(file_); + int result = getc_unlocked(file_); if (result == EOF && ferror(file_) != 0) FMT_THROW(system_error(errno, FMT_STRING("getc failed"))); return result; @@ -182,6 +188,7 @@ template class glibc_file : public file_base { public: using file_base::file_base; + // Returns the file's read buffer as a string_view. auto buffer() const -> string_view { return {this->file_->_IO_read_ptr, to_unsigned(this->file_->_IO_read_end - this->file_->_IO_read_ptr)}; @@ -193,7 +200,6 @@ template class apple_file : public file_base { public: using file_base::file_base; - // Returns the file's read buffer as a string_view. auto buffer() const -> string_view { return {reinterpret_cast(this->file_->_p), to_unsigned(this->file_->_r)}; @@ -223,24 +229,19 @@ template class fallback_file : public file_base { } }; -template -auto get_file(F* file, int) -> glibc_file { - return file; -} -template -auto get_file(F* file, int) -> apple_file { - return file; -} -auto get_file(FILE* file, ...) -> fallback_file { return file; } - class file_scan_buffer : public scan_buffer { private: - decltype(get_file(static_cast(nullptr), 0)) file_; + template + static auto get_file(F* f, int) -> glibc_file { + return f; + } + template + static auto get_file(F* f, int) -> apple_file { + return f; + } + static auto get_file(FILE* f, ...) -> fallback_file { return f; } -#ifdef _WIN32 - static void flockfile(FILE* f) { _lock_file(f); } - static void funlockfile(FILE* f) { _unlock_file(f); } -#endif + decltype(get_file(static_cast(nullptr), 0)) file_; // Fills the buffer if it is empty. void fill() {