2010-09-10 04:36:07 +04:00
|
|
|
#ifndef UTIL_FILE_PIECE__
|
|
|
|
#define UTIL_FILE_PIECE__
|
|
|
|
|
2010-09-28 20:26:55 +04:00
|
|
|
#include "util/ersatz_progress.hh"
|
|
|
|
#include "util/exception.hh"
|
2011-09-21 20:06:48 +04:00
|
|
|
#include "util/file.hh"
|
2010-09-28 20:26:55 +04:00
|
|
|
#include "util/mmap.hh"
|
2012-10-30 23:33:22 +04:00
|
|
|
#include "util/read_compressed.hh"
|
2010-09-28 20:26:55 +04:00
|
|
|
#include "util/string_piece.hh"
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-11-11 00:46:59 +04:00
|
|
|
#include <cstddef>
|
2010-09-10 04:36:07 +04:00
|
|
|
#include <string>
|
|
|
|
|
2011-11-12 02:24:25 +04:00
|
|
|
#include <stdint.h>
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
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
|
|
|
|
2011-01-25 22:11:48 +03:00
|
|
|
extern const bool kSpaces[256];
|
|
|
|
|
2013-01-05 01:02:47 +04:00
|
|
|
// Memory backing the returned StringPiece may vanish on the next call.
|
2010-09-10 04:36:07 +04:00
|
|
|
class FilePiece {
|
|
|
|
public:
|
2012-10-30 23:33:22 +04:00
|
|
|
// 1 MB default.
|
|
|
|
explicit FilePiece(const char *file, std::ostream *show_progress = NULL, std::size_t min_buffer = 1048576);
|
2013-01-05 01:02:47 +04:00
|
|
|
// Takes ownership of fd. name is used for messages.
|
2013-01-17 15:58:58 +04:00
|
|
|
explicit FilePiece(int fd, const char *name = NULL, std::ostream *show_progress = NULL, std::size_t min_buffer = 1048576);
|
2010-10-23 09:21:10 +04:00
|
|
|
|
|
|
|
~FilePiece();
|
2013-01-05 01:02:47 +04:00
|
|
|
|
|
|
|
char get() {
|
2010-10-23 09:21:10 +04:00
|
|
|
if (position_ == position_end_) {
|
|
|
|
Shift();
|
|
|
|
if (at_end_) throw EndOfFileException();
|
|
|
|
}
|
2010-09-10 04:36:07 +04:00
|
|
|
return *(position_++);
|
|
|
|
}
|
|
|
|
|
2013-01-05 01:02:47 +04:00
|
|
|
// Leaves the delimiter, if any, to be returned by get(). Delimiters defined by isspace().
|
2011-07-14 00:53:18 +04:00
|
|
|
StringPiece ReadDelimited(const bool *delim = kSpaces) {
|
2011-01-25 22:11:48 +03:00
|
|
|
SkipSpaces(delim);
|
|
|
|
return Consume(FindDelimiterOrEOF(delim));
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
2011-01-25 22:11:48 +03:00
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
// Unlike ReadDelimited, this includes leading spaces and consumes the delimiter.
|
2013-01-05 01:02:47 +04:00
|
|
|
// It is similar to getline in that way.
|
2011-07-14 00:53:18 +04:00
|
|
|
StringPiece ReadLine(char delim = '\n');
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-07-14 00:53:18 +04:00
|
|
|
float ReadFloat();
|
|
|
|
double ReadDouble();
|
|
|
|
long int ReadLong();
|
|
|
|
unsigned long int ReadULong();
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2013-01-05 01:02:47 +04:00
|
|
|
// Skip spaces defined by isspace.
|
2011-07-14 00:53:18 +04:00
|
|
|
void SkipSpaces(const bool *delim = kSpaces) {
|
2011-01-25 22:11:48 +03:00
|
|
|
for (; ; ++position_) {
|
|
|
|
if (position_ == position_end_) Shift();
|
|
|
|
if (!delim[static_cast<unsigned char>(*position_)]) return;
|
|
|
|
}
|
|
|
|
}
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2011-11-11 00:46:59 +04:00
|
|
|
uint64_t Offset() const {
|
2010-09-15 01:33:11 +04:00
|
|
|
return position_ - data_.begin() + mapped_offset_;
|
|
|
|
}
|
|
|
|
|
2010-10-23 09:21:10 +04:00
|
|
|
const std::string &FileName() const { return file_name_; }
|
2013-01-05 01:02:47 +04:00
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
private:
|
2011-11-11 00:46:59 +04:00
|
|
|
void Initialize(const char *name, std::ostream *show_progress, std::size_t min_buffer);
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2011-07-14 00:53:18 +04:00
|
|
|
template <class T> T ReadNumber();
|
2010-11-28 05:54:56 +03:00
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
StringPiece Consume(const char *to) {
|
|
|
|
StringPiece ret(position_, to - position_);
|
|
|
|
position_ = to;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-14 00:53:18 +04:00
|
|
|
const char *FindDelimiterOrEOF(const bool *delim = kSpaces);
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-07-14 00:53:18 +04:00
|
|
|
void Shift();
|
2010-09-15 01:33:11 +04:00
|
|
|
// Backends to Shift().
|
2011-11-11 00:46:59 +04:00
|
|
|
void MMapShift(uint64_t desired_begin);
|
2010-10-23 09:21:10 +04:00
|
|
|
|
2011-07-14 00:53:18 +04:00
|
|
|
void TransitionToRead();
|
|
|
|
void ReadShift();
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
const char *position_, *last_space_, *position_end_;
|
|
|
|
|
|
|
|
scoped_fd file_;
|
2011-11-11 00:46:59 +04:00
|
|
|
const uint64_t total_size_;
|
|
|
|
const uint64_t page_;
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-11-11 00:46:59 +04:00
|
|
|
std::size_t default_map_size_;
|
|
|
|
uint64_t mapped_offset_;
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
// 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-10-23 09:21:10 +04:00
|
|
|
|
|
|
|
std::string file_name_;
|
|
|
|
|
2012-10-30 23:33:22 +04:00
|
|
|
ReadCompressed fell_back_;
|
2010-09-10 04:36:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
|
|
|
#endif // UTIL_FILE_PIECE__
|