File tree 2 files changed +16
-30
lines changed
2 files changed +16
-30
lines changed Original file line number Diff line number Diff line change @@ -76,11 +76,7 @@ bool File::ReadFileToString(const string& filename, string* out) {
76
76
return false ;
77
77
InputBuffer in (stream);
78
78
*out = " " ;
79
- string temp;
80
- while (in.ReadLine (&temp)) {
81
- *out += temp;
82
- *out += ' \n ' ;
83
- }
79
+ in.Read (out);
84
80
return in.CloseFile ();
85
81
}
86
82
@@ -156,32 +152,17 @@ InputBuffer::~InputBuffer() {
156
152
}
157
153
}
158
154
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);
178
165
}
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
185
166
return true ;
186
167
}
187
168
Original file line number Diff line number Diff line change @@ -68,6 +68,11 @@ class InputBuffer {
68
68
// Return false if an error occurs or at end-of-file, true otherwise.
69
69
bool ReadLine (string* out);
70
70
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
+
71
76
// Close the FILE* used by InputBuffer.
72
77
// Return false if an error occurs, true otherwise.
73
78
bool CloseFile ();
You can’t perform that action at this time.
0 commit comments