comparison src/lstream.c @ 185:3d6bfa290dbd r20-3b19

Import from CVS: tag r20-3b19
author cvs
date Mon, 13 Aug 2007 09:55:28 +0200
parents 8eaf7971accc
children e45d5e7c476e
comparison
equal deleted inserted replaced
184:bcd2674570bf 185:3d6bfa290dbd
70 int Lstream_putc (Lstream *stream, int c) 70 int Lstream_putc (Lstream *stream, int c)
71 Write out one byte to the stream. This is a macro and so 71 Write out one byte to the stream. This is a macro and so
72 it is very efficient. The C argument is only evaluated once 72 it is very efficient. The C argument is only evaluated once
73 but the STREAM argument is evaluated more than once. Returns 73 but the STREAM argument is evaluated more than once. Returns
74 0 on success, -1 on error. 74 0 on success, -1 on error.
75 75
76 int Lstream_getc (Lstream *stream) 76 int Lstream_getc (Lstream *stream)
77 Read one byte from the stream. This is a macro and so it 77 Read one byte from the stream. This is a macro and so it
78 is very efficient. The STREAM argument is evaluated more 78 is very efficient. The STREAM argument is evaluated more
79 than once. Return value is -1 for EOF or error. 79 than once. Return value is -1 for EOF or error.
80 80
90 STREAM argument is evaluated more than once. 90 STREAM argument is evaluated more than once.
91 91
92 int Lstream_fputc (Lstream *stream, int c) 92 int Lstream_fputc (Lstream *stream, int c)
93 int Lstream_fgetc (Lstream *stream) 93 int Lstream_fgetc (Lstream *stream)
94 void Lstream_fungetc (Lstream *stream, int c) 94 void Lstream_fungetc (Lstream *stream, int c)
95 Function equivalents of the above macros. 95 Function equivalents of the above macros.
96 96
97 int Lstream_read (Lstream *stream, void *data, int size) 97 int Lstream_read (Lstream *stream, void *data, int size)
98 Read SIZE bytes of DATA from the stream. Return the number of 98 Read SIZE bytes of DATA from the stream. Return the number of
99 bytes read. 0 means EOF. -1 means an error occurred and no 99 bytes read. 0 means EOF. -1 means an error occurred and no
100 bytes were read. 100 bytes were read.
107 void Lstream_unread (Lstream *stream, void *data, int size) 107 void Lstream_unread (Lstream *stream, void *data, int size)
108 Push back SIZE bytes of DATA onto the input queue. The 108 Push back SIZE bytes of DATA onto the input queue. The
109 next call to Lstream_read() with the same size will read the 109 next call to Lstream_read() with the same size will read the
110 same bytes back. Note that this will be the case even if 110 same bytes back. Note that this will be the case even if
111 there is other pending unread data. 111 there is other pending unread data.
112
113 int Lstream_delete (Lstream *stream)
114 Frees all memory associated with the stream is freed. Calling
115 this is not strictly necessary, but it is much more efficient
116 than having the Lstream be garbage-collected.
112 117
113 int Lstream_close (Lstream *stream) 118 int Lstream_close (Lstream *stream)
114 Close the stream. All data will be flushed out. 119 Close the stream. All data will be flushed out.
115 120
116 void Lstream_reopen (Lstream *stream) 121 void Lstream_reopen (Lstream *stream)
427 /* Like Lstream_write(), but does not handle line-buffering correctly. */ 432 /* Like Lstream_write(), but does not handle line-buffering correctly. */
428 433
429 static int 434 static int
430 Lstream_write_1 (Lstream *lstr, CONST void *data, int size) 435 Lstream_write_1 (Lstream *lstr, CONST void *data, int size)
431 { 436 {
432 CONST unsigned char *p = data; 437 CONST unsigned char *p = (CONST unsigned char *) data;
433 int off = 0; 438 int off = 0;
434 if (! (lstr->flags & LSTREAM_FL_IS_OPEN)) 439 if (! (lstr->flags & LSTREAM_FL_IS_OPEN))
435 { 440 {
436 Lisp_Object obj = Qnil; 441 Lisp_Object obj = Qnil;
437 XSETLSTREAM (obj, lstr); 442 XSETLSTREAM (obj, lstr);
498 503
499 int 504 int
500 Lstream_write (Lstream *lstr, CONST void *data, int size) 505 Lstream_write (Lstream *lstr, CONST void *data, int size)
501 { 506 {
502 int i; 507 int i;
503 CONST unsigned char *p = data; 508 CONST unsigned char *p = (CONST unsigned char *) data;
504 509
505 assert (size >= 0); 510 assert (size >= 0);
506 if (size == 0) 511 if (size == 0)
507 return size; 512 return size;
508 if (lstr->buffering != LSTREAM_LINE_BUFFERED) 513 if (lstr->buffering != LSTREAM_LINE_BUFFERED)
537 Lisp_Object obj = Qnil; 542 Lisp_Object obj = Qnil;
538 XSETLSTREAM (obj, lstr); 543 XSETLSTREAM (obj, lstr);
539 signal_simple_internal_error 544 signal_simple_internal_error
540 ("Internal error: lstream has no reader", obj); 545 ("Internal error: lstream has no reader", obj);
541 } 546 }
542 547
543 return (lstr->imp->reader) (lstr, buffer, size); 548 return (lstr->imp->reader) (lstr, buffer, size);
544 } 549 }
545 550
546 /* Assuming the buffer is empty, fill it up again. */ 551 /* Assuming the buffer is empty, fill it up again. */
547 552
560 size_needed, unsigned char); 565 size_needed, unsigned char);
561 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed); 566 size_gotten = Lstream_raw_read (lstr, lstr->in_buffer, size_needed);
562 lstr->in_buffer_current = max (0, size_gotten); 567 lstr->in_buffer_current = max (0, size_gotten);
563 lstr->in_buffer_ind = 0; 568 lstr->in_buffer_ind = 0;
564 return size_gotten < 0 ? -1 : size_gotten; 569 return size_gotten < 0 ? -1 : size_gotten;
565 } 570 }
566 571
567 int 572 int
568 Lstream_read (Lstream *lstr, void *data, int size) 573 Lstream_read (Lstream *lstr, void *data, int size)
569 { 574 {
570 unsigned char *p = (unsigned char *) data; 575 unsigned char *p = (unsigned char *) data;
670 if (Lstream_flush (lstr) < 0) 675 if (Lstream_flush (lstr) < 0)
671 return -1; 676 return -1;
672 lstr->byte_count = 0; 677 lstr->byte_count = 0;
673 return (lstr->imp->rewinder) (lstr); 678 return (lstr->imp->rewinder) (lstr);
674 } 679 }
675 680
676 int 681 int
677 Lstream_seekable_p (Lstream *lstr) 682 Lstream_seekable_p (Lstream *lstr)
678 { 683 {
679 if (!lstr->imp->rewinder) 684 if (!lstr->imp->rewinder)
680 return 0; 685 return 0;
972 static int 977 static int
973 filedesc_writer (Lstream *stream, CONST unsigned char *data, int size) 978 filedesc_writer (Lstream *stream, CONST unsigned char *data, int size)
974 { 979 {
975 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream); 980 struct filedesc_stream *str = FILEDESC_STREAM_DATA (stream);
976 int retval; 981 int retval;
977 int need_newline = 0; 982 int need_newline = 0;
978 983
979 /* 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
980 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
981 the PTY drivers right so this problem doesn't exist? 986 the PTY drivers right so this problem doesn't exist?
982 987
1180 str->init_offset = offset; 1185 str->init_offset = offset;
1181 str->obj = string; 1186 str->obj = string;
1182 XSETLSTREAM (obj, lstr); 1187 XSETLSTREAM (obj, lstr);
1183 return obj; 1188 return obj;
1184 } 1189 }
1185 1190
1186 static int 1191 static int
1187 lisp_string_reader (Lstream *stream, unsigned char *data, int size) 1192 lisp_string_reader (Lstream *stream, unsigned char *data, int size)
1188 { 1193 {
1189 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream); 1194 struct lisp_string_stream *str = LISP_STRING_STREAM_DATA (stream);
1190 /* Don't lose if the string shrank past us ... */ 1195 /* Don't lose if the string shrank past us ... */
1262 str->inbuf = buf; 1267 str->inbuf = buf;
1263 str->size = size; 1268 str->size = size;
1264 XSETLSTREAM (obj, lstr); 1269 XSETLSTREAM (obj, lstr);
1265 return obj; 1270 return obj;
1266 } 1271 }
1267 1272
1268 Lisp_Object 1273 Lisp_Object
1269 make_fixed_buffer_output_stream (unsigned char *buf, int size) 1274 make_fixed_buffer_output_stream (unsigned char *buf, int size)
1270 { 1275 {
1271 Lisp_Object obj = Qnil; 1276 Lisp_Object obj = Qnil;
1272 Lstream *lstr; 1277 Lstream *lstr;
1349 { 1354 {
1350 Lisp_Object obj = Qnil; 1355 Lisp_Object obj = Qnil;
1351 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w")); 1356 XSETLSTREAM (obj, Lstream_new (lstream_resizing_buffer, "w"));
1352 return obj; 1357 return obj;
1353 } 1358 }
1354 1359
1355 static int 1360 static int
1356 resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, int size) 1361 resizing_buffer_writer (Lstream *stream, CONST unsigned char *data, int size)
1357 { 1362 {
1358 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream); 1363 struct resizing_buffer_stream *str = RESIZING_BUFFER_STREAM_DATA (stream);
1359 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char); 1364 DO_REALLOC (str->buf, str->allocked, str->stored + size, unsigned char);
1411 Lisp_Object obj = Qnil; 1416 Lisp_Object obj = Qnil;
1412 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w")); 1417 XSETLSTREAM (obj, Lstream_new (lstream_dynarr, "w"));
1413 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn; 1418 DYNARR_STREAM_DATA (XLSTREAM (obj))->dyn = dyn;
1414 return obj; 1419 return obj;
1415 } 1420 }
1416 1421
1417 static int 1422 static int
1418 dynarr_writer (Lstream *stream, CONST unsigned char *data, int size) 1423 dynarr_writer (Lstream *stream, CONST unsigned char *data, int size)
1419 { 1424 {
1420 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream); 1425 struct dynarr_stream *str = DYNARR_STREAM_DATA (stream);
1421 Dynarr_add_many (str->dyn, data, size); 1426 Dynarr_add_many (str->dyn, data, size);
1467 int reading = !strcmp (mode, "r"); 1472 int reading = !strcmp (mode, "r");
1468 1473
1469 /* Make sure the luser didn't pass "w" in. */ 1474 /* Make sure the luser didn't pass "w" in. */
1470 if (!strcmp (mode, "w")) 1475 if (!strcmp (mode, "w"))
1471 abort (); 1476 abort ();
1472 1477
1473 if (flags & LSTR_IGNORE_ACCESSIBLE) 1478 if (flags & LSTR_IGNORE_ACCESSIBLE)
1474 { 1479 {
1475 bmin = BUF_BEG (buf); 1480 bmin = BUF_BEG (buf);
1476 bmax = BUF_Z (buf); 1481 bmax = BUF_Z (buf);
1477 } 1482 }
1560 start = bytind_clip_to_bounds (BI_BUF_BEGV (buf), start, 1565 start = bytind_clip_to_bounds (BI_BUF_BEGV (buf), start,
1561 BI_BUF_ZV (buf)); 1566 BI_BUF_ZV (buf));
1562 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end, 1567 end = bytind_clip_to_bounds (BI_BUF_BEGV (buf), end,
1563 BI_BUF_ZV (buf)); 1568 BI_BUF_ZV (buf));
1564 } 1569 }
1565 1570
1566 size = min (size, end - start); 1571 size = min (size, end - start);
1567 end = start + size; 1572 end = start + size;
1568 /* We cannot return a partial character. */ 1573 /* We cannot return a partial character. */
1569 VALIDATE_BYTIND_BACKWARD (buf, end); 1574 VALIDATE_BYTIND_BACKWARD (buf, end);
1570 1575