Skip to content

Commit

Permalink
Write directly to a stream buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 18, 2024
1 parent b2cde48 commit 004edd8
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 81 deletions.
149 changes: 149 additions & 0 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,149 @@ FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string {
}

namespace detail {

struct span {
char* data;
size_t size;
};

// A FILE wrapper. F is FILE defined as a template parameter to make system API
// detection work.
template <typename F> class file_base {
public:
F* file_;

public:
file_base(F* file) : file_(file) {}
operator F*() const { return file_; }

// Reads a code unit from the stream.
auto get() -> int {
int result = getc_unlocked(file_);
if (result == EOF && ferror(file_) != 0)
FMT_THROW(system_error(errno, FMT_STRING("getc failed")));
return result;
}

// Puts the code unit back into the stream buffer.
void unget(char c) {
if (ungetc(c, file_) == EOF)
FMT_THROW(system_error(errno, FMT_STRING("ungetc failed")));
}
};

// A FILE wrapper for glibc.
template <typename F> class glibc_file : public file_base<F> {
public:
using file_base<F>::file_base;

// Returns the file's read buffer as a string_view.
auto get_read_buffer() const -> string_view {
return {this->file_->_IO_read_ptr,
to_unsigned(this->file_->_IO_read_end - this->file_->_IO_read_ptr)};
}
};

// A FILE wrapper for Apple's libc.
template <typename F> class apple_file : public file_base<F> {
private:
auto offset() const -> size_t {
return this->file_->_p - this->file_->_bf._base;
}

public:
using file_base<F>::file_base;

auto is_buffered() const -> bool {
return (this->file_->_flags & 0x0002) == 0; // __SNBF
}

auto get_read_buffer() const -> string_view {
return {reinterpret_cast<char*>(this->file_->_p),
to_unsigned(this->file_->_r)};
}

auto get_write_buffer() const -> span {
if (!this->file_->_p || offset() == this->file_->_bf._size) {
fputc(0, this->file_);
--this->file_->_p;
++this->file_->_w;
}
FMT_ASSERT(offset() != this->file_->_bf._size, "");
return {reinterpret_cast<char*>(this->file_->_p),
to_unsigned(this->file_->_bf._size - offset())};
}

void advance_write_buffer(size_t size) {
this->file_->_p += size;
this->file_->_w -= size;
}
};

// A fallback FILE wrapper.
template <typename F> class fallback_file : public file_base<F> {
private:
char next_; // The next unconsumed character in the buffer.
bool has_next_ = false;

public:
using file_base<F>::file_base;

auto get_read_buffer() const -> string_view {
return {&next_, has_next_ ? 1u : 0u};
}

auto get() -> int {
has_next_ = false;
return file_base<F>::get();
}

void unget(char c) {
file_base<F>::unget(c);
next_ = c;
has_next_ = 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

template <typename F, FMT_ENABLE_IF(sizeof(F::_p) != 0)>
auto get_file(F* f, int) -> apple_file<F> {
return f;
}
inline auto get_file(FILE* f, ...) -> fallback_file<FILE> { return f; }

using file_ref = decltype(get_file(static_cast<FILE*>(nullptr), 0));

class file_print_buffer : public buffer<char> {
private:
file_ref file_;

void set_buffer() {
auto buf = file_.get_write_buffer();
this->set(buf.data, buf.size);
}

static FMT_CONSTEXPR void grow(buffer<char>& buf, size_t) {
auto& self = static_cast<file_print_buffer&>(buf);
self.set_buffer();
}

public:
explicit file_print_buffer(FILE* f) : buffer(grow, size_t()), file_(f) {
flockfile(f);
set_buffer();
}
~file_print_buffer() {
file_.advance_write_buffer(size());
funlockfile(file_);
}
};

#if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR)
FMT_FUNC auto write_console(int, string_view) -> bool { return false; }
#else
Expand Down Expand Up @@ -1470,6 +1613,12 @@ FMT_FUNC void print(std::FILE* f, string_view text) {
} // namespace detail

FMT_FUNC void vprint(std::FILE* f, string_view fmt, format_args args) {
#ifdef __APPLE__
if (detail::file_ref(f).is_buffered()) {
auto&& buffer = detail::file_print_buffer(f);
return detail::vformat_to(buffer, fmt, args);
}
#endif
auto buffer = memory_buffer();
detail::vformat_to(buffer, fmt, args);
detail::print(f, {buffer.data(), buffer.size()});
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ endif()
add_fmt_test(printf-test)
add_fmt_test(ranges-test ranges-odr-test.cc)

add_fmt_test(scan-test)
add_fmt_test(scan-test HEADER_ONLY)
check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
if (HAVE_STRPTIME)
target_compile_definitions(scan-test PRIVATE FMT_HAVE_STRPTIME)
Expand Down
83 changes: 3 additions & 80 deletions test/scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,83 +150,6 @@ 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 <typename F> class file_base {
protected:
F* file_;

public:
file_base(F* file) : file_(file) {}
operator F*() const { return file_; }

// Reads a code unit from the stream.
auto get() -> int {
int result = getc_unlocked(file_);
if (result == EOF && ferror(file_) != 0)
FMT_THROW(system_error(errno, FMT_STRING("getc failed")));
return result;
}

// Puts the code unit back into the stream buffer.
void unget(char c) {
if (ungetc(c, file_) == EOF)
FMT_THROW(system_error(errno, FMT_STRING("ungetc failed")));
}
};

// A FILE wrapper for glibc.
template <typename F> class glibc_file : public file_base<F> {
public:
using file_base<F>::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)};
}
};

// A FILE wrapper for Apple's libc.
template <typename F> class apple_file : public file_base<F> {
public:
using file_base<F>::file_base;

auto buffer() const -> string_view {
return {reinterpret_cast<char*>(this->file_->_p),
to_unsigned(this->file_->_r)};
}
};

// A fallback FILE wrapper.
template <typename F> class fallback_file : public file_base<F> {
private:
char next_; // The next unconsumed character in the buffer.
bool has_next_ = false;

public:
using file_base<F>::file_base;

auto buffer() const -> string_view { return {&next_, has_next_ ? 1u : 0u}; }

auto get() -> int {
has_next_ = false;
return file_base<F>::get();
}

void unget(char c) {
file_base<F>::unget(c);
next_ = c;
has_next_ = true;
}
};

class file_scan_buffer : public scan_buffer {
private:
template <typename F, FMT_ENABLE_IF(sizeof(F::_IO_read_ptr) != 0)>
Expand All @@ -243,19 +166,19 @@ class file_scan_buffer : public scan_buffer {

// Fills the buffer if it is empty.
void fill() {
string_view buf = file_.buffer();
string_view buf = file_.get_read_buffer();
if (buf.size() == 0) {
int c = file_.get();
// Put the character back since we are only filling the buffer.
if (c != EOF) file_.unget(static_cast<char>(c));
buf = file_.buffer();
buf = file_.get_read_buffer();
}
set(buf);
}

void consume() override {
// Consume the current buffer content.
size_t n = to_unsigned(ptr() - file_.buffer().begin());
size_t n = to_unsigned(ptr() - file_.get_read_buffer().begin());
for (size_t i = 0; i != n; ++i) file_.get();
fill();
}
Expand Down

0 comments on commit 004edd8

Please sign in to comment.