Skip to content

Commit

Permalink
Fixed issue 1133 - part3 (Nick's replacement of InputBuffer-ReadLine …
Browse files Browse the repository at this point in the history
…with InputBuffer-Read)
  • Loading branch information
zdenop committed May 1, 2015
1 parent 5e754af commit d1c749f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
41 changes: 11 additions & 30 deletions training/fileio.cpp
Expand Up @@ -76,11 +76,7 @@ bool File::ReadFileToString(const string& filename, string* out) {
return false;
InputBuffer in(stream);
*out = "";
string temp;
while (in.ReadLine(&temp)) {
*out += temp;
*out += '\n';
}
in.Read(out);
return in.CloseFile();
}

Expand Down Expand Up @@ -156,32 +152,17 @@ InputBuffer::~InputBuffer() {
}
}

bool InputBuffer::ReadLine(string* out) {
ASSERT_HOST(stream_ != NULL);
char* line = NULL;
int len = -1;
#ifndef HAVE_GETLINE
char line_buf[BUFSIZ];
if ((line = fgets(line_buf, BUFSIZ, stream_)) != NULL) {
len = strlen(line);
if (line_buf[0] != '\0' && line_buf[len - 1] == '\n')
line_buf[len - 1] = '\0';
} else {
return false;
}
*out = string(line);
#else
size_t line_size;
len = getline(&line, &line_size, stream_);
if (len < 0) {
return false;
bool InputBuffer::Read(string *out) {
char buf[BUFSIZ+1];
int l;
while((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
if(ferror(stream_)) {
clearerr(stream_);
return false;
}
buf[l] = 0;
out->append(buf);
}

if (len >= 1 && line[len - 1] == '\n')
line[len - 1] = '\0';
*out = string(line);
free(line);
#endif // HAVE_GETLINE
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions training/fileio.h
Expand Up @@ -68,6 +68,11 @@ class InputBuffer {
// Return false if an error occurs or at end-of-file, true otherwise.
bool ReadLine(string* out);

// Read data until end-of-file.
// The data is stored in '*out'.
// Return false if an error occurs, true otherwise.
bool Read(string* out);

// Close the FILE* used by InputBuffer.
// Return false if an error occurs, true otherwise.
bool CloseFile();
Expand Down

0 comments on commit d1c749f

Please sign in to comment.