(_file_strerror): New function.
(file_stream_create): Register strerror function. (stdio_stream_create): Enable caching only if the file is not seekable and MU_STREAM_SEEKABLE bit is set in flags. Register different sets of (read,readline,write) handlers depending on the setting of MU_STREAM_SEEKABLE bit.
Showing
1 changed file
with
40 additions
and
21 deletions
... | @@ -218,17 +218,17 @@ _stdin_file_read (stream_t stream, char *optr, size_t osize, | ... | @@ -218,17 +218,17 @@ _stdin_file_read (stream_t stream, char *optr, size_t osize, |
218 | nbytes = 0; | 218 | nbytes = 0; |
219 | else | 219 | else |
220 | { | 220 | { |
221 | status = _file_read (stream, optr, osize, fs_offset, &nbytes); | 221 | status = _file_read (stream, optr, osize, fs_offset, &nbytes); |
222 | if (status == 0 && nbytes) | 222 | if (status == 0 && nbytes) |
223 | { | 223 | { |
224 | size_t k; | 224 | size_t k; |
225 | 225 | ||
226 | status = stream_write (fs->cache, optr, nbytes, fs_offset, &k); | 226 | status = stream_write (fs->cache, optr, nbytes, fs_offset, &k); |
227 | if (status) | 227 | if (status) |
228 | return status; | 228 | return status; |
229 | if (k != nbytes) | 229 | if (k != nbytes) |
230 | return EIO; | 230 | return EIO; |
231 | } | 231 | } |
232 | } | 232 | } |
233 | if (pnbytes) | 233 | if (pnbytes) |
234 | *pnbytes = nbytes; | 234 | *pnbytes = nbytes; |
... | @@ -464,6 +464,13 @@ _file_open (stream_t stream) | ... | @@ -464,6 +464,13 @@ _file_open (stream_t stream) |
464 | } | 464 | } |
465 | 465 | ||
466 | int | 466 | int |
467 | _file_strerror (stream_t unused, const char **pstr) | ||
468 | { | ||
469 | *pstr = strerror (errno); | ||
470 | return 0; | ||
471 | } | ||
472 | |||
473 | int | ||
467 | file_stream_create (stream_t *stream, const char* filename, int flags) | 474 | file_stream_create (stream_t *stream, const char* filename, int flags) |
468 | { | 475 | { |
469 | struct _file_stream *fs; | 476 | struct _file_stream *fs; |
... | @@ -500,12 +507,13 @@ file_stream_create (stream_t *stream, const char* filename, int flags) | ... | @@ -500,12 +507,13 @@ file_stream_create (stream_t *stream, const char* filename, int flags) |
500 | stream_set_size (*stream, _file_size, fs); | 507 | stream_set_size (*stream, _file_size, fs); |
501 | stream_set_flush (*stream, _file_flush, fs); | 508 | stream_set_flush (*stream, _file_flush, fs); |
502 | stream_set_destroy (*stream, _file_destroy, fs); | 509 | stream_set_destroy (*stream, _file_destroy, fs); |
503 | 510 | stream_set_strerror (*stream, _file_strerror, fs); | |
511 | |||
504 | return 0; | 512 | return 0; |
505 | } | 513 | } |
506 | 514 | ||
507 | int | 515 | int |
508 | stdio_stream_create (stream_t *stream, FILE* file, int flags) | 516 | stdio_stream_create (stream_t *stream, FILE *file, int flags) |
509 | { | 517 | { |
510 | struct _file_stream *fs; | 518 | struct _file_stream *fs; |
511 | int ret; | 519 | int ret; |
... | @@ -529,22 +537,33 @@ stdio_stream_create (stream_t *stream, FILE* file, int flags) | ... | @@ -529,22 +537,33 @@ stdio_stream_create (stream_t *stream, FILE* file, int flags) |
529 | return ret; | 537 | return ret; |
530 | } | 538 | } |
531 | 539 | ||
532 | if ((ret = memory_stream_create (&fs->cache, 0, MU_STREAM_RDWR)) | 540 | /* Check if we need to enable caching */ |
533 | || (ret = stream_open (fs->cache))) | 541 | |
542 | if ((flags & MU_STREAM_SEEKABLE) && lseek (fileno (file), 0, 0)) | ||
534 | { | 543 | { |
535 | stream_destroy (stream, fs); | 544 | if ((ret = memory_stream_create (&fs->cache, 0, MU_STREAM_RDWR)) |
536 | free (fs); | 545 | || (ret = stream_open (fs->cache))) |
537 | return ret; | 546 | { |
547 | stream_destroy (stream, fs); | ||
548 | free (fs); | ||
549 | return ret; | ||
550 | } | ||
551 | stream_set_read (*stream, _stdin_file_read, fs); | ||
552 | stream_set_readline (*stream, _stdin_file_readline, fs); | ||
553 | stream_set_write (*stream, _stdout_file_write, fs); | ||
538 | } | 554 | } |
539 | 555 | else | |
556 | { | ||
557 | stream_set_read (*stream, _file_read, fs); | ||
558 | stream_set_readline (*stream, _file_readline, fs); | ||
559 | stream_set_write (*stream, _file_write, fs); | ||
560 | } | ||
561 | |||
540 | /* We don't need to open the FILE, just return success. */ | 562 | /* We don't need to open the FILE, just return success. */ |
541 | 563 | ||
542 | stream_set_open (*stream, NULL, fs); | 564 | stream_set_open (*stream, NULL, fs); |
543 | stream_set_close (*stream, _file_close, fs); | 565 | stream_set_close (*stream, _file_close, fs); |
544 | stream_set_fd (*stream, _file_get_fd, fs); | 566 | stream_set_fd (*stream, _file_get_fd, fs); |
545 | stream_set_read (*stream, _stdin_file_read, fs); | ||
546 | stream_set_readline (*stream, _stdin_file_readline, fs); | ||
547 | stream_set_write (*stream, _stdout_file_write, fs); | ||
548 | stream_set_flush (*stream, _file_flush, fs); | 567 | stream_set_flush (*stream, _file_flush, fs); |
549 | stream_set_destroy (*stream, _file_destroy, fs); | 568 | stream_set_destroy (*stream, _file_destroy, fs); |
550 | 569 | ... | ... |
-
Please register or sign in to post a comment