diff -ur ScummVM-cvs20041016/common/file.cpp ScummVM-cvs20041016+hack/common/file.cpp
old
|
new
|
|
270 | 270 | return len; |
271 | 271 | } |
272 | 272 | |
| 273 | #define LF 0x0A |
| 274 | #define CR 0x0D |
| 275 | |
273 | 276 | char *File::gets(void *ptr, uint32 len) { |
274 | 277 | char *ptr2 = (char *)ptr; |
275 | | char *res; |
| 278 | char *res = ptr2; |
| 279 | uint32 read_chars = 1; |
276 | 280 | |
277 | 281 | if (_handle == NULL) { |
278 | 282 | error("File::gets: File is not open!"); |
279 | 283 | return 0; |
280 | 284 | } |
281 | 285 | |
282 | | if (len == 0) |
283 | | return 0; |
| 286 | if (len == 0 || !ptr) |
| 287 | return NULL; |
| 288 | |
| 289 | // We don't include the newline character(s) in the buffer, and we |
| 290 | // always terminate it - we never read more than len-1 characters. |
| 291 | |
| 292 | // EOF is treated as a line break, unless it was the first character |
| 293 | // that was read. |
| 294 | |
| 295 | // 0 is treated as a line break, even though it should never occur in |
| 296 | // a text file. |
| 297 | |
| 298 | // DOS and Windows use CRLF line breaks |
| 299 | // Unix and OS X use LF line breaks |
| 300 | // Macintosh before OS X uses CR line breaks |
| 301 | |
| 302 | bool first = true; |
284 | 303 | |
285 | | res = fgets(ptr2, len, _handle); |
| 304 | while (read_chars < len) { |
| 305 | int c = getc(_handle); |
| 306 | |
| 307 | if (c == EOF) { |
| 308 | if (first) |
| 309 | return NULL; |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | first = false; |
| 314 | |
| 315 | if (c == 0) |
| 316 | break; |
| 317 | |
| 318 | if (c == LF) |
| 319 | break; |
| 320 | |
| 321 | if (c == CR) { |
| 322 | c = getc(_handle); |
| 323 | if (c != LF) |
| 324 | ungetc(c, _handle); |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | *ptr2++ = (char) c; |
| 329 | read_chars++; |
| 330 | } |
286 | 331 | |
| 332 | *ptr2 = 0; |
287 | 333 | return res; |
288 | 334 | } |