annotate src/lstream.h @ 394:7d59cb494b73 r21-2-12

Import from CVS: tag r21-2-12
author cvs
date Mon, 13 Aug 2007 11:11:37 +0200
parents c5d627a313b1
children 74fd4e045ea6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Generic stream implementation -- header file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1995 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 Copyright (C) 1996 Ben Wing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 /* Written by Ben Wing. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 #ifndef _XEMACS_LSTREAM_H_
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #define _XEMACS_LSTREAM_H_
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 /* definition of Lstream object */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 DECLARE_LRECORD (lstream, struct lstream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 #define XLSTREAM(x) XRECORD (x, lstream, struct lstream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #define XSETLSTREAM(x, p) XSETRECORD (x, p, lstream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 #define LSTREAMP(x) RECORDP (x, lstream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 /* #define CHECK_LSTREAM(x) CHECK_RECORD (x, lstream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 Lstream pointers should never escape to the Lisp level, so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 functions should not be doing this. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #ifndef EOF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #define EOF (-1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 typedef enum lstream_buffering
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 /* No buffering. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 LSTREAM_UNBUFFERED,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 /* Buffer until a '\n' character is reached. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 LSTREAM_LINE_BUFFERED,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 /* Buffer in standard-size (i.e. 512-byte) blocks. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 LSTREAM_BLOCK_BUFFERED,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 /* Buffer in blocks of a specified size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 LSTREAM_BLOCKN_BUFFERED,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 /* Buffer until the stream is closed (only applies to write-only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 streams). Only one call to the stream writer will be made,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 and that is when the stream is closed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 LSTREAM_UNLIMITED
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 } Lstream_buffering;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 /* Methods defining how this stream works. Some may be undefined. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 /* We do not implement the seek/tell paradigm. I tried to do that,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 but getting the semantics right in the presence of buffering is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 extremely tricky and very error-prone and basically not worth it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 This is especially the case with complicated streams like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 decoding streams -- the seek pointer in this case can't be a single
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 integer but has to be a whole complicated structure that records
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 all of the stream's state at the time.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 Rewind semantics are generally easy to implement, so we do provide
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 a rewind method. Even rewind() may not be available on a stream,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 however -- e.g. on process output. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 typedef struct lstream_implementation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 CONST char *name;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
78 size_t size; /* Number of additional bytes to be allocated with this
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
79 stream. Access this data using Lstream_data(). */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 /* Read some data from the stream's end and store it into DATA, which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 can hold SIZE bytes. Return the number of bytes read. A return
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 value of 0 means no bytes can be read at this time. This may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 be because of an EOF, or because there is a granularity greater
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 than one byte that the stream imposes on the returned data, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 SIZE is less than this granularity. (This will happen frequently
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 for streams that need to return whole characters, because
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 Lstream_read() calls the reader function repeatedly until it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 has the number of bytes it wants or until 0 is returned.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 The lstream functions do not treat a 0 return as EOF or do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 anything special; however, the calling function will interpret
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 any 0 it gets back as EOF. This will normally not happen unless
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 the caller calls Lstream_read() with a very small size.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 This function can be NULL if the stream is output-only. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 /* The omniscient mly, blinded by the irresistable thrall of Common
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 Lisp, thinks that it is bogus that the types and implementations
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 of input and output streams are the same. */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
98 int (*reader) (Lstream *stream, unsigned char *data, size_t size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 /* Send some data to the stream's end. Data to be sent is in DATA
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 and is SIZE bytes. Return the number of bytes sent. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 function can send and return fewer bytes than is passed in; in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 that case, the function will just be called again until there is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 no data left or 0 is returned. A return value of 0 means that no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 more data can be currently stored, but there is no error; the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 data will be squirrelled away until the writer can accept
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 data. (This is useful, e.g., of you're dealing with a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 non-blocking file descriptor and are getting EWOULDBLOCK errors.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 This function can be NULL if the stream is input-only. */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
109 int (*writer) (Lstream *stream, CONST unsigned char *data, size_t size);
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 219
diff changeset
110 /* Return non-zero if the last write operation on the stream resulted
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 219
diff changeset
111 in an attempt to block (EWOULDBLOCK). If this method does not
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 219
diff changeset
112 exists, the implementation returns 0 */
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 219
diff changeset
113 int (*was_blocked_p) (Lstream *stream);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 /* Rewind the stream. If this is NULL, the stream is not seekable. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 int (*rewinder) (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 /* Indicate whether this stream is seekable -- i.e. it can be rewound.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 This method is ignored if the stream does not have a rewind
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 method. If this method is not present, the result is determined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 by whether a rewind method is present. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 int (*seekable_p) (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 /* Perform any additional operations necessary to flush the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 data in this stream. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 int (*flusher) (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 /* Perform any additional operations necessary to close this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 stream down. May be NULL. This function is called when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 Lstream_close() is called or when the stream is garbage-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 collected. When this function is called, all pending data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 in the stream will already have been written out. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 int (*closer) (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 /* Mark this object for garbage collection. Same semantics as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 a standard Lisp_Object marker. This function can be NULL. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 Lisp_Object (*marker) (Lisp_Object lstream, void (*markfun) (Lisp_Object));
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 70
diff changeset
133 } Lstream_implementation;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 #define DEFINE_LSTREAM_IMPLEMENTATION(name,c_name,size) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 Lstream_implementation c_name[1] = \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 { { (name), (size) } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
219
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 185
diff changeset
139 #define LSTREAM_FL_IS_OPEN 1
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 185
diff changeset
140 #define LSTREAM_FL_READ 2
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 185
diff changeset
141 #define LSTREAM_FL_WRITE 4
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 185
diff changeset
142 #define LSTREAM_FL_NO_PARTIAL_CHARS 8
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents: 185
diff changeset
143 #define LSTREAM_FL_CLOSE_AT_DISKSAVE 16
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 struct lstream
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 struct lcrecord_header header;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 CONST Lstream_implementation *imp; /* methods for this stream */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 Lstream_buffering buffering; /* type of buffering in use */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
150 size_t buffering_size; /* number of bytes buffered */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 unsigned char *in_buffer; /* holds characters read from stream end */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
153 size_t in_buffer_size; /* allocated size of buffer */
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
154 size_t in_buffer_current; /* number of characters in buffer */
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
155 size_t in_buffer_ind; /* pointer to next character to take from buffer */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 unsigned char *out_buffer; /* holds characters to write to stream end */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
158 size_t out_buffer_size; /* allocated size of buffer */
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
159 size_t out_buffer_ind; /* pointer to next buffer spot to write a character */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 /* The unget buffer is more or less a stack -- things get pushed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 onto the end and read back from the end. Lstream_read()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 basically reads backwards from the end to get stuff; Lstream_unread()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 similarly has to push the data on backwards. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 unsigned char *unget_buffer; /* holds characters pushed back onto input */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
166 size_t unget_buffer_size; /* allocated size of buffer */
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
167 size_t unget_buffer_ind; /* pointer to next buffer spot to write a character */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
169 size_t byte_count;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 long flags; /* Align pointer for 64 bit machines (kny) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 char data[1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 #define LSTREAM_TYPE_P(lstr, type) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 ((lstr)->imp == lstream_##type)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 #ifdef ERROR_CHECK_TYPECHECK
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
178 INLINE struct lstream *
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
179 error_check_lstream_type (struct lstream *stream,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
180 CONST Lstream_implementation *imp);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
181 INLINE struct lstream *
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
182 error_check_lstream_type (struct lstream *stream,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
183 CONST Lstream_implementation *imp)
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
184 {
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
185 assert (stream->imp == imp);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
186 return stream;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
187 }
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
188 # define LSTREAM_TYPE_DATA(lstr, type) \
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
189 ((struct type##_stream *) \
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
190 Lstream_data (error_check_lstream_type(lstr, lstream_##type)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 # define LSTREAM_TYPE_DATA(lstr, type) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 ((struct type##_stream *) Lstream_data (lstr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 /* Declare that lstream-type TYPE has method M; used in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 initialization routines */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 #define LSTREAM_HAS_METHOD(type, m) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (lstream_##type->m = type##_##m)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 Lstream *Lstream_new (CONST Lstream_implementation *imp,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 CONST char *mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 void Lstream_reopen (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 void Lstream_set_buffering (Lstream *lstr, Lstream_buffering buffering,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 int buffering_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 int Lstream_flush (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 int Lstream_flush_out (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 int Lstream_fputc (Lstream *lstr, int c);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 int Lstream_fgetc (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 void Lstream_fungetc (Lstream *lstr, int c);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
212 int Lstream_read (Lstream *lstr, void *data, size_t size);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
213 int Lstream_write (Lstream *lstr, CONST void *data, size_t size);
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 219
diff changeset
214 int Lstream_was_blocked_p (Lstream *lstr);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
215 void Lstream_unread (Lstream *lstr, CONST void *data, size_t size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 int Lstream_rewind (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 int Lstream_seekable_p (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 int Lstream_close (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 void Lstream_delete (Lstream *lstr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 void Lstream_set_character_mode (Lstream *str);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 /* Call the function equivalent if the out buffer is full. Otherwise,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 add to the end of the out buffer and, if line buffering is called for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 and the character marks the end of a line, write out the buffer. */
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 70
diff changeset
225
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 #define Lstream_putc(stream, c) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 ((stream)->out_buffer_ind >= (stream)->out_buffer_size ? \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 Lstream_fputc (stream, c) : \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 ((stream)->out_buffer[(stream)->out_buffer_ind++] = \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (unsigned char) (c), \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (stream)->byte_count++, \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (stream)->buffering == LSTREAM_LINE_BUFFERED && \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (stream)->out_buffer[(stream)->out_buffer_ind - 1] == '\n' ? \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 Lstream_flush_out (stream) : 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 /* Retrieve from unget buffer if there are any characters there;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 else retrieve from in buffer if there's anything there;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 else call the function equivalent */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 #define Lstream_getc(stream) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ((stream)->unget_buffer_ind > 0 ? \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 ((stream)->byte_count++, \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (stream)->unget_buffer[--(stream)->unget_buffer_ind]) : \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (stream)->in_buffer_ind < (stream)->in_buffer_current ? \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 ((stream)->byte_count++, \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (stream)->in_buffer[(stream)->in_buffer_ind++]) : \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 Lstream_fgetc (stream))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 /* Add to the end if it won't overflow buffer; otherwise call the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 function equivalent */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 #define Lstream_ungetc(stream, c) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 ((stream)->unget_buffer_ind >= (stream)->unget_buffer_size ? \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 Lstream_fungetc (stream, c) : \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (void) ((stream)->byte_count--, \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 ((stream)->unget_buffer[(stream)->unget_buffer_ind++] = \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (unsigned char) (c))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 #define Lstream_data(stream) ((void *) ((stream)->data))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 #define Lstream_byte_count(stream) ((stream)->byte_count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 /* working with an Lstream as a stream of Emchars */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
265 #ifdef MULE
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
266
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
267 #ifndef BYTE_ASCII_P
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
268 #include "mule-charset.h"
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
269 #endif
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
270
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
271 INLINE Emchar Lstream_get_emchar (Lstream *stream);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
272 INLINE Emchar
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
273 Lstream_get_emchar (Lstream *stream)
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
274 {
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
275 int c = Lstream_getc (stream);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
276 return BYTE_ASCII_P (c) ? (Emchar) c :
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
277 Lstream_get_emchar_1 (stream, c);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
278 }
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
279
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
280 INLINE int Lstream_put_emchar (Lstream *stream, Emchar ch);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
281 INLINE int
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
282 Lstream_put_emchar (Lstream *stream, Emchar ch)
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
283 {
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
284 return CHAR_ASCII_P (ch) ?
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
285 Lstream_putc (stream, ch) :
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
286 Lstream_fput_emchar (stream, ch);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
287 }
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
288
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
289 INLINE void Lstream_unget_emchar (Lstream *stream, Emchar ch);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
290 INLINE void
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
291 Lstream_unget_emchar (Lstream *stream, Emchar ch)
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
292 {
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
293 if (CHAR_ASCII_P (ch))
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
294 Lstream_ungetc (stream, ch);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
295 else
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
296 Lstream_funget_emchar (stream, ch);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
297 }
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
298 #else /* not MULE */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
299
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 # define Lstream_get_emchar(stream) Lstream_getc (stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 # define Lstream_put_emchar(stream, ch) Lstream_putc (stream, ch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 # define Lstream_unget_emchar(stream, ch) Lstream_ungetc (stream, ch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
304 #endif /* not MULE */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
305
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 /* Lstream implementations */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 /************************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 /* Flags we can pass to the filedesc and stdio streams. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 /* If set, close the descriptor or FILE * when the stream is closed. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 #define LSTR_CLOSING 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 /* If set, allow quitting out of the actual I/O. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 #define LSTR_ALLOW_QUIT 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 /* If set and filedesc_stream_set_pty_flushing() has been called
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 on the stream, do not send more than pty_max_bytes on a single
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 line without flushing the data out using the eof_char. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 #define LSTR_PTY_FLUSHING 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 /* If set, an EWOULDBLOCK error is not treated as an error but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 simply causes the write function to return 0 as the number
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 of bytes written out. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 #define LSTR_BLOCKED_OK 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 Lisp_Object make_stdio_input_stream (FILE *stream, int flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 Lisp_Object make_stdio_output_stream (FILE *stream, int flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 Lisp_Object make_filedesc_input_stream (int filedesc, int offset, int count,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 int flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 Lisp_Object make_filedesc_output_stream (int filedesc, int offset, int count,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 int flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 void filedesc_stream_set_pty_flushing (Lstream *stream,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 int pty_max_bytes,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 Bufbyte eof_char);
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 219
diff changeset
338 int filedesc_stream_fd (Lstream *stream);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 Lisp_Object make_lisp_string_input_stream (Lisp_Object string,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 Bytecount offset,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 Bytecount len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 Lisp_Object make_fixed_buffer_input_stream (CONST unsigned char *buf,
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
343 size_t size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 Lisp_Object make_fixed_buffer_output_stream (unsigned char *buf,
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
345 size_t size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 CONST unsigned char *fixed_buffer_input_stream_ptr (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 unsigned char *fixed_buffer_output_stream_ptr (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 Lisp_Object make_resizing_buffer_output_stream (void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 unsigned char *resizing_buffer_stream_ptr (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 Lisp_Object make_dynarr_output_stream (unsigned_char_dynarr *dyn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 #define LSTR_SELECTIVE 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 #define LSTR_IGNORE_ACCESSIBLE 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 Lisp_Object make_lisp_buffer_input_stream (struct buffer *buf, Bufpos start,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 Bufpos end, int flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 Lisp_Object make_lisp_buffer_output_stream (struct buffer *buf, Bufpos pos,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 int flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 Bufpos lisp_buffer_stream_startpos (Lstream *stream);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 #endif /* _XEMACS_LSTREAM_H_ */