Fixed issue 1133 - part3 (Nick's replacement of InputBuffer-ReadLine with InputBuffer-Read)

This commit is contained in:
Zdenko Podobný 2015-05-01 19:33:56 +02:00
parent 5e754af9cb
commit d1c749f6ad
2 changed files with 16 additions and 30 deletions

View File

@ -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();
}
@ -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;
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);
}
*out = string(line);
#else
size_t line_size;
len = getline(&line, &line_size, stream_);
if (len < 0) {
return false;
}
if (len >= 1 && line[len - 1] == '\n')
line[len - 1] = '\0';
*out = string(line);
free(line);
#endif // HAVE_GETLINE
return true;
}

View File

@ -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();