1. open readable file in "in" mode vs. "out" mode

2. open readable file as binary stream for text file to avoid reading partial file due to u0x1a
This commit is contained in:
Gary Gao 2018-12-12 15:14:14 -08:00
Родитель 1a38d26a13
Коммит 939804c93b
3 изменённых файлов: 6 добавлений и 10 удалений

Просмотреть файл

@ -22,12 +22,10 @@ namespace filesystem {
class PosixReadableFile : public ReadableFile {
public:
PosixReadableFile(absl::string_view filename, bool is_binary = false)
PosixReadableFile(absl::string_view filename)
: is_(filename.empty()
? &std::cin
: new std::ifstream(WPATH(filename.data()),
is_binary ? std::ios::binary | std::ios::in
: std::ios::out)) {
: new std::ifstream(WPATH(filename.data()), std::ios::binary | std::ios::in)) {
if (!*is_)
status_ = util::StatusBuilder(util::error::NOT_FOUND)
<< "\"" << filename.data() << "\": " << util::StrError(errno);
@ -89,9 +87,8 @@ class PosixWritableFile : public WritableFile {
std::ostream *os_;
};
std::unique_ptr<ReadableFile> NewReadableFile(absl::string_view filename,
bool is_binary) {
return port::MakeUnique<PosixReadableFile>(filename, is_binary);
std::unique_ptr<ReadableFile> NewReadableFile(absl::string_view filename) {
return port::MakeUnique<PosixReadableFile>(filename);
}
std::unique_ptr<WritableFile> NewWritableFile(absl::string_view filename,

Просмотреть файл

@ -48,8 +48,7 @@ class WritableFile {
virtual bool WriteLine(absl::string_view text) = 0;
};
std::unique_ptr<ReadableFile> NewReadableFile(absl::string_view filename,
bool is_binary = false);
std::unique_ptr<ReadableFile> NewReadableFile(absl::string_view filename);
std::unique_ptr<WritableFile> NewWritableFile(absl::string_view filename,
bool is_binary = false);

Просмотреть файл

@ -43,7 +43,7 @@ SentencePieceProcessor::SentencePieceProcessor() {}
SentencePieceProcessor::~SentencePieceProcessor() {}
util::Status SentencePieceProcessor::Load(util::min_string_view filename) {
auto input = filesystem::NewReadableFile(string_util::ToSV(filename), true);
auto input = filesystem::NewReadableFile(string_util::ToSV(filename));
RETURN_IF_ERROR(input->status());
std::string proto;
CHECK_OR_RETURN(input->ReadAll(&proto));