comparison src/lstream.h @ 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
39 functions should not be doing this. */ 39 functions should not be doing this. */
40 40
41 #ifndef EOF 41 #ifndef EOF
42 #define EOF (-1) 42 #define EOF (-1)
43 #endif 43 #endif
44
45 /* Typedef specifying a count of bytes in a data block to be written
46 out or read in, using Lstream_read(), Lstream_write(), and related
47 functions. This MUST BE SIGNED, since it also is used in functions
48 that return the number of bytes actually read to or written from in
49 an operation, and these functions can return -1 to signal error.
50
51 Note that the standard Unix read() and write() functions define the
52 count going in as a size_t, which is UNSIGNED, and the count going
53 out as an ssize_t, which is SIGNED. This is a horrible design
54 flaw. Not only is it highly likely to lead to logic errors when a
55 -1 gets interpreted as a large positive number, but operations are
56 bound to fail in all sorts of horrible ways when a number in the
57 upper-half of the size_t range is passed in -- this number is
58 unrepresentable as an ssize_t, so code that checks to see how many
59 bytes are actually written (which is mandatory if you are dealing
60 with certain types of devices) will get completely screwed up.
61 */
62
63 typedef EMACS_INT Lstream_data_count;
44 64
45 typedef enum lstream_buffering 65 typedef enum lstream_buffering
46 { 66 {
47 /* No buffering. */ 67 /* No buffering. */
48 LSTREAM_UNBUFFERED, 68 LSTREAM_UNBUFFERED,
73 however -- e.g. on process output. */ 93 however -- e.g. on process output. */
74 94
75 typedef struct lstream_implementation 95 typedef struct lstream_implementation
76 { 96 {
77 const char *name; 97 const char *name;
78 size_t size; /* Number of additional bytes to be allocated with this 98 Lstream_data_count size; /* Number of additional bytes to be
79 stream. Access this data using Lstream_data(). */ 99 allocated with this stream. Access this
100 data using Lstream_data(). */
80 /* Read some data from the stream's end and store it into DATA, which 101 /* Read some data from the stream's end and store it into DATA, which
81 can hold SIZE bytes. Return the number of bytes read. A return 102 can hold SIZE bytes. Return the number of bytes read. A return
82 value of 0 means no bytes can be read at this time. This may 103 value of 0 means no bytes can be read at this time. This may
83 be because of an EOF, or because there is a granularity greater 104 be because of an EOF, or because there is a granularity greater
84 than one byte that the stream imposes on the returned data, and 105 than one byte that the stream imposes on the returned data, and
93 114
94 This function can be NULL if the stream is output-only. */ 115 This function can be NULL if the stream is output-only. */
95 /* The omniscient mly, blinded by the irresistible thrall of Common 116 /* The omniscient mly, blinded by the irresistible thrall of Common
96 Lisp, thinks that it is bogus that the types and implementations 117 Lisp, thinks that it is bogus that the types and implementations
97 of input and output streams are the same. */ 118 of input and output streams are the same. */
98 ssize_t (*reader) (Lstream *stream, unsigned char *data, size_t size); 119 Lstream_data_count (*reader) (Lstream *stream, unsigned char *data,
120 Lstream_data_count size);
99 /* Send some data to the stream's end. Data to be sent is in DATA 121 /* Send some data to the stream's end. Data to be sent is in DATA
100 and is SIZE bytes. Return the number of bytes sent. This 122 and is SIZE bytes. Return the number of bytes sent. This
101 function can send and return fewer bytes than is passed in; in 123 function can send and return fewer bytes than is passed in; in
102 that case, the function will just be called again until there is 124 that case, the function will just be called again until there is
103 no data left or 0 is returned. A return value of 0 means that no 125 no data left or 0 is returned. A return value of 0 means that no
104 more data can be currently stored, but there is no error; the 126 more data can be currently stored, but there is no error; the
105 data will be squirrelled away until the writer can accept 127 data will be squirrelled away until the writer can accept
106 data. (This is useful, e.g., of you're dealing with a 128 data. (This is useful, e.g., of you're dealing with a
107 non-blocking file descriptor and are getting EWOULDBLOCK errors.) 129 non-blocking file descriptor and are getting EWOULDBLOCK errors.)
108 This function can be NULL if the stream is input-only. */ 130 This function can be NULL if the stream is input-only. */
109 ssize_t (*writer) (Lstream *stream, const unsigned char *data, size_t size); 131 Lstream_data_count (*writer) (Lstream *stream, const unsigned char *data,
132 Lstream_data_count size);
110 /* Return non-zero if the last write operation on the stream resulted 133 /* Return non-zero if the last write operation on the stream resulted
111 in an attempt to block (EWOULDBLOCK). If this method does not 134 in an attempt to block (EWOULDBLOCK). If this method does not
112 exists, the implementation returns 0 */ 135 exists, the implementation returns 0 */
113 int (*was_blocked_p) (Lstream *stream); 136 int (*was_blocked_p) (Lstream *stream);
114 /* Rewind the stream. If this is NULL, the stream is not seekable. */ 137 /* Rewind the stream. If this is NULL, the stream is not seekable. */
145 struct lstream 168 struct lstream
146 { 169 {
147 struct lcrecord_header header; 170 struct lcrecord_header header;
148 const Lstream_implementation *imp; /* methods for this stream */ 171 const Lstream_implementation *imp; /* methods for this stream */
149 Lstream_buffering buffering; /* type of buffering in use */ 172 Lstream_buffering buffering; /* type of buffering in use */
150 size_t buffering_size; /* number of bytes buffered */ 173 Lstream_data_count buffering_size; /* number of bytes buffered */
151 174
152 unsigned char *in_buffer; /* holds characters read from stream end */ 175 unsigned char *in_buffer; /* holds characters read from stream end */
153 size_t in_buffer_size; /* allocated size of buffer */ 176 Lstream_data_count in_buffer_size; /* allocated size of buffer */
154 size_t in_buffer_current; /* number of characters in buffer */ 177 Lstream_data_count in_buffer_current; /* number of characters in buffer */
155 size_t in_buffer_ind; /* pointer to next character to take from buffer */ 178 Lstream_data_count in_buffer_ind; /* pointer to next character to
179 take from buffer */
156 180
157 unsigned char *out_buffer; /* holds characters to write to stream end */ 181 unsigned char *out_buffer; /* holds characters to write to stream end */
158 size_t out_buffer_size; /* allocated size of buffer */ 182 Lstream_data_count out_buffer_size; /* allocated size of buffer */
159 size_t out_buffer_ind; /* pointer to next buffer spot to write a character */ 183 Lstream_data_count out_buffer_ind; /* pointer to next buffer spot to
184 write a character */
160 185
161 /* The unget buffer is more or less a stack -- things get pushed 186 /* The unget buffer is more or less a stack -- things get pushed
162 onto the end and read back from the end. Lstream_read() 187 onto the end and read back from the end. Lstream_read()
163 basically reads backwards from the end to get stuff; Lstream_unread() 188 basically reads backwards from the end to get stuff; Lstream_unread()
164 similarly has to push the data on backwards. */ 189 similarly has to push the data on backwards. */
165 unsigned char *unget_buffer; /* holds characters pushed back onto input */ 190 unsigned char *unget_buffer; /* holds characters pushed back onto input */
166 size_t unget_buffer_size; /* allocated size of buffer */ 191 Lstream_data_count unget_buffer_size; /* allocated size of buffer */
167 size_t unget_buffer_ind; /* pointer to next buffer spot to write a character */ 192 Lstream_data_count unget_buffer_ind; /* pointer to next buffer spot
168 193 to write a character */
169 size_t byte_count; 194
195 Lstream_data_count byte_count;
170 int flags; 196 int flags;
171 max_align_t data[1]; 197 max_align_t data[1];
172 }; 198 };
173 199
174 #define LSTREAM_TYPE_P(lstr, type) \ 200 #define LSTREAM_TYPE_P(lstr, type) \
207 int Lstream_flush (Lstream *lstr); 233 int Lstream_flush (Lstream *lstr);
208 int Lstream_flush_out (Lstream *lstr); 234 int Lstream_flush_out (Lstream *lstr);
209 int Lstream_fputc (Lstream *lstr, int c); 235 int Lstream_fputc (Lstream *lstr, int c);
210 int Lstream_fgetc (Lstream *lstr); 236 int Lstream_fgetc (Lstream *lstr);
211 void Lstream_fungetc (Lstream *lstr, int c); 237 void Lstream_fungetc (Lstream *lstr, int c);
212 ssize_t Lstream_read (Lstream *lstr, void *data, size_t size); 238 Lstream_data_count Lstream_read (Lstream *lstr, void *data,
213 ssize_t Lstream_write (Lstream *lstr, const void *data, size_t size); 239 Lstream_data_count size);
240 Lstream_data_count Lstream_write (Lstream *lstr, const void *data,
241 Lstream_data_count size);
214 int Lstream_was_blocked_p (Lstream *lstr); 242 int Lstream_was_blocked_p (Lstream *lstr);
215 void Lstream_unread (Lstream *lstr, const void *data, size_t size); 243 void Lstream_unread (Lstream *lstr, const void *data, Lstream_data_count size);
216 int Lstream_rewind (Lstream *lstr); 244 int Lstream_rewind (Lstream *lstr);
217 int Lstream_seekable_p (Lstream *lstr); 245 int Lstream_seekable_p (Lstream *lstr);
218 int Lstream_close (Lstream *lstr); 246 int Lstream_close (Lstream *lstr);
219 void Lstream_delete (Lstream *lstr); 247 void Lstream_delete (Lstream *lstr);
220 void Lstream_set_character_mode (Lstream *str); 248 void Lstream_set_character_mode (Lstream *str);
338 Bufbyte eof_char); 366 Bufbyte eof_char);
339 int filedesc_stream_fd (Lstream *stream); 367 int filedesc_stream_fd (Lstream *stream);
340 Lisp_Object make_lisp_string_input_stream (Lisp_Object string, 368 Lisp_Object make_lisp_string_input_stream (Lisp_Object string,
341 Bytecount offset, 369 Bytecount offset,
342 Bytecount len); 370 Bytecount len);
343 Lisp_Object make_fixed_buffer_input_stream (const void *buf, size_t size); 371 Lisp_Object make_fixed_buffer_input_stream (const void *buf,
344 Lisp_Object make_fixed_buffer_output_stream (void *buf, size_t size); 372 Lstream_data_count size);
373 Lisp_Object make_fixed_buffer_output_stream (void *buf,
374 Lstream_data_count size);
345 const unsigned char *fixed_buffer_input_stream_ptr (Lstream *stream); 375 const unsigned char *fixed_buffer_input_stream_ptr (Lstream *stream);
346 unsigned char *fixed_buffer_output_stream_ptr (Lstream *stream); 376 unsigned char *fixed_buffer_output_stream_ptr (Lstream *stream);
347 Lisp_Object make_resizing_buffer_output_stream (void); 377 Lisp_Object make_resizing_buffer_output_stream (void);
348 unsigned char *resizing_buffer_stream_ptr (Lstream *stream); 378 unsigned char *resizing_buffer_stream_ptr (Lstream *stream);
349 Lisp_Object make_dynarr_output_stream (unsigned_char_dynarr *dyn); 379 Lisp_Object make_dynarr_output_stream (unsigned_char_dynarr *dyn);