Skip to content

Commit

Permalink
Refactor file layer
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Dec 22, 2023
1 parent 1ca1a4a commit 3dc0dce
Showing 1 changed file with 73 additions and 32 deletions.
105 changes: 73 additions & 32 deletions test/scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,51 +136,92 @@ class string_scan_buffer : public scan_buffer {
: scan_buffer(s.begin(), s.end(), true) {}
};

class file_scan_buffer : public scan_buffer {
private:
FILE* file_;
char next_;
bool filled_ = false;
// 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_;

~file_base() = default;

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(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 Apple's libc.
template <typename F> class apple_file : public file_base<F> {
public:
using file_base<F>::file_base;

// Returns the file's read buffer as a string_view.
template <typename F, FMT_ENABLE_IF(sizeof(F::_p) != 0)>
auto get_buffer(F* file) -> string_view { // Apple libc
char* ptr = reinterpret_cast<char*>(file->_p);
return {ptr, to_unsigned(file->_r)};
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();
}
auto get_buffer(...) -> string_view {
return {&next_, (filled_ ? 1u : 0u)};

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

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

class file_scan_buffer : public scan_buffer {
private:
decltype(get_file(static_cast<FILE*>(nullptr), 0)) file_;

void do_fill() {
string_view buf = get_buffer(file_);
string_view buf = file_.buffer();
if (buf.size() == 0) {
int result = getc(file_);
if (result != EOF) {
// Put the character back since we are only filling the buffer.
if (ungetc(result, file_) == EOF)
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
next_ = static_cast<char>(result);
filled_ = true;
} else {
if (ferror(file_) != 0)
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
filled_ = false;
}
buf = get_buffer(file_);
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();
}
this->set(buf.begin(), buf.end());
}

void consume() override {
// Consume the current buffer content.
string_view buf = get_buffer(file_);
for (size_t i = 0, n = buf.size(); i != n; ++i) {
int result = getc(file_);
if (result == EOF && ferror(file_) != 0)
FMT_THROW(system_error(errno, FMT_STRING("I/O error")));
}
filled_ = false;
for (size_t i = 0, n = file_.buffer().size(); i != n; ++i) file_.get();
do_fill();
}

Expand Down

0 comments on commit 3dc0dce

Please sign in to comment.