Skip to content

Commit d1c749f

Browse files
committed
Fixed issue 1133 - part3 (Nick's replacement of InputBuffer-ReadLine with InputBuffer-Read)
1 parent 5e754af commit d1c749f

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

training/fileio.cpp

+11-30
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ bool File::ReadFileToString(const string& filename, string* out) {
7676
return false;
7777
InputBuffer in(stream);
7878
*out = "";
79-
string temp;
80-
while (in.ReadLine(&temp)) {
81-
*out += temp;
82-
*out += '\n';
83-
}
79+
in.Read(out);
8480
return in.CloseFile();
8581
}
8682

@@ -156,32 +152,17 @@ InputBuffer::~InputBuffer() {
156152
}
157153
}
158154

159-
bool InputBuffer::ReadLine(string* out) {
160-
ASSERT_HOST(stream_ != NULL);
161-
char* line = NULL;
162-
int len = -1;
163-
#ifndef HAVE_GETLINE
164-
char line_buf[BUFSIZ];
165-
if ((line = fgets(line_buf, BUFSIZ, stream_)) != NULL) {
166-
len = strlen(line);
167-
if (line_buf[0] != '\0' && line_buf[len - 1] == '\n')
168-
line_buf[len - 1] = '\0';
169-
} else {
170-
return false;
171-
}
172-
*out = string(line);
173-
#else
174-
size_t line_size;
175-
len = getline(&line, &line_size, stream_);
176-
if (len < 0) {
177-
return false;
155+
bool InputBuffer::Read(string *out) {
156+
char buf[BUFSIZ+1];
157+
int l;
158+
while((l = fread(buf, 1, BUFSIZ, stream_)) > 0) {
159+
if(ferror(stream_)) {
160+
clearerr(stream_);
161+
return false;
162+
}
163+
buf[l] = 0;
164+
out->append(buf);
178165
}
179-
180-
if (len >= 1 && line[len - 1] == '\n')
181-
line[len - 1] = '\0';
182-
*out = string(line);
183-
free(line);
184-
#endif // HAVE_GETLINE
185166
return true;
186167
}
187168

training/fileio.h

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class InputBuffer {
6868
// Return false if an error occurs or at end-of-file, true otherwise.
6969
bool ReadLine(string* out);
7070

71+
// Read data until end-of-file.
72+
// The data is stored in '*out'.
73+
// Return false if an error occurs, true otherwise.
74+
bool Read(string* out);
75+
7176
// Close the FILE* used by InputBuffer.
7277
// Return false if an error occurs, true otherwise.
7378
bool CloseFile();

0 commit comments

Comments
 (0)