Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add readEOS flag to BlobReader and BlobIO so that EOF read logic work… #538

Merged
merged 1 commit into from
Sep 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/fileio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,33 @@ class BlobReader
const uint8_t* data;
size_t data_array_size;
size_t seek_pos;
bool readEOS;
public:
const int EOS = -1;

BlobReader(const uint8_t* _data, size_t _data_array_size)
: data(_data)
, data_array_size(_data_array_size)
, seek_pos(0)
, readEOS(false)
{
}

bool isEOF() const {
return seek_pos >= data_array_size;
if(readEOS) {
Copy link
Contributor

@lehitoskin lehitoskin Jul 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to return readEOS;

Copy link
Contributor Author

@mdejong mdejong Jul 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the form you describe makes it more difficult to actually debug the code when a developer wants to set a breakpoint that would get hit only in the EOF returns true case. By actually writing out the if block, the code can then be debugged with a breakpoint as is.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that most debuggers like gdb let you set conditional breakpoints for this purpose.

return true;
} else {
return false;
}
}
long ftell() const {
return seek_pos;
}
int get_c() {
if(seek_pos >= data_array_size)
if(seek_pos >= data_array_size) {
readEOS = true;
return EOS;
}
return data[seek_pos++];
}
char * gets(char *buf, int n) {
Expand All @@ -106,16 +114,19 @@ class BlobReader
buf[i++] = data[seek_pos++];
buf[n-1] = '\0';

if(i < max_write)
if(i < max_write) {
readEOS = true;
return 0;
else
} else {
return buf;
}
}
int fputc(int FLIF_UNUSED(c)) {
// cannot write on const memory
return EOS;
}
void fseek(long offset, int where) {
readEOS = false;
switch(where) {
case SEEK_SET:
seek_pos = offset;
Expand Down Expand Up @@ -146,8 +157,10 @@ class BlobIO
// keeps track how many bytes were written
size_t bytes_used;
size_t seek_pos;
bool readEOS;

void grow(size_t necessary_size) {
readEOS = false;
if(necessary_size < data_array_size)
return;

Expand Down Expand Up @@ -177,6 +190,7 @@ class BlobIO
, data_array_size(0)
, bytes_used(0)
, seek_pos(0)
, readEOS(false)
{
}

Expand All @@ -200,14 +214,20 @@ class BlobIO
// nothing to do
}
bool isEOF() const {
return seek_pos >= bytes_used;
if(readEOS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be simplified (identical to line 94).

return true;
} else {
return false;
}
}
int ftell() const {
long ftell() const {
return seek_pos;
}
int get_c() {
if(seek_pos >= bytes_used)
if(seek_pos >= bytes_used) {
readEOS = true;
return EOS;
}
return data[seek_pos++];
}
char * gets(char *buf, int n) {
Expand All @@ -217,10 +237,12 @@ class BlobIO
buf[i++] = data[seek_pos++];
buf[n-1] = '\0';

if(i < max_write)
if(i < max_write) {
readEOS = true;
return 0;
else
} else {
return buf;
}
}
int fputs(const char *s) {
size_t i = 0;
Expand All @@ -243,6 +265,7 @@ class BlobIO
return c;
}
void fseek(long offset, int where) {
readEOS = false;
switch(where) {
case SEEK_SET:
seek_pos = offset;
Expand Down