2010-09-10 04:36:07 +04:00
|
|
|
#ifndef UTIL_FILE_PIECE__
|
|
|
|
#define UTIL_FILE_PIECE__
|
|
|
|
|
2010-09-22 02:43:29 +04:00
|
|
|
#include "ersatz_progress.hh"
|
|
|
|
#include "exception.hh"
|
|
|
|
#include "mmap.hh"
|
|
|
|
#include "scoped.hh"
|
|
|
|
#include "string_piece.hh"
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
class EndOfFileException : public Exception {
|
2010-09-10 04:36:07 +04:00
|
|
|
public:
|
2010-09-15 01:33:11 +04:00
|
|
|
EndOfFileException() throw();
|
|
|
|
~EndOfFileException() throw();
|
2010-09-10 04:36:07 +04:00
|
|
|
};
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
class ParseNumberException : public Exception {
|
2010-09-10 04:36:07 +04:00
|
|
|
public:
|
|
|
|
explicit ParseNumberException(StringPiece value) throw();
|
|
|
|
~ParseNumberException() throw() {}
|
2010-09-15 01:33:11 +04:00
|
|
|
};
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
int OpenReadOrThrow(const char *name);
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
// Return value for SizeFile when it can't size properly.
|
|
|
|
const off_t kBadSize = -1;
|
|
|
|
off_t SizeFile(int fd);
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
class FilePiece {
|
|
|
|
public:
|
|
|
|
// 32 MB default.
|
2010-09-15 01:33:11 +04:00
|
|
|
explicit FilePiece(const char *file, std::ostream *show_progress = NULL, off_t min_buffer = 33554432);
|
|
|
|
// Takes ownership of fd. name is used for messages.
|
|
|
|
explicit FilePiece(const char *name, int fd, std::ostream *show_progress = NULL, off_t min_buffer = 33554432);
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
char get() throw(EndOfFileException) {
|
|
|
|
if (position_ == position_end_) Shift();
|
|
|
|
return *(position_++);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Memory backing the returned StringPiece may vanish on the next call.
|
|
|
|
// Leaves the delimiter, if any, to be returned by get().
|
|
|
|
StringPiece ReadDelimited() throw(EndOfFileException) {
|
|
|
|
SkipSpaces();
|
|
|
|
return Consume(FindDelimiterOrEOF());
|
|
|
|
}
|
|
|
|
// Unlike ReadDelimited, this includes leading spaces and consumes the delimiter.
|
|
|
|
// It is similar to getline in that way.
|
|
|
|
StringPiece ReadLine(char delim = '\n') throw(EndOfFileException);
|
|
|
|
|
|
|
|
float ReadFloat() throw(EndOfFileException, ParseNumberException);
|
|
|
|
|
|
|
|
void SkipSpaces() throw (EndOfFileException);
|
2010-09-15 01:33:11 +04:00
|
|
|
|
|
|
|
off_t Offset() const {
|
|
|
|
return position_ - data_.begin() + mapped_offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only for testing.
|
|
|
|
void ForceFallbackToRead() {
|
|
|
|
fallback_to_read_ = true;
|
|
|
|
}
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
private:
|
2010-09-15 01:33:11 +04:00
|
|
|
void Initialize(const char *name, std::ostream *show_progress, off_t min_buffer);
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
StringPiece Consume(const char *to) {
|
|
|
|
StringPiece ret(position_, to - position_);
|
|
|
|
position_ = to;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *FindDelimiterOrEOF() throw(EndOfFileException);
|
|
|
|
|
|
|
|
void Shift() throw (EndOfFileException);
|
2010-09-15 01:33:11 +04:00
|
|
|
// Backends to Shift().
|
|
|
|
void MMapShift(off_t desired_begin) throw ();
|
|
|
|
void ReadShift(off_t desired_begin) throw ();
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
const char *position_, *last_space_, *position_end_;
|
|
|
|
|
|
|
|
scoped_fd file_;
|
|
|
|
const off_t total_size_;
|
|
|
|
const off_t page_;
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
size_t default_map_size_;
|
2010-09-10 04:36:07 +04:00
|
|
|
off_t mapped_offset_;
|
|
|
|
|
|
|
|
// Order matters: file_ should always be destroyed after this.
|
2010-09-15 01:33:11 +04:00
|
|
|
scoped_memory data_;
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
bool at_end_;
|
2010-09-15 01:33:11 +04:00
|
|
|
bool fallback_to_read_;
|
|
|
|
|
|
|
|
ErsatzProgress progress_;
|
2010-09-10 04:36:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
|
|
|
#endif // UTIL_FILE_PIECE__
|