Mercurial > hg > xemacs-beta
comparison src/lstream.c @ 462:0784d089fdc9 r21-2-46
Import from CVS: tag r21-2-46
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:44:37 +0200 |
parents | e7ef97881643 |
children | 183866b06e0b |
comparison
equal
deleted
inserted
replaced
461:120ed4009e51 | 462:0784d089fdc9 |
---|---|
93 int Lstream_fputc (Lstream *stream, int c) | 93 int Lstream_fputc (Lstream *stream, int c) |
94 int Lstream_fgetc (Lstream *stream) | 94 int Lstream_fgetc (Lstream *stream) |
95 void Lstream_fungetc (Lstream *stream, int c) | 95 void Lstream_fungetc (Lstream *stream, int c) |
96 Function equivalents of the above macros. | 96 Function equivalents of the above macros. |
97 | 97 |
98 ssize_t Lstream_read (Lstream *stream, void *data, size_t size) | 98 Lstream_data_count Lstream_read (Lstream *stream, void *data, |
99 Lstream_data_count size) | |
99 Read SIZE bytes of DATA from the stream. Return the number of | 100 Read SIZE bytes of DATA from the stream. Return the number of |
100 bytes read. 0 means EOF. -1 means an error occurred and no | 101 bytes read. 0 means EOF. -1 means an error occurred and no |
101 bytes were read. | 102 bytes were read. |
102 | 103 |
103 ssize_t Lstream_write (Lstream *stream, void *data, size_t size) | 104 Lstream_data_count Lstream_write (Lstream *stream, void *data, |
105 Lstream_data_count size) | |
104 Write SIZE bytes of DATA to the stream. Return the number of | 106 Write SIZE bytes of DATA to the stream. Return the number of |
105 bytes written. -1 means an error occurred and no bytes were | 107 bytes written. -1 means an error occurred and no bytes were |
106 written. | 108 written. |
107 | 109 |
108 void Lstream_unread (Lstream *stream, void *data, size_t size) | 110 void Lstream_unread (Lstream *stream, void *data, Lstream_data_count size) |
109 Push back SIZE bytes of DATA onto the input queue. The | 111 Push back SIZE bytes of DATA onto the input queue. The |
110 next call to Lstream_read() with the same size will read the | 112 next call to Lstream_read() with the same size will read the |
111 same bytes back. Note that this will be the case even if | 113 same bytes back. Note that this will be the case even if |
112 there is other pending unread data. | 114 there is other pending unread data. |
113 | 115 |
306 /* Attempt to flush out all of the buffered data for writing. */ | 308 /* Attempt to flush out all of the buffered data for writing. */ |
307 | 309 |
308 int | 310 int |
309 Lstream_flush_out (Lstream *lstr) | 311 Lstream_flush_out (Lstream *lstr) |
310 { | 312 { |
311 ssize_t num_written; | 313 Lstream_data_count num_written; |
312 | 314 |
313 while (lstr->out_buffer_ind > 0) | 315 while (lstr->out_buffer_ind > 0) |
314 { | 316 { |
315 size_t size = lstr->out_buffer_ind; | 317 Lstream_data_count size = lstr->out_buffer_ind; |
316 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 318 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
317 Lstream_internal_error ("lstream not open", lstr); | 319 Lstream_internal_error ("lstream not open", lstr); |
318 if (! (lstr->flags & LSTREAM_FL_WRITE)) | 320 if (! (lstr->flags & LSTREAM_FL_WRITE)) |
319 Lstream_internal_error ("lstream not open for writing", lstr); | 321 Lstream_internal_error ("lstream not open for writing", lstr); |
320 if (!lstr->imp->writer) | 322 if (!lstr->imp->writer) |
393 the buffering size. (This is used to deal with the possibility | 395 the buffering size. (This is used to deal with the possibility |
394 that the stream writer might refuse to write any bytes now, e.g. | 396 that the stream writer might refuse to write any bytes now, e.g. |
395 if it's getting EWOULDBLOCK errors. We have to keep stocking them | 397 if it's getting EWOULDBLOCK errors. We have to keep stocking them |
396 up until they can be written, so as to avoid losing data. */ | 398 up until they can be written, so as to avoid losing data. */ |
397 | 399 |
398 static size_t | 400 static Lstream_data_count |
399 Lstream_adding (Lstream *lstr, size_t num, int force) | 401 Lstream_adding (Lstream *lstr, Lstream_data_count num, int force) |
400 { | 402 { |
401 size_t size = num + lstr->out_buffer_ind; | 403 Lstream_data_count size = num + lstr->out_buffer_ind; |
402 | 404 |
403 if (size <= lstr->out_buffer_size) | 405 if (size <= lstr->out_buffer_size) |
404 return num; | 406 return num; |
405 | 407 |
406 /* Maybe chop it down so that we don't buffer more characters | 408 /* Maybe chop it down so that we don't buffer more characters |
418 return size - lstr->out_buffer_ind; | 420 return size - lstr->out_buffer_ind; |
419 } | 421 } |
420 | 422 |
421 /* Like Lstream_write(), but does not handle line-buffering correctly. */ | 423 /* Like Lstream_write(), but does not handle line-buffering correctly. */ |
422 | 424 |
423 static ssize_t | 425 static Lstream_data_count |
424 Lstream_write_1 (Lstream *lstr, const void *data, size_t size) | 426 Lstream_write_1 (Lstream *lstr, const void *data, Lstream_data_count size) |
425 { | 427 { |
426 const unsigned char *p = (const unsigned char *) data; | 428 const unsigned char *p = (const unsigned char *) data; |
427 ssize_t off = 0; | 429 Lstream_data_count off = 0; |
428 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 430 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
429 Lstream_internal_error ("lstream not open", lstr); | 431 Lstream_internal_error ("lstream not open", lstr); |
430 if (! (lstr->flags & LSTREAM_FL_WRITE)) | 432 if (! (lstr->flags & LSTREAM_FL_WRITE)) |
431 Lstream_internal_error ("lstream not open for writing", lstr); | 433 Lstream_internal_error ("lstream not open for writing", lstr); |
432 { | 434 { |
433 int couldnt_write_last_time = 0; | 435 int couldnt_write_last_time = 0; |
434 | 436 |
435 while (1) | 437 while (1) |
436 { | 438 { |
437 /* Figure out how much we can add to the buffer */ | 439 /* Figure out how much we can add to the buffer */ |
438 size_t chunk = Lstream_adding (lstr, size, 0); | 440 Lstream_data_count chunk = Lstream_adding (lstr, size, 0); |
439 if (chunk == 0) | 441 if (chunk == 0) |
440 { | 442 { |
441 if (couldnt_write_last_time) | 443 if (couldnt_write_last_time) |
442 /* Ung, we ran out of space and tried to flush | 444 /* Ung, we ran out of space and tried to flush |
443 the buffer, but it didn't work because the stream | 445 the buffer, but it didn't work because the stream |
478 /* If the stream is not line-buffered, then we can just call | 480 /* If the stream is not line-buffered, then we can just call |
479 Lstream_write_1(), which writes in chunks. Otherwise, we | 481 Lstream_write_1(), which writes in chunks. Otherwise, we |
480 repeatedly call Lstream_putc(), which knows how to handle | 482 repeatedly call Lstream_putc(), which knows how to handle |
481 line buffering. Returns number of bytes written. */ | 483 line buffering. Returns number of bytes written. */ |
482 | 484 |
483 ssize_t | 485 Lstream_data_count |
484 Lstream_write (Lstream *lstr, const void *data, size_t size) | 486 Lstream_write (Lstream *lstr, const void *data, Lstream_data_count size) |
485 { | 487 { |
486 size_t i; | 488 Lstream_data_count i; |
487 const unsigned char *p = (const unsigned char *) data; | 489 const unsigned char *p = (const unsigned char *) data; |
488 | 490 |
489 if (size == 0) | 491 if (size == 0) |
490 return size; | 492 return size; |
491 if (lstr->buffering != LSTREAM_LINE_BUFFERED) | 493 if (lstr->buffering != LSTREAM_LINE_BUFFERED) |
493 for (i = 0; i < size; i++) | 495 for (i = 0; i < size; i++) |
494 { | 496 { |
495 if (Lstream_putc (lstr, p[i]) < 0) | 497 if (Lstream_putc (lstr, p[i]) < 0) |
496 break; | 498 break; |
497 } | 499 } |
498 return i == 0 ? -1 : (ssize_t) i; | 500 return i == 0 ? -1 : i; |
499 } | 501 } |
500 | 502 |
501 int | 503 int |
502 Lstream_was_blocked_p (Lstream *lstr) | 504 Lstream_was_blocked_p (Lstream *lstr) |
503 { | 505 { |
504 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; | 506 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; |
505 } | 507 } |
506 | 508 |
507 static ssize_t | 509 static Lstream_data_count |
508 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, size_t size) | 510 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, |
511 Lstream_data_count size) | |
509 { | 512 { |
510 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 513 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
511 Lstream_internal_error ("lstream not open", lstr); | 514 Lstream_internal_error ("lstream not open", lstr); |
512 if (! (lstr->flags & LSTREAM_FL_READ)) | 515 if (! (lstr->flags & LSTREAM_FL_READ)) |
513 Lstream_internal_error ("lstream not open for reading", lstr); | 516 Lstream_internal_error ("lstream not open for reading", lstr); |
517 return (lstr->imp->reader) (lstr, buffer, size); | 520 return (lstr->imp->reader) (lstr, buffer, size); |
518 } | 521 } |
519 | 522 |
520 /* Assuming the buffer is empty, fill it up again. */ | 523 /* Assuming the buffer is empty, fill it up again. */ |
521 | 524 |
522 static ssize_t | 525 static Lstream_data_count |
523 Lstream_read_more (Lstream *lstr) | 526 Lstream_read_more (Lstream *lstr) |
524 { | 527 { |
525 #if 0 | 528 #if 0 |
526 ssize_t size_needed = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); | 529 Lstream_data_count size_needed |
530 = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); | |
527 #else | 531 #else |
528 /* If someone requested a larger buffer size, so be it! */ | 532 /* If someone requested a larger buffer size, so be it! */ |
529 ssize_t size_needed = max (1, lstr->buffering_size); | 533 Lstream_data_count size_needed = |
534 max (1, lstr->buffering_size); | |
530 #endif | 535 #endif |
531 ssize_t size_gotten; | 536 Lstream_data_count size_gotten; |
532 | 537 |
533 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, | 538 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, |
534 size_needed, unsigned char); | 539 size_needed, unsigned char); |
535 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); | 540 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); |
536 lstr->in_buffer_current = max (0, size_gotten); | 541 lstr->in_buffer_current = max (0, size_gotten); |
537 lstr->in_buffer_ind = 0; | 542 lstr->in_buffer_ind = 0; |
538 return size_gotten < 0 ? -1 : size_gotten; | 543 return size_gotten < 0 ? -1 : size_gotten; |
539 } | 544 } |
540 | 545 |
541 ssize_t | 546 Lstream_data_count |
542 Lstream_read (Lstream *lstr, void *data, size_t size) | 547 Lstream_read (Lstream *lstr, void *data, Lstream_data_count size) |
543 { | 548 { |
544 unsigned char *p = (unsigned char *) data; | 549 unsigned char *p = (unsigned char *) data; |
545 size_t off = 0; | 550 Lstream_data_count off = 0; |
546 size_t chunk; | 551 Lstream_data_count chunk; |
547 int error_occurred = 0; | 552 int error_occurred = 0; |
548 | 553 |
549 if (size == 0) | 554 if (size == 0) |
550 return 0; | 555 return 0; |
551 | 556 |
574 } | 579 } |
575 | 580 |
576 /* If we need some more, try to get some more from the stream's end */ | 581 /* If we need some more, try to get some more from the stream's end */ |
577 if (size > 0) | 582 if (size > 0) |
578 { | 583 { |
579 ssize_t retval = Lstream_read_more (lstr); | 584 Lstream_data_count retval = Lstream_read_more (lstr); |
580 if (retval < 0) | 585 if (retval < 0) |
581 error_occurred = 1; | 586 error_occurred = 1; |
582 if (retval <= 0) | 587 if (retval <= 0) |
583 break; | 588 break; |
584 } | 589 } |
598 character, and bump forward to see if the character is | 603 character, and bump forward to see if the character is |
599 complete. */ | 604 complete. */ |
600 VALIDATE_CHARPTR_BACKWARD (dataend); | 605 VALIDATE_CHARPTR_BACKWARD (dataend); |
601 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off) | 606 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off) |
602 { | 607 { |
603 size_t newoff = dataend - p; | 608 Lstream_data_count newoff = dataend - p; |
604 /* If not, chop the size down to ignore the last char | 609 /* If not, chop the size down to ignore the last char |
605 and stash it away for next time. */ | 610 and stash it away for next time. */ |
606 Lstream_unread (lstr, dataend, off - newoff); | 611 Lstream_unread (lstr, dataend, off - newoff); |
607 off = newoff; | 612 off = newoff; |
608 } | 613 } |
609 } | 614 } |
610 } | 615 } |
611 | 616 |
612 return off == 0 && error_occurred ? -1 : (ssize_t) off; | 617 return off == 0 && error_occurred ? -1 : off; |
613 } | 618 } |
614 | 619 |
615 void | 620 void |
616 Lstream_unread (Lstream *lstr, const void *data, size_t size) | 621 Lstream_unread (Lstream *lstr, const void *data, Lstream_data_count size) |
617 { | 622 { |
618 const unsigned char *p = (const unsigned char *) data; | 623 const unsigned char *p = (const unsigned char *) data; |
619 | 624 |
620 /* Make sure buffer is big enough */ | 625 /* Make sure buffer is big enough */ |
621 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, | 626 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, |
730 | 735 |
731 int | 736 int |
732 Lstream_fputc (Lstream *lstr, int c) | 737 Lstream_fputc (Lstream *lstr, int c) |
733 { | 738 { |
734 unsigned char ch = (unsigned char) c; | 739 unsigned char ch = (unsigned char) c; |
735 ssize_t retval = Lstream_write_1 (lstr, &ch, 1); | 740 Lstream_data_count retval = Lstream_write_1 (lstr, &ch, 1); |
736 if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') | 741 if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') |
737 return Lstream_flush_out (lstr); | 742 return Lstream_flush_out (lstr); |
738 return retval < 0 ? -1 : 0; | 743 return retval < 0 ? -1 : 0; |
739 } | 744 } |
740 | 745 |
811 | 816 |
812 This is probably reasonable, so I don't think we should change this | 817 This is probably reasonable, so I don't think we should change this |
813 code (it could even be argued that the error might have fixed | 818 code (it could even be argued that the error might have fixed |
814 itself, so we should do the fread() again. */ | 819 itself, so we should do the fread() again. */ |
815 | 820 |
816 static ssize_t | 821 static Lstream_data_count |
817 stdio_reader (Lstream *stream, unsigned char *data, size_t size) | 822 stdio_reader (Lstream *stream, unsigned char *data, Lstream_data_count size) |
818 { | 823 { |
819 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | 824 struct stdio_stream *str = STDIO_STREAM_DATA (stream); |
820 size_t val = fread (data, 1, size, str->file); | 825 Lstream_data_count val = fread (data, 1, size, str->file); |
821 if (!val && ferror (str->file)) | 826 if (!val && ferror (str->file)) |
822 return -1; | 827 return -1; |
823 return val; | 828 return val; |
824 } | 829 } |
825 | 830 |
826 static ssize_t | 831 static Lstream_data_count |
827 stdio_writer (Lstream *stream, const unsigned char *data, size_t size) | 832 stdio_writer (Lstream *stream, const unsigned char *data, |
833 Lstream_data_count size) | |
828 { | 834 { |
829 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | 835 struct stdio_stream *str = STDIO_STREAM_DATA (stream); |
830 size_t val = fwrite (data, 1, size, str->file); | 836 Lstream_data_count val = fwrite (data, 1, size, str->file); |
831 if (!val && ferror (str->file)) | 837 if (!val && ferror (str->file)) |
832 return -1; | 838 return -1; |
833 return val; | 839 return val; |
834 } | 840 } |
835 | 841 |
936 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) | 942 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) |
937 { | 943 { |
938 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); | 944 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); |
939 } | 945 } |
940 | 946 |
941 static ssize_t | 947 static Lstream_data_count |
942 filedesc_reader (Lstream *stream, unsigned char *data, size_t size) | 948 filedesc_reader (Lstream *stream, unsigned char *data, Lstream_data_count size) |
943 { | 949 { |
944 ssize_t nread; | 950 Lstream_data_count nread; |
945 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 951 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
946 if (str->end_pos >= 0) | 952 if (str->end_pos >= 0) |
947 size = min (size, (size_t) (str->end_pos - str->current_pos)); | 953 size = min (size, (Lstream_data_count) (str->end_pos - str->current_pos)); |
948 nread = str->allow_quit ? | 954 nread = str->allow_quit ? |
949 read_allowing_quit (str->fd, data, size) : | 955 read_allowing_quit (str->fd, data, size) : |
950 read (str->fd, data, size); | 956 read (str->fd, data, size); |
951 if (nread > 0) | 957 if (nread > 0) |
952 str->current_pos += nread; | 958 str->current_pos += nread; |
965 return 1; | 971 return 1; |
966 #endif | 972 #endif |
967 return 0; | 973 return 0; |
968 } | 974 } |
969 | 975 |
970 static ssize_t | 976 static Lstream_data_count |
971 filedesc_writer (Lstream *stream, const unsigned char *data, size_t size) | 977 filedesc_writer (Lstream *stream, const unsigned char *data, |
978 Lstream_data_count size) | |
972 { | 979 { |
973 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 980 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
974 ssize_t retval; | 981 Lstream_data_count retval; |
975 int need_newline = 0; | 982 int need_newline = 0; |
976 | 983 |
977 /* This function would be simple if it were not for the blasted | 984 /* This function would be simple if it were not for the blasted |
978 PTY max-bytes stuff. Why the hell can't they just have written | 985 PTY max-bytes stuff. Why the hell can't they just have written |
979 the PTY drivers right so this problem doesn't exist? | 986 the PTY drivers right so this problem doesn't exist? |
1023 and flush with an EOF if necessary. Be careful to | 1030 and flush with an EOF if necessary. Be careful to |
1024 keep track of write errors as we go along and look | 1031 keep track of write errors as we go along and look |
1025 out for EWOULDBLOCK. */ | 1032 out for EWOULDBLOCK. */ |
1026 if (str->chars_sans_newline >= str->pty_max_bytes) | 1033 if (str->chars_sans_newline >= str->pty_max_bytes) |
1027 { | 1034 { |
1028 ssize_t retval2 = str->allow_quit ? | 1035 Lstream_data_count retval2 = str->allow_quit ? |
1029 write_allowing_quit (str->fd, &str->eof_char, 1) : | 1036 write_allowing_quit (str->fd, &str->eof_char, 1) : |
1030 write (str->fd, &str->eof_char, 1); | 1037 write (str->fd, &str->eof_char, 1); |
1031 | 1038 |
1032 if (retval2 > 0) | 1039 if (retval2 > 0) |
1033 str->chars_sans_newline = 0; | 1040 str->chars_sans_newline = 0; |
1056 first byte is a newline, we'd get stuck never writing anything | 1063 first byte is a newline, we'd get stuck never writing anything |
1057 in pty-flushing mode. */ | 1064 in pty-flushing mode. */ |
1058 if (need_newline) | 1065 if (need_newline) |
1059 { | 1066 { |
1060 Bufbyte nl = '\n'; | 1067 Bufbyte nl = '\n'; |
1061 ssize_t retval2 = str->allow_quit ? | 1068 Lstream_data_count retval2 = str->allow_quit ? |
1062 write_allowing_quit (str->fd, &nl, 1) : | 1069 write_allowing_quit (str->fd, &nl, 1) : |
1063 write (str->fd, &nl, 1); | 1070 write (str->fd, &nl, 1); |
1064 | 1071 |
1065 if (retval2 > 0) | 1072 if (retval2 > 0) |
1066 { | 1073 { |
1192 str->obj = string; | 1199 str->obj = string; |
1193 XSETLSTREAM (obj, lstr); | 1200 XSETLSTREAM (obj, lstr); |
1194 return obj; | 1201 return obj; |
1195 } | 1202 } |
1196 | 1203 |
1197 static ssize_t | 1204 static Lstream_data_count |
1198 lisp_string_reader (Lstream *stream, unsigned char *data, size_t size) | 1205 lisp_string_reader (Lstream *stream, unsigned char *data, |
1206 Lstream_data_count size) | |
1199 { | 1207 { |
1200 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); | 1208 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); |
1201 /* Don't lose if the string shrank past us ... */ | 1209 /* Don't lose if the string shrank past us ... */ |
1202 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); | 1210 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); |
1203 Bufbyte *strstart = XSTRING_DATA (str->obj); | 1211 Bufbyte *strstart = XSTRING_DATA (str->obj); |
1208 /* Being in the middle of a character is `normal' unless | 1216 /* Being in the middle of a character is `normal' unless |
1209 LSTREAM_NO_PARTIAL_CHARS - mrb */ | 1217 LSTREAM_NO_PARTIAL_CHARS - mrb */ |
1210 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) | 1218 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) |
1211 VALIDATE_CHARPTR_BACKWARD (start); | 1219 VALIDATE_CHARPTR_BACKWARD (start); |
1212 offset = start - strstart; | 1220 offset = start - strstart; |
1213 size = min (size, (size_t) (str->end - offset)); | 1221 size = min (size, (Lstream_data_count) (str->end - offset)); |
1214 memcpy (data, start, size); | 1222 memcpy (data, start, size); |
1215 str->offset = offset + size; | 1223 str->offset = offset + size; |
1216 return size; | 1224 return size; |
1217 } | 1225 } |
1218 | 1226 |
1251 | 1259 |
1252 struct fixed_buffer_stream | 1260 struct fixed_buffer_stream |
1253 { | 1261 { |
1254 const unsigned char *inbuf; | 1262 const unsigned char *inbuf; |
1255 unsigned char *outbuf; | 1263 unsigned char *outbuf; |
1256 size_t size; | 1264 Lstream_data_count size; |
1257 size_t offset; | 1265 Lstream_data_count offset; |
1258 }; | 1266 }; |
1259 | 1267 |
1260 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer, | 1268 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer, |
1261 sizeof (struct fixed_buffer_stream)); | 1269 sizeof (struct fixed_buffer_stream)); |
1262 | 1270 |
1263 Lisp_Object | 1271 Lisp_Object |
1264 make_fixed_buffer_input_stream (const void *buf, size_t size) | 1272 make_fixed_buffer_input_stream (const void *buf, Lstream_data_count size) |
1265 { | 1273 { |
1266 Lisp_Object obj; | 1274 Lisp_Object obj; |
1267 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); | 1275 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); |
1268 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | 1276 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); |
1269 str->inbuf = (const unsigned char *) buf; | 1277 str->inbuf = (const unsigned char *) buf; |
1271 XSETLSTREAM (obj, lstr); | 1279 XSETLSTREAM (obj, lstr); |
1272 return obj; | 1280 return obj; |
1273 } | 1281 } |
1274 | 1282 |
1275 Lisp_Object | 1283 Lisp_Object |
1276 make_fixed_buffer_output_stream (void *buf, size_t size) | 1284 make_fixed_buffer_output_stream (void *buf, Lstream_data_count size) |
1277 { | 1285 { |
1278 Lisp_Object obj; | 1286 Lisp_Object obj; |
1279 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); | 1287 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); |
1280 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | 1288 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); |
1281 str->outbuf = (unsigned char *) buf; | 1289 str->outbuf = (unsigned char *) buf; |
1282 str->size = size; | 1290 str->size = size; |
1283 XSETLSTREAM (obj, lstr); | 1291 XSETLSTREAM (obj, lstr); |
1284 return obj; | 1292 return obj; |
1285 } | 1293 } |
1286 | 1294 |
1287 static ssize_t | 1295 static Lstream_data_count |
1288 fixed_buffer_reader (Lstream *stream, unsigned char *data, size_t size) | 1296 fixed_buffer_reader (Lstream *stream, unsigned char *data, |
1297 Lstream_data_count size) | |
1289 { | 1298 { |
1290 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | 1299 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); |
1291 size = min (size, str->size - str->offset); | 1300 size = min (size, str->size - str->offset); |
1292 memcpy (data, str->inbuf + str->offset, size); | 1301 memcpy (data, str->inbuf + str->offset, size); |
1293 str->offset += size; | 1302 str->offset += size; |
1294 return size; | 1303 return size; |
1295 } | 1304 } |
1296 | 1305 |
1297 static ssize_t | 1306 static Lstream_data_count |
1298 fixed_buffer_writer (Lstream *stream, const unsigned char *data, size_t size) | 1307 fixed_buffer_writer (Lstream *stream, const unsigned char *data, |
1308 Lstream_data_count size) | |
1299 { | 1309 { |
1300 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | 1310 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); |
1301 if (str->offset == str->size) | 1311 if (str->offset == str->size) |
1302 { | 1312 { |
1303 /* If we're at the end, just throw away the data and pretend | 1313 /* If we're at the end, just throw away the data and pretend |
1338 LSTREAM_TYPE_DATA (stream, resizing_buffer) | 1348 LSTREAM_TYPE_DATA (stream, resizing_buffer) |
1339 | 1349 |
1340 struct resizing_buffer_stream | 1350 struct resizing_buffer_stream |
1341 { | 1351 { |
1342 unsigned char *buf; | 1352 unsigned char *buf; |
1343 size_t allocked; | 1353 Lstream_data_count allocked; |
1344 int max_stored; | 1354 int max_stored; |
1345 int stored; | 1355 int stored; |
1346 }; | 1356 }; |
1347 | 1357 |
1348 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer, | 1358 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer, |
1354 Lisp_Object obj; | 1364 Lisp_Object obj; |
1355 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); | 1365 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); |
1356 return obj; | 1366 return obj; |
1357 } | 1367 } |
1358 | 1368 |
1359 static ssize_t | 1369 static Lstream_data_count |
1360 resizing_buffer_writer (Lstream *stream, const unsigned char *data, size_t size) | 1370 resizing_buffer_writer (Lstream *stream, const unsigned char *data, |
1371 Lstream_data_count size) | |
1361 { | 1372 { |
1362 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); | 1373 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); |
1363 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); | 1374 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); |
1364 memcpy (str->buf + str->stored, data, size); | 1375 memcpy (str->buf + str->stored, data, size); |
1365 str->stored += size; | 1376 str->stored += size; |
1416 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); | 1427 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); |
1417 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; | 1428 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; |
1418 return obj; | 1429 return obj; |
1419 } | 1430 } |
1420 | 1431 |
1421 static ssize_t | 1432 static Lstream_data_count |
1422 dynarr_writer (Lstream *stream, const unsigned char *data, size_t size) | 1433 dynarr_writer (Lstream *stream, const unsigned char *data, |
1434 Lstream_data_count size) | |
1423 { | 1435 { |
1424 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); | 1436 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); |
1425 Dynarr_add_many (str->dyn, data, size); | 1437 Dynarr_add_many (str->dyn, data, size); |
1426 return size; | 1438 return size; |
1427 } | 1439 } |
1540 | 1552 |
1541 Lstream_set_character_mode (XLSTREAM (lstr)); | 1553 Lstream_set_character_mode (XLSTREAM (lstr)); |
1542 return lstr; | 1554 return lstr; |
1543 } | 1555 } |
1544 | 1556 |
1545 static ssize_t | 1557 static Lstream_data_count |
1546 lisp_buffer_reader (Lstream *stream, unsigned char *data, size_t size) | 1558 lisp_buffer_reader (Lstream *stream, unsigned char *data, |
1559 Lstream_data_count size) | |
1547 { | 1560 { |
1548 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | 1561 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); |
1549 unsigned char *orig_data = data; | 1562 unsigned char *orig_data = data; |
1550 Bytind start; | 1563 Bytind start; |
1551 Bytind end; | 1564 Bytind end; |
1565 BI_BUF_ZV (buf)); | 1578 BI_BUF_ZV (buf)); |
1566 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end, | 1579 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end, |
1567 BI_BUF_ZV (buf)); | 1580 BI_BUF_ZV (buf)); |
1568 } | 1581 } |
1569 | 1582 |
1570 size = min (size, (size_t) (end - start)); | 1583 size = min (size, (Lstream_data_count) (end - start)); |
1571 end = start + size; | 1584 end = start + size; |
1572 /* We cannot return a partial character. */ | 1585 /* We cannot return a partial character. */ |
1573 VALIDATE_BYTIND_BACKWARD (buf, end); | 1586 VALIDATE_BYTIND_BACKWARD (buf, end); |
1574 | 1587 |
1575 while (start < end) | 1588 while (start < end) |
1598 | 1611 |
1599 set_bi_marker_position (str->start, end); | 1612 set_bi_marker_position (str->start, end); |
1600 return data - orig_data; | 1613 return data - orig_data; |
1601 } | 1614 } |
1602 | 1615 |
1603 static ssize_t | 1616 static Lstream_data_count |
1604 lisp_buffer_writer (Lstream *stream, const unsigned char *data, size_t size) | 1617 lisp_buffer_writer (Lstream *stream, const unsigned char *data, |
1618 Lstream_data_count size) | |
1605 { | 1619 { |
1606 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | 1620 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); |
1607 Bufpos pos; | 1621 Bufpos pos; |
1608 struct buffer *buf = XBUFFER (str->buffer); | 1622 struct buffer *buf = XBUFFER (str->buffer); |
1609 | 1623 |