2014-06-02 21:28:02 +04:00
|
|
|
#ifndef UTIL_FILE_PIECE_H
|
|
|
|
#define UTIL_FILE_PIECE_H
|
2010-09-10 04:36:07 +04:00
|
|
|
|
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>
|
2013-01-23 00:23:35 +04:00
|
|
|
#include <iosfwd>
|
2010-09-10 04:36:07 +04:00
|
|
|
#include <string>
|
2015-03-28 15:37:48 +03:00
|
|
|
#include <cassert>
|
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
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
/* Read from an istream. Don't use this if you can avoid it. Raw fd IO is
|
|
|
|
* much faster. But sometimes you just have an istream like Boost's HTTP
|
|
|
|
* server and want to parse it the same way.
|
|
|
|
* name is just used for messages and FileName().
|
|
|
|
*/
|
|
|
|
explicit FilePiece(std::istream &stream, const char *name = 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
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
// Read word until the line or file ends.
|
|
|
|
bool ReadWordSameLine(StringPiece &to, const bool *delim = kSpaces) {
|
|
|
|
assert(delim[static_cast<unsigned char>('\n')]);
|
|
|
|
// Skip non-enter spaces.
|
|
|
|
for (; ; ++position_) {
|
|
|
|
if (position_ == position_end_) {
|
|
|
|
try {
|
|
|
|
Shift();
|
|
|
|
} catch (const util::EndOfFileException &e) { return false; }
|
|
|
|
// And break out at end of file.
|
|
|
|
if (position_ == position_end_) return false;
|
|
|
|
}
|
|
|
|
if (!delim[static_cast<unsigned char>(*position_)]) break;
|
|
|
|
if (*position_ == '\n') return false;
|
|
|
|
}
|
|
|
|
// We can't be at the end of file because there's at least one character open.
|
|
|
|
to = Consume(FindDelimiterOrEOF(delim));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
// Doesn't throw EndOfFileException, just returns false.
|
|
|
|
bool ReadLineOrEOF(StringPiece &to, char delim = '\n');
|
|
|
|
|
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) {
|
2014-01-02 01:19:06 +04:00
|
|
|
assert(position_ <= position_end_);
|
2011-01-25 22:11:48 +03:00
|
|
|
for (; ; ++position_) {
|
2014-01-02 01:19:06 +04:00
|
|
|
if (position_ == position_end_) {
|
|
|
|
Shift();
|
|
|
|
// And break out at end of file.
|
|
|
|
if (position_ == position_end_) return;
|
|
|
|
}
|
|
|
|
assert(position_ < position_end_);
|
2011-01-25 22:11:48 +03:00
|
|
|
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:
|
2013-01-23 00:23:35 +04:00
|
|
|
void InitializeNoRead(const char *name, std::size_t min_buffer);
|
|
|
|
// Calls InitializeNoRead, so don't call both.
|
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) {
|
2014-01-02 01:19:06 +04:00
|
|
|
assert(to >= position_);
|
2010-09-10 04:36:07 +04:00
|
|
|
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
|
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
#endif // UTIL_FILE_PIECE_H
|