Mercurial > hg > xemacs-beta
comparison src/lstream.c @ 647:b39c14581166
[xemacs-hg @ 2001-08-13 04:45:47 by ben]
removal of unsigned, size_t, etc.
author | ben |
---|---|
date | Mon, 13 Aug 2001 04:46:48 +0000 |
parents | 183866b06e0b |
children | fdefd0186b75 |
comparison
equal
deleted
inserted
replaced
646:00c54252fe4f | 647:b39c14581166 |
---|---|
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 Lstream_data_count Lstream_read (Lstream *stream, void *data, | 98 Lstream_Data_Count Lstream_read (Lstream *stream, void *data, |
99 Lstream_data_count size) | 99 Lstream_Data_Count size) |
100 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 |
101 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 |
102 bytes were read. | 102 bytes were read. |
103 | 103 |
104 Lstream_data_count Lstream_write (Lstream *stream, void *data, | 104 Lstream_Data_Count Lstream_write (Lstream *stream, void *data, |
105 Lstream_data_count size) | 105 Lstream_Data_Count size) |
106 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 |
107 bytes written. -1 means an error occurred and no bytes were | 107 bytes written. -1 means an error occurred and no bytes were |
108 written. | 108 written. |
109 | 109 |
110 void Lstream_unread (Lstream *stream, void *data, Lstream_data_count size) | 110 void Lstream_unread (Lstream *stream, void *data, Lstream_Data_Count size) |
111 Push back SIZE bytes of DATA onto the input queue. The | 111 Push back SIZE bytes of DATA onto the input queue. The |
112 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 |
113 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 |
114 there is other pending unread data. | 114 there is other pending unread data. |
115 | 115 |
179 /* Just close. */ | 179 /* Just close. */ |
180 Lstream_close (lstr); | 180 Lstream_close (lstr); |
181 } | 181 } |
182 } | 182 } |
183 | 183 |
184 inline static size_t | 184 inline static Memory_Count |
185 aligned_sizeof_lstream (size_t lstream_type_specific_size) | 185 aligned_sizeof_lstream (Memory_Count lstream_type_specific_size) |
186 { | 186 { |
187 return ALIGN_SIZE (offsetof (Lstream, data) + lstream_type_specific_size, | 187 return ALIGN_SIZE (offsetof (Lstream, data) + lstream_type_specific_size, |
188 ALIGNOF (max_align_t)); | 188 ALIGNOF (max_align_t)); |
189 } | 189 } |
190 | 190 |
191 static size_t | 191 static Memory_Count |
192 sizeof_lstream (const void *header) | 192 sizeof_lstream (const void *header) |
193 { | 193 { |
194 return aligned_sizeof_lstream (((const Lstream *) header)->imp->size); | 194 return aligned_sizeof_lstream (((const Lstream *) header)->imp->size); |
195 } | 195 } |
196 | 196 |
301 /* Attempt to flush out all of the buffered data for writing. */ | 301 /* Attempt to flush out all of the buffered data for writing. */ |
302 | 302 |
303 int | 303 int |
304 Lstream_flush_out (Lstream *lstr) | 304 Lstream_flush_out (Lstream *lstr) |
305 { | 305 { |
306 Lstream_data_count num_written; | 306 Lstream_Data_Count num_written; |
307 | 307 |
308 while (lstr->out_buffer_ind > 0) | 308 while (lstr->out_buffer_ind > 0) |
309 { | 309 { |
310 Lstream_data_count size = lstr->out_buffer_ind; | 310 Lstream_Data_Count size = lstr->out_buffer_ind; |
311 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 311 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
312 Lstream_internal_error ("lstream not open", lstr); | 312 Lstream_internal_error ("lstream not open", lstr); |
313 if (! (lstr->flags & LSTREAM_FL_WRITE)) | 313 if (! (lstr->flags & LSTREAM_FL_WRITE)) |
314 Lstream_internal_error ("lstream not open for writing", lstr); | 314 Lstream_internal_error ("lstream not open for writing", lstr); |
315 if (!lstr->imp->writer) | 315 if (!lstr->imp->writer) |
388 the buffering size. (This is used to deal with the possibility | 388 the buffering size. (This is used to deal with the possibility |
389 that the stream writer might refuse to write any bytes now, e.g. | 389 that the stream writer might refuse to write any bytes now, e.g. |
390 if it's getting EWOULDBLOCK errors. We have to keep stocking them | 390 if it's getting EWOULDBLOCK errors. We have to keep stocking them |
391 up until they can be written, so as to avoid losing data. */ | 391 up until they can be written, so as to avoid losing data. */ |
392 | 392 |
393 static Lstream_data_count | 393 static Lstream_Data_Count |
394 Lstream_adding (Lstream *lstr, Lstream_data_count num, int force) | 394 Lstream_adding (Lstream *lstr, Lstream_Data_Count num, int force) |
395 { | 395 { |
396 Lstream_data_count size = num + lstr->out_buffer_ind; | 396 Lstream_Data_Count size = num + lstr->out_buffer_ind; |
397 | 397 |
398 if (size <= lstr->out_buffer_size) | 398 if (size <= lstr->out_buffer_size) |
399 return num; | 399 return num; |
400 | 400 |
401 /* Maybe chop it down so that we don't buffer more characters | 401 /* Maybe chop it down so that we don't buffer more characters |
413 return size - lstr->out_buffer_ind; | 413 return size - lstr->out_buffer_ind; |
414 } | 414 } |
415 | 415 |
416 /* Like Lstream_write(), but does not handle line-buffering correctly. */ | 416 /* Like Lstream_write(), but does not handle line-buffering correctly. */ |
417 | 417 |
418 static Lstream_data_count | 418 static Lstream_Data_Count |
419 Lstream_write_1 (Lstream *lstr, const void *data, Lstream_data_count size) | 419 Lstream_write_1 (Lstream *lstr, const void *data, Lstream_Data_Count size) |
420 { | 420 { |
421 const unsigned char *p = (const unsigned char *) data; | 421 const unsigned char *p = (const unsigned char *) data; |
422 Lstream_data_count off = 0; | 422 Lstream_Data_Count off = 0; |
423 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 423 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
424 Lstream_internal_error ("lstream not open", lstr); | 424 Lstream_internal_error ("lstream not open", lstr); |
425 if (! (lstr->flags & LSTREAM_FL_WRITE)) | 425 if (! (lstr->flags & LSTREAM_FL_WRITE)) |
426 Lstream_internal_error ("lstream not open for writing", lstr); | 426 Lstream_internal_error ("lstream not open for writing", lstr); |
427 { | 427 { |
428 int couldnt_write_last_time = 0; | 428 int couldnt_write_last_time = 0; |
429 | 429 |
430 while (1) | 430 while (1) |
431 { | 431 { |
432 /* Figure out how much we can add to the buffer */ | 432 /* Figure out how much we can add to the buffer */ |
433 Lstream_data_count chunk = Lstream_adding (lstr, size, 0); | 433 Lstream_Data_Count chunk = Lstream_adding (lstr, size, 0); |
434 if (chunk == 0) | 434 if (chunk == 0) |
435 { | 435 { |
436 if (couldnt_write_last_time) | 436 if (couldnt_write_last_time) |
437 /* Ung, we ran out of space and tried to flush | 437 /* Ung, we ran out of space and tried to flush |
438 the buffer, but it didn't work because the stream | 438 the buffer, but it didn't work because the stream |
473 /* If the stream is not line-buffered, then we can just call | 473 /* If the stream is not line-buffered, then we can just call |
474 Lstream_write_1(), which writes in chunks. Otherwise, we | 474 Lstream_write_1(), which writes in chunks. Otherwise, we |
475 repeatedly call Lstream_putc(), which knows how to handle | 475 repeatedly call Lstream_putc(), which knows how to handle |
476 line buffering. Returns number of bytes written. */ | 476 line buffering. Returns number of bytes written. */ |
477 | 477 |
478 Lstream_data_count | 478 Lstream_Data_Count |
479 Lstream_write (Lstream *lstr, const void *data, Lstream_data_count size) | 479 Lstream_write (Lstream *lstr, const void *data, Lstream_Data_Count size) |
480 { | 480 { |
481 Lstream_data_count i; | 481 Lstream_Data_Count i; |
482 const unsigned char *p = (const unsigned char *) data; | 482 const unsigned char *p = (const unsigned char *) data; |
483 | 483 |
484 if (size == 0) | 484 if (size == 0) |
485 return size; | 485 return size; |
486 if (lstr->buffering != LSTREAM_LINE_BUFFERED) | 486 if (lstr->buffering != LSTREAM_LINE_BUFFERED) |
497 Lstream_was_blocked_p (Lstream *lstr) | 497 Lstream_was_blocked_p (Lstream *lstr) |
498 { | 498 { |
499 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; | 499 return lstr->imp->was_blocked_p ? lstr->imp->was_blocked_p (lstr) : 0; |
500 } | 500 } |
501 | 501 |
502 static Lstream_data_count | 502 static Lstream_Data_Count |
503 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, | 503 Lstream_raw_read (Lstream *lstr, unsigned char *buffer, |
504 Lstream_data_count size) | 504 Lstream_Data_Count size) |
505 { | 505 { |
506 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) | 506 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) |
507 Lstream_internal_error ("lstream not open", lstr); | 507 Lstream_internal_error ("lstream not open", lstr); |
508 if (! (lstr->flags & LSTREAM_FL_READ)) | 508 if (! (lstr->flags & LSTREAM_FL_READ)) |
509 Lstream_internal_error ("lstream not open for reading", lstr); | 509 Lstream_internal_error ("lstream not open for reading", lstr); |
513 return (lstr->imp->reader) (lstr, buffer, size); | 513 return (lstr->imp->reader) (lstr, buffer, size); |
514 } | 514 } |
515 | 515 |
516 /* Assuming the buffer is empty, fill it up again. */ | 516 /* Assuming the buffer is empty, fill it up again. */ |
517 | 517 |
518 static Lstream_data_count | 518 static Lstream_Data_Count |
519 Lstream_read_more (Lstream *lstr) | 519 Lstream_read_more (Lstream *lstr) |
520 { | 520 { |
521 #if 0 | 521 #if 0 |
522 Lstream_data_count size_needed | 522 Lstream_Data_Count size_needed |
523 = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); | 523 = max (1, min (MAX_READ_SIZE, lstr->buffering_size)); |
524 #else | 524 #else |
525 /* If someone requested a larger buffer size, so be it! */ | 525 /* If someone requested a larger buffer size, so be it! */ |
526 Lstream_data_count size_needed = | 526 Lstream_Data_Count size_needed = |
527 max (1, lstr->buffering_size); | 527 max (1, lstr->buffering_size); |
528 #endif | 528 #endif |
529 Lstream_data_count size_gotten; | 529 Lstream_Data_Count size_gotten; |
530 | 530 |
531 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, | 531 DO_REALLOC (lstr->in_buffer, lstr->in_buffer_size, |
532 size_needed, unsigned char); | 532 size_needed, unsigned char); |
533 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); | 533 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); |
534 lstr->in_buffer_current = max (0, size_gotten); | 534 lstr->in_buffer_current = max (0, size_gotten); |
535 lstr->in_buffer_ind = 0; | 535 lstr->in_buffer_ind = 0; |
536 return size_gotten < 0 ? -1 : size_gotten; | 536 return size_gotten < 0 ? -1 : size_gotten; |
537 } | 537 } |
538 | 538 |
539 Lstream_data_count | 539 Lstream_Data_Count |
540 Lstream_read (Lstream *lstr, void *data, Lstream_data_count size) | 540 Lstream_read (Lstream *lstr, void *data, Lstream_Data_Count size) |
541 { | 541 { |
542 unsigned char *p = (unsigned char *) data; | 542 unsigned char *p = (unsigned char *) data; |
543 Lstream_data_count off = 0; | 543 Lstream_Data_Count off = 0; |
544 Lstream_data_count chunk; | 544 Lstream_Data_Count chunk; |
545 int error_occurred = 0; | 545 int error_occurred = 0; |
546 | 546 |
547 if (size == 0) | 547 if (size == 0) |
548 return 0; | 548 return 0; |
549 | 549 |
572 } | 572 } |
573 | 573 |
574 /* If we need some more, try to get some more from the stream's end */ | 574 /* If we need some more, try to get some more from the stream's end */ |
575 if (size > 0) | 575 if (size > 0) |
576 { | 576 { |
577 Lstream_data_count retval = Lstream_read_more (lstr); | 577 Lstream_Data_Count retval = Lstream_read_more (lstr); |
578 if (retval < 0) | 578 if (retval < 0) |
579 error_occurred = 1; | 579 error_occurred = 1; |
580 if (retval <= 0) | 580 if (retval <= 0) |
581 break; | 581 break; |
582 } | 582 } |
596 character, and bump forward to see if the character is | 596 character, and bump forward to see if the character is |
597 complete. */ | 597 complete. */ |
598 VALIDATE_CHARPTR_BACKWARD (dataend); | 598 VALIDATE_CHARPTR_BACKWARD (dataend); |
599 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off) | 599 if (dataend + REP_BYTES_BY_FIRST_BYTE (*dataend) != p + off) |
600 { | 600 { |
601 Lstream_data_count newoff = dataend - p; | 601 Lstream_Data_Count newoff = dataend - p; |
602 /* If not, chop the size down to ignore the last char | 602 /* If not, chop the size down to ignore the last char |
603 and stash it away for next time. */ | 603 and stash it away for next time. */ |
604 Lstream_unread (lstr, dataend, off - newoff); | 604 Lstream_unread (lstr, dataend, off - newoff); |
605 off = newoff; | 605 off = newoff; |
606 } | 606 } |
609 | 609 |
610 return off == 0 && error_occurred ? -1 : off; | 610 return off == 0 && error_occurred ? -1 : off; |
611 } | 611 } |
612 | 612 |
613 void | 613 void |
614 Lstream_unread (Lstream *lstr, const void *data, Lstream_data_count size) | 614 Lstream_unread (Lstream *lstr, const void *data, Lstream_Data_Count size) |
615 { | 615 { |
616 const unsigned char *p = (const unsigned char *) data; | 616 const unsigned char *p = (const unsigned char *) data; |
617 | 617 |
618 /* Make sure buffer is big enough */ | 618 /* Make sure buffer is big enough */ |
619 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, | 619 DO_REALLOC (lstr->unget_buffer, lstr->unget_buffer_size, |
728 | 728 |
729 int | 729 int |
730 Lstream_fputc (Lstream *lstr, int c) | 730 Lstream_fputc (Lstream *lstr, int c) |
731 { | 731 { |
732 unsigned char ch = (unsigned char) c; | 732 unsigned char ch = (unsigned char) c; |
733 Lstream_data_count retval = Lstream_write_1 (lstr, &ch, 1); | 733 Lstream_Data_Count retval = Lstream_write_1 (lstr, &ch, 1); |
734 if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') | 734 if (retval >= 0 && lstr->buffering == LSTREAM_LINE_BUFFERED && ch == '\n') |
735 return Lstream_flush_out (lstr); | 735 return Lstream_flush_out (lstr); |
736 return retval < 0 ? -1 : 0; | 736 return retval < 0 ? -1 : 0; |
737 } | 737 } |
738 | 738 |
809 | 809 |
810 This is probably reasonable, so I don't think we should change this | 810 This is probably reasonable, so I don't think we should change this |
811 code (it could even be argued that the error might have fixed | 811 code (it could even be argued that the error might have fixed |
812 itself, so we should do the fread() again. */ | 812 itself, so we should do the fread() again. */ |
813 | 813 |
814 static Lstream_data_count | 814 static Lstream_Data_Count |
815 stdio_reader (Lstream *stream, unsigned char *data, Lstream_data_count size) | 815 stdio_reader (Lstream *stream, unsigned char *data, Lstream_Data_Count size) |
816 { | 816 { |
817 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | 817 struct stdio_stream *str = STDIO_STREAM_DATA (stream); |
818 Lstream_data_count val = fread (data, 1, size, str->file); | 818 Lstream_Data_Count val = fread (data, 1, size, str->file); |
819 if (!val && ferror (str->file)) | 819 if (!val && ferror (str->file)) |
820 return -1; | 820 return -1; |
821 return val; | 821 return val; |
822 } | 822 } |
823 | 823 |
824 static Lstream_data_count | 824 static Lstream_Data_Count |
825 stdio_writer (Lstream *stream, const unsigned char *data, | 825 stdio_writer (Lstream *stream, const unsigned char *data, |
826 Lstream_data_count size) | 826 Lstream_Data_Count size) |
827 { | 827 { |
828 struct stdio_stream *str = STDIO_STREAM_DATA (stream); | 828 struct stdio_stream *str = STDIO_STREAM_DATA (stream); |
829 Lstream_data_count val = fwrite (data, 1, size, str->file); | 829 Lstream_Data_Count val = fwrite (data, 1, size, str->file); |
830 if (!val && ferror (str->file)) | 830 if (!val && ferror (str->file)) |
831 return -1; | 831 return -1; |
832 return val; | 832 return val; |
833 } | 833 } |
834 | 834 |
935 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) | 935 make_filedesc_output_stream (int filedesc, int offset, int count, int flags) |
936 { | 936 { |
937 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); | 937 return make_filedesc_stream_1 (filedesc, offset, count, flags, "w"); |
938 } | 938 } |
939 | 939 |
940 static Lstream_data_count | 940 static Lstream_Data_Count |
941 filedesc_reader (Lstream *stream, unsigned char *data, Lstream_data_count size) | 941 filedesc_reader (Lstream *stream, unsigned char *data, Lstream_Data_Count size) |
942 { | 942 { |
943 Lstream_data_count nread; | 943 Lstream_Data_Count nread; |
944 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 944 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
945 if (str->end_pos >= 0) | 945 if (str->end_pos >= 0) |
946 size = min (size, (Lstream_data_count) (str->end_pos - str->current_pos)); | 946 size = min (size, (Lstream_Data_Count) (str->end_pos - str->current_pos)); |
947 nread = str->allow_quit ? | 947 nread = str->allow_quit ? |
948 read_allowing_quit (str->fd, data, size) : | 948 read_allowing_quit (str->fd, data, size) : |
949 read (str->fd, data, size); | 949 read (str->fd, data, size); |
950 if (nread > 0) | 950 if (nread > 0) |
951 str->current_pos += nread; | 951 str->current_pos += nread; |
964 return 1; | 964 return 1; |
965 #endif | 965 #endif |
966 return 0; | 966 return 0; |
967 } | 967 } |
968 | 968 |
969 static Lstream_data_count | 969 static Lstream_Data_Count |
970 filedesc_writer (Lstream *stream, const unsigned char *data, | 970 filedesc_writer (Lstream *stream, const unsigned char *data, |
971 Lstream_data_count size) | 971 Lstream_Data_Count size) |
972 { | 972 { |
973 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); | 973 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); |
974 Lstream_data_count retval; | 974 Lstream_Data_Count retval; |
975 int need_newline = 0; | 975 int need_newline = 0; |
976 | 976 |
977 /* This function would be simple if it were not for the blasted | 977 /* 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 | 978 PTY max-bytes stuff. Why the hell can't they just have written |
979 the PTY drivers right so this problem doesn't exist? | 979 the PTY drivers right so this problem doesn't exist? |
1023 and flush with an EOF if necessary. Be careful to | 1023 and flush with an EOF if necessary. Be careful to |
1024 keep track of write errors as we go along and look | 1024 keep track of write errors as we go along and look |
1025 out for EWOULDBLOCK. */ | 1025 out for EWOULDBLOCK. */ |
1026 if (str->chars_sans_newline >= str->pty_max_bytes) | 1026 if (str->chars_sans_newline >= str->pty_max_bytes) |
1027 { | 1027 { |
1028 Lstream_data_count retval2 = str->allow_quit ? | 1028 Lstream_Data_Count retval2 = str->allow_quit ? |
1029 write_allowing_quit (str->fd, &str->eof_char, 1) : | 1029 write_allowing_quit (str->fd, &str->eof_char, 1) : |
1030 write (str->fd, &str->eof_char, 1); | 1030 write (str->fd, &str->eof_char, 1); |
1031 | 1031 |
1032 if (retval2 > 0) | 1032 if (retval2 > 0) |
1033 str->chars_sans_newline = 0; | 1033 str->chars_sans_newline = 0; |
1056 first byte is a newline, we'd get stuck never writing anything | 1056 first byte is a newline, we'd get stuck never writing anything |
1057 in pty-flushing mode. */ | 1057 in pty-flushing mode. */ |
1058 if (need_newline) | 1058 if (need_newline) |
1059 { | 1059 { |
1060 Bufbyte nl = '\n'; | 1060 Bufbyte nl = '\n'; |
1061 Lstream_data_count retval2 = str->allow_quit ? | 1061 Lstream_Data_Count retval2 = str->allow_quit ? |
1062 write_allowing_quit (str->fd, &nl, 1) : | 1062 write_allowing_quit (str->fd, &nl, 1) : |
1063 write (str->fd, &nl, 1); | 1063 write (str->fd, &nl, 1); |
1064 | 1064 |
1065 if (retval2 > 0) | 1065 if (retval2 > 0) |
1066 { | 1066 { |
1192 str->obj = string; | 1192 str->obj = string; |
1193 XSETLSTREAM (obj, lstr); | 1193 XSETLSTREAM (obj, lstr); |
1194 return obj; | 1194 return obj; |
1195 } | 1195 } |
1196 | 1196 |
1197 static Lstream_data_count | 1197 static Lstream_Data_Count |
1198 lisp_string_reader (Lstream *stream, unsigned char *data, | 1198 lisp_string_reader (Lstream *stream, unsigned char *data, |
1199 Lstream_data_count size) | 1199 Lstream_Data_Count size) |
1200 { | 1200 { |
1201 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); | 1201 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); |
1202 /* Don't lose if the string shrank past us ... */ | 1202 /* Don't lose if the string shrank past us ... */ |
1203 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); | 1203 Bytecount offset = min (str->offset, XSTRING_LENGTH (str->obj)); |
1204 Bufbyte *strstart = XSTRING_DATA (str->obj); | 1204 Bufbyte *strstart = XSTRING_DATA (str->obj); |
1209 /* Being in the middle of a character is `normal' unless | 1209 /* Being in the middle of a character is `normal' unless |
1210 LSTREAM_NO_PARTIAL_CHARS - mrb */ | 1210 LSTREAM_NO_PARTIAL_CHARS - mrb */ |
1211 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) | 1211 if (stream->flags & LSTREAM_FL_NO_PARTIAL_CHARS) |
1212 VALIDATE_CHARPTR_BACKWARD (start); | 1212 VALIDATE_CHARPTR_BACKWARD (start); |
1213 offset = start - strstart; | 1213 offset = start - strstart; |
1214 size = min (size, (Lstream_data_count) (str->end - offset)); | 1214 size = min (size, (Lstream_Data_Count) (str->end - offset)); |
1215 memcpy (data, start, size); | 1215 memcpy (data, start, size); |
1216 str->offset = offset + size; | 1216 str->offset = offset + size; |
1217 return size; | 1217 return size; |
1218 } | 1218 } |
1219 | 1219 |
1252 | 1252 |
1253 struct fixed_buffer_stream | 1253 struct fixed_buffer_stream |
1254 { | 1254 { |
1255 const unsigned char *inbuf; | 1255 const unsigned char *inbuf; |
1256 unsigned char *outbuf; | 1256 unsigned char *outbuf; |
1257 Lstream_data_count size; | 1257 Lstream_Data_Count size; |
1258 Lstream_data_count offset; | 1258 Lstream_Data_Count offset; |
1259 }; | 1259 }; |
1260 | 1260 |
1261 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer, | 1261 DEFINE_LSTREAM_IMPLEMENTATION ("fixed-buffer", lstream_fixed_buffer, |
1262 sizeof (struct fixed_buffer_stream)); | 1262 sizeof (struct fixed_buffer_stream)); |
1263 | 1263 |
1264 Lisp_Object | 1264 Lisp_Object |
1265 make_fixed_buffer_input_stream (const void *buf, Lstream_data_count size) | 1265 make_fixed_buffer_input_stream (const void *buf, Lstream_Data_Count size) |
1266 { | 1266 { |
1267 Lisp_Object obj; | 1267 Lisp_Object obj; |
1268 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); | 1268 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "r"); |
1269 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | 1269 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); |
1270 str->inbuf = (const unsigned char *) buf; | 1270 str->inbuf = (const unsigned char *) buf; |
1272 XSETLSTREAM (obj, lstr); | 1272 XSETLSTREAM (obj, lstr); |
1273 return obj; | 1273 return obj; |
1274 } | 1274 } |
1275 | 1275 |
1276 Lisp_Object | 1276 Lisp_Object |
1277 make_fixed_buffer_output_stream (void *buf, Lstream_data_count size) | 1277 make_fixed_buffer_output_stream (void *buf, Lstream_Data_Count size) |
1278 { | 1278 { |
1279 Lisp_Object obj; | 1279 Lisp_Object obj; |
1280 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); | 1280 Lstream *lstr = Lstream_new (lstream_fixed_buffer, "w"); |
1281 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); | 1281 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (lstr); |
1282 str->outbuf = (unsigned char *) buf; | 1282 str->outbuf = (unsigned char *) buf; |
1283 str->size = size; | 1283 str->size = size; |
1284 XSETLSTREAM (obj, lstr); | 1284 XSETLSTREAM (obj, lstr); |
1285 return obj; | 1285 return obj; |
1286 } | 1286 } |
1287 | 1287 |
1288 static Lstream_data_count | 1288 static Lstream_Data_Count |
1289 fixed_buffer_reader (Lstream *stream, unsigned char *data, | 1289 fixed_buffer_reader (Lstream *stream, unsigned char *data, |
1290 Lstream_data_count size) | 1290 Lstream_Data_Count size) |
1291 { | 1291 { |
1292 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | 1292 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); |
1293 size = min (size, str->size - str->offset); | 1293 size = min (size, str->size - str->offset); |
1294 memcpy (data, str->inbuf + str->offset, size); | 1294 memcpy (data, str->inbuf + str->offset, size); |
1295 str->offset += size; | 1295 str->offset += size; |
1296 return size; | 1296 return size; |
1297 } | 1297 } |
1298 | 1298 |
1299 static Lstream_data_count | 1299 static Lstream_Data_Count |
1300 fixed_buffer_writer (Lstream *stream, const unsigned char *data, | 1300 fixed_buffer_writer (Lstream *stream, const unsigned char *data, |
1301 Lstream_data_count size) | 1301 Lstream_Data_Count size) |
1302 { | 1302 { |
1303 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); | 1303 struct fixed_buffer_stream *str = FIXED_BUFFER_STREAM_DATA (stream); |
1304 if (str->offset == str->size) | 1304 if (str->offset == str->size) |
1305 { | 1305 { |
1306 /* If we're at the end, just throw away the data and pretend | 1306 /* If we're at the end, just throw away the data and pretend |
1341 LSTREAM_TYPE_DATA (stream, resizing_buffer) | 1341 LSTREAM_TYPE_DATA (stream, resizing_buffer) |
1342 | 1342 |
1343 struct resizing_buffer_stream | 1343 struct resizing_buffer_stream |
1344 { | 1344 { |
1345 unsigned char *buf; | 1345 unsigned char *buf; |
1346 Lstream_data_count allocked; | 1346 Lstream_Data_Count allocked; |
1347 int max_stored; | 1347 int max_stored; |
1348 int stored; | 1348 int stored; |
1349 }; | 1349 }; |
1350 | 1350 |
1351 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer, | 1351 DEFINE_LSTREAM_IMPLEMENTATION ("resizing-buffer", lstream_resizing_buffer, |
1357 Lisp_Object obj; | 1357 Lisp_Object obj; |
1358 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); | 1358 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); |
1359 return obj; | 1359 return obj; |
1360 } | 1360 } |
1361 | 1361 |
1362 static Lstream_data_count | 1362 static Lstream_Data_Count |
1363 resizing_buffer_writer (Lstream *stream, const unsigned char *data, | 1363 resizing_buffer_writer (Lstream *stream, const unsigned char *data, |
1364 Lstream_data_count size) | 1364 Lstream_Data_Count size) |
1365 { | 1365 { |
1366 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); | 1366 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); |
1367 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); | 1367 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); |
1368 memcpy (str->buf + str->stored, data, size); | 1368 memcpy (str->buf + str->stored, data, size); |
1369 str->stored += size; | 1369 str->stored += size; |
1420 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); | 1420 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); |
1421 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; | 1421 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; |
1422 return obj; | 1422 return obj; |
1423 } | 1423 } |
1424 | 1424 |
1425 static Lstream_data_count | 1425 static Lstream_Data_Count |
1426 dynarr_writer (Lstream *stream, const unsigned char *data, | 1426 dynarr_writer (Lstream *stream, const unsigned char *data, |
1427 Lstream_data_count size) | 1427 Lstream_Data_Count size) |
1428 { | 1428 { |
1429 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); | 1429 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); |
1430 Dynarr_add_many (str->dyn, data, size); | 1430 Dynarr_add_many (str->dyn, data, size); |
1431 return size; | 1431 return size; |
1432 } | 1432 } |
1545 | 1545 |
1546 Lstream_set_character_mode (XLSTREAM (lstr)); | 1546 Lstream_set_character_mode (XLSTREAM (lstr)); |
1547 return lstr; | 1547 return lstr; |
1548 } | 1548 } |
1549 | 1549 |
1550 static Lstream_data_count | 1550 static Lstream_Data_Count |
1551 lisp_buffer_reader (Lstream *stream, unsigned char *data, | 1551 lisp_buffer_reader (Lstream *stream, unsigned char *data, |
1552 Lstream_data_count size) | 1552 Lstream_Data_Count size) |
1553 { | 1553 { |
1554 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | 1554 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); |
1555 unsigned char *orig_data = data; | 1555 unsigned char *orig_data = data; |
1556 Bytind start; | 1556 Bytind start; |
1557 Bytind end; | 1557 Bytind end; |
1571 BI_BUF_ZV (buf)); | 1571 BI_BUF_ZV (buf)); |
1572 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end, | 1572 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end, |
1573 BI_BUF_ZV (buf)); | 1573 BI_BUF_ZV (buf)); |
1574 } | 1574 } |
1575 | 1575 |
1576 size = min (size, (Lstream_data_count) (end - start)); | 1576 size = min (size, (Lstream_Data_Count) (end - start)); |
1577 end = start + size; | 1577 end = start + size; |
1578 /* We cannot return a partial character. */ | 1578 /* We cannot return a partial character. */ |
1579 VALIDATE_BYTIND_BACKWARD (buf, end); | 1579 VALIDATE_BYTIND_BACKWARD (buf, end); |
1580 | 1580 |
1581 while (start < end) | 1581 while (start < end) |
1604 | 1604 |
1605 set_bi_marker_position (str->start, end); | 1605 set_bi_marker_position (str->start, end); |
1606 return data - orig_data; | 1606 return data - orig_data; |
1607 } | 1607 } |
1608 | 1608 |
1609 static Lstream_data_count | 1609 static Lstream_Data_Count |
1610 lisp_buffer_writer (Lstream *stream, const unsigned char *data, | 1610 lisp_buffer_writer (Lstream *stream, const unsigned char *data, |
1611 Lstream_data_count size) | 1611 Lstream_Data_Count size) |
1612 { | 1612 { |
1613 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); | 1613 struct lisp_buffer_stream *str = LISP_BUFFER_STREAM_DATA (stream); |
1614 Bufpos pos; | 1614 Bufpos pos; |
1615 struct buffer *buf = XBUFFER (str->buffer); | 1615 struct buffer *buf = XBUFFER (str->buffer); |
1616 | 1616 |