Skip to content

Commit

Permalink
Add eof() function to reader APIs (#105)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #105

We need to know whether the underlying source has any more data, otherwise we won't know whether to stop reading

Reviewed By: elliottlawrence

Differential Revision: D34967134

fbshipit-source-id: 8ac2f71747e534a63f75cac8e30aaf599922cab6
  • Loading branch information
adshastri authored and facebook-github-bot committed Mar 21, 2022
1 parent f8863c8 commit cf5b501
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fbpcf/io/api/CloudFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ int CloudFileReader::close() {
size_t CloudFileReader::read(std::vector<char>& /* buf */) {
return 0;
}
bool CloudFileReader::eof() {
return false;
}

CloudFileReader::~CloudFileReader() {
close();
Expand Down
1 change: 1 addition & 0 deletions fbpcf/io/api/CloudFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CloudFileReader : public IReaderCloser {

int close() override;
size_t read(std::vector<char>& buf) override;
bool eof() override;
~CloudFileReader() override;
};

Expand Down
4 changes: 4 additions & 0 deletions fbpcf/io/api/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ size_t FileReader::read(std::vector<char>& buf) {
return childReader_->read(buf);
}

bool FileReader::eof() {
return childReader_->eof();
}

int FileReader::close() {
return childReader_->close();
}
Expand Down
1 change: 1 addition & 0 deletions fbpcf/io/api/FileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class FileReader : public IReaderCloser {

int close() override;
size_t read(std::vector<char>& buf) override;
bool eof() override;
~FileReader() override;

private:
Expand Down
5 changes: 5 additions & 0 deletions fbpcf/io/api/IReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class IReader {
* read
*/
virtual size_t read(std::vector<char>& buf) = 0;
/*
* eof() returns whether there is any more
* data left in the file
*/
virtual bool eof() = 0;
virtual ~IReader() = default;
};

Expand Down
4 changes: 4 additions & 0 deletions fbpcf/io/api/LocalFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ size_t LocalFileReader::read(std::vector<char>& buf) {
return inputStream_->gcount();
}

bool LocalFileReader::eof() {
return inputStream_->eof();
}

LocalFileReader::~LocalFileReader() {
close();
}
Expand Down
1 change: 1 addition & 0 deletions fbpcf/io/api/LocalFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LocalFileReader : public IReaderCloser {

int close() override;
size_t read(std::vector<char>& buf) override;
bool eof() override;
~LocalFileReader() override;

private:
Expand Down
1 change: 1 addition & 0 deletions fbpcf/io/api/SocketReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SocketReader : public IReaderCloser {

int close() override;
size_t read(std::vector<char>& buf) override;
bool eof() override;
~SocketReader() override;
};

Expand Down
6 changes: 6 additions & 0 deletions fbpcf/io/api/test/LocalFileReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ TEST(LocalFileReaderTest, testReadingFromFile) {
IOTestHelper::getBaseDirFromPath(__FILE__) +
"data/local_file_reader_test_file.txt");

EXPECT_FALSE(reader->eof());

/*
CASE 1A
Buffer of size 20, read 20 bytes
Expand All @@ -27,6 +29,7 @@ TEST(LocalFileReaderTest, testReadingFromFile) {

EXPECT_EQ(nBytes, 20);
IOTestHelper::expectBufferToEqualString(buf, "this is a test file\n", 20);
EXPECT_FALSE(reader->eof());

/*
CASE 1B
Expand All @@ -37,6 +40,7 @@ TEST(LocalFileReaderTest, testReadingFromFile) {
EXPECT_EQ(nBytes, 25);
IOTestHelper::expectBufferToEqualString(
buf2, "it has many lines in it\n\n", 25);
EXPECT_FALSE(reader->eof());

/*
CASE 2
Expand All @@ -51,6 +55,8 @@ TEST(LocalFileReaderTest, testReadingFromFile) {
IOTestHelper::expectBufferToEqualString(
buf3, "the quick brown fox jumped over the lazy dog\n", 45);

EXPECT_TRUE(reader->eof());

EXPECT_THROW(reader->read(buf3), std::runtime_error);
}

Expand Down

0 comments on commit cf5b501

Please sign in to comment.