25#ifndef OR_TOOLS_UTIL_FILELINEITER_H_
26#define OR_TOOLS_UTIL_FILELINEITER_H_
47 : next_position_after_eol_(0),
53 const std::string&
operator*()
const {
return line_; }
55 return file_ != other.file_;
60 bool HasOption(
int option)
const {
return options_ & option; }
64 if (file_ ==
nullptr)
return;
67 int i = next_position_after_eol_;
68 for (;
i < buffer_size_; ++
i) {
69 if (buffer_[i] ==
'\n')
break;
71 if (i == buffer_size_) {
72 line_.append(&buffer_[next_position_after_eol_],
73 i - next_position_after_eol_);
74 buffer_size_ = file_->
Read(&buffer_, kBufferSize);
75 if (buffer_size_ < 0) {
76 LOG(WARNING) <<
"Error while reading file.";
80 next_position_after_eol_ = 0;
81 if (buffer_size_ == 0) {
88 line_.append(&buffer_[next_position_after_eol_],
89 i - next_position_after_eol_ + 1);
90 next_position_after_eol_ =
i + 1;
96 (line_.empty() || line_ ==
"\n"));
99 void PostProcessLine() {
101 line_.erase(std::remove(line_.begin(), line_.end(),
'\r'), line_.end());
103 const auto eol = std::find(line_.begin(), line_.end(),
'\n');
109 static constexpr int kBufferSize = 5 * 1024;
110 char buffer_[kBufferSize];
111 int next_position_after_eol_;
112 int64_t buffer_size_;
134 : file_(
file), options_(options) {
152 LOG(WARNING) <<
"Could not open: " << filename;
Implements the minimum interface for a range-based for loop iterator.
bool operator!=(const FileLineIterator &other) const
FileLineIterator(File *file, int options)
const std::string & operator*() const
FileLines & operator=(const FileLines &)=delete
FileLines(absl::string_view filename, File *const file, const int options=FileLineIterator::DEFAULT)
FileLines(const FileLines &)=delete
FileLineIterator end() const
FileLines(absl::string_view filename, int options=FileLineIterator::DEFAULT)
size_t Read(void *buff, size_t size)
Reads "size" bytes to buff from file, buff should be pre-allocated.
bool Close()
Closes the file.
absl::Status Open(absl::string_view filename, absl::string_view mode, File **f, Options options)
As of 2016-01, these methods can only be used with flags = file::Defaults().