comparison packages/language/c/libc/stdio/current/include/stream.hxx @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children b939e9cada46
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 #ifndef CYGONCE_LIBC_STDIO_STREAM_HXX
2 #define CYGONCE_LIBC_STDIO_STREAM_HXX
3 //========================================================================
4 //
5 // stream.hxx
6 //
7 // Internal C library stdio stream interface definitions
8 //
9 //========================================================================
10 //####COPYRIGHTBEGIN####
11 //
12 // -------------------------------------------
13 // The contents of this file are subject to the Red Hat eCos Public License
14 // Version 1.1 (the "License"); you may not use this file except in
15 // compliance with the License. You may obtain a copy of the License at
16 // http://www.redhat.com/
17 //
18 // Software distributed under the License is distributed on an "AS IS"
19 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
20 // License for the specific language governing rights and limitations under
21 // the License.
22 //
23 // The Original Code is eCos - Embedded Configurable Operating System,
24 // released September 30, 1998.
25 //
26 // The Initial Developer of the Original Code is Red Hat.
27 // Portions created by Red Hat are
28 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
29 // All Rights Reserved.
30 // -------------------------------------------
31 //
32 //####COPYRIGHTEND####
33 //========================================================================
34 //#####DESCRIPTIONBEGIN####
35 //
36 // Author(s): jlarmour
37 // Contributors:
38 // Date: 2000-04-19
39 // Purpose:
40 // Description:
41 // Usage: #include <cyg/libc/stdio/stream.hxx>
42 //
43 //####DESCRIPTIONEND####
44 //
45 //========================================================================
46
47 // CONFIGURATION
48
49 #include <pkgconf/libc_stdio.h> // Configuration header
50
51 // INCLUDES
52
53 #include <cyg/infra/cyg_type.h> // Common project-wide type definitions
54 #include <cyg/infra/cyg_ass.h> // Get assertion macros, as appropriate
55 #include <errno.h> // Cyg_ErrNo
56 #include <stdio.h> // fpos_t and IOBUF defines
57 #include <cyg/libc/stdio/io.hxx> // Physical IO support
58 #include <cyg/libc/stdio/streambuf.hxx> // Stdio stream file buffers
59
60 #ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
61 #include <pkgconf/kernel.h>
62 #include <cyg/kernel/mutex.hxx> // Cyg_Mutex
63 #endif
64
65 // TYPE DEFINITIONS
66
67 class Cyg_StdioStream
68 {
69 friend int setvbuf( FILE *, char *, int, size_t );
70
71 private:
72
73 // error status for this file
74 Cyg_ErrNo error;
75
76
77 cyg_stdio_handle_t my_device;
78
79 #ifdef CYGFUN_LIBC_STDIO_ungetc
80 cyg_uint8 unread_char_buf;
81 #endif
82
83 #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
84 Cyg_StdioStreamBuffer io_buf; // read/write buffer
85 #endif
86 cyg_uint8 readbuf_char; // a one character emergency "buffer"
87 // only used when configured to not buffer
88 // (i.e. !CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO)
89 // or set at runtime to not buffer (i.e.
90 // buffering mode is _IONBF)
91
92 // some flags indicating the state of the file. Some of it is internal
93 // state, which should not be public. Use bitfields to save
94 // space, which means using "unsigned int" rather than cyg_uintX
95 struct {
96 unsigned int at_eof : 1; // Have we reached eof?
97
98 unsigned int opened_for_read : 1; // opened_for_read and
99
100 unsigned int opened_for_write : 1; // opened_for_write can
101 // be set simultaneously
102
103 unsigned int binary : 1; // opened for binary or
104 // text mode?
105
106 #ifdef CYGFUN_LIBC_STDIO_ungetc
107 unsigned int unread_char_buf_in_use : 1; // unget buf in use?
108 #endif
109
110 #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
111 unsigned int buffering : 1; // Is this file buffered?
112
113 unsigned int line_buffering : 1; // If so, is it line
114 // buffered? If it is
115 // buffered, but NOT line
116 // buffered, it must be
117 // fully buffered
118
119 unsigned int last_buffer_op_was_read : 1; // did we last read from
120 // the buffer. If not we
121 // must have written
122 #endif
123 unsigned int readbuf_char_in_use : 1; // is the above
124 // readbuf_char in use?
125
126 } flags;
127
128 // current position for reading/writing
129 fpos_t position;
130
131 #ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
132 Cyg_Mutex stream_lock; // used for locking this stream
133 #endif // ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
134
135 #ifdef CYGDBG_USE_ASSERTS
136 // a value to check that this class is hopefully valid
137 cyg_ucount32 magic_validity_word;
138 #endif
139
140 public:
141 // different modes when constructing (i.e. opening).
142 typedef enum {
143 CYG_STREAM_READ,
144 CYG_STREAM_WRITE,
145 CYG_STREAM_READWRITE
146 } OpenMode;
147
148 // CONSTRUCTORS
149
150 // This constructs the stream - effectively opens the file. This is
151 // used for static initialisation, and actually calls construct below
152 //
153 // dev is a valid Cyg_Device_Table_t, although it does not have to
154 // be a member of the device table object itself
155 //
156 // open_mode is one of CYG_STREAM_READ, CYG_STREAM_WRITE or
157 // CYG_STREAM_READWRITE
158 //
159 // append is true if the file position should be set at EOF on opening
160 //
161 // binary is true if this is a binary stream. Otherwise it is a text
162 // stream and character conversions (especially newline) may occur
163 //
164 // buffer_mode is one of _IONBF, _IOLBF, _IOFBF (from <stdio.h>)
165 // If buffer_mode is _IONBF, buffer_size should still be set to 0
166 // and buffer_addr to NULL. If buffering is not configured, none
167 // of buffer_mode, buffer_size and buffer_addr have any effect
168 //
169 // buffer_size is the size of buffer to use
170 //
171 // buffer_addr is the address of a user supplied buffer. By default
172 // (when NULL) a system one is provided.
173 //
174 // The "return code" is set by assignment to the error member of this
175 // stream - use the get_error() method to check
176
177 Cyg_StdioStream( cyg_stdio_handle_t dev, OpenMode open_mode,
178 cyg_bool append, cyg_bool binary, int buffer_mode,
179 cyg_ucount32 buffer_size=BUFSIZ,
180 cyg_uint8 *buffer_addr=NULL );
181
182 Cyg_StdioStream( OpenMode open_mode,
183 cyg_ucount32 buffer_size=BUFSIZ,
184 cyg_uint8 *buffer_addr=NULL );
185
186 private:
187 void initialize( cyg_stdio_handle_t dev, OpenMode open_mode,
188 cyg_bool append, cyg_bool binary, int buffer_mode,
189 cyg_ucount32 buffer_size=BUFSIZ,
190 cyg_uint8 *buffer_addr=NULL );
191 public:
192
193 // DESTRUCTOR
194
195 ~Cyg_StdioStream();
196
197
198 // MEMBER FUNCTIONS
199
200 // Close the stream. This should be called before the destructor,
201 // so we can see and report any errors produced.
202 Cyg_ErrNo close();
203
204 // Refill read buffer from the stream - note this blocks until
205 // something arrives on the stream
206 Cyg_ErrNo
207 refill_read_buffer( void );
208
209
210 // Read not more than buffer_length bytes from the read buffer into the
211 // user buffer.
212 // The number of bytes put into the user buffer is written
213 // into *bytes_read
214 Cyg_ErrNo
215 read( cyg_uint8 *user_buffer, cyg_ucount32 buffer_length,
216 cyg_ucount32 *bytes_read );
217
218
219 // Read a single byte from the stream. Returns EAGAIN if no character
220 // available or EEOF if end of file (as well as setting the EOF state)
221 Cyg_ErrNo
222 read_byte( cyg_uint8 *c );
223
224 // Read a single byte from the stream, but don't remove it. Returns
225 // EAGAIN if no character available or EEOF if end of file (as well
226 // as setting the EOF state)
227 Cyg_ErrNo
228 peek_byte( cyg_uint8 *c );
229
230
231 // Return a byte into the stream - basically the same as ungetc()
232 Cyg_ErrNo
233 unread_byte( cyg_uint8 c );
234
235 // the number of bytes available to read without needing to refill the
236 // buffer
237 cyg_ucount32
238 bytes_available_to_read( void );
239
240 Cyg_ErrNo
241 write( const cyg_uint8 *buffer, cyg_ucount32 buffer_length,
242 cyg_ucount32 *bytes_written );
243
244 Cyg_ErrNo
245 write_byte( cyg_uint8 c );
246
247
248 Cyg_ErrNo
249 flush_output( void );
250
251 Cyg_ErrNo
252 flush_output_unlocked( void );
253
254 // prevent multiple access in thread safe mode
255
256 // lock_me() returns false if it couldn't be locked, which could
257 // happen if the file descriptor is bad
258
259 cyg_bool
260 lock_me( void );
261
262 // trylock_me() returns false if it couldn't be locked, probably
263 // because it is already locked
264 cyg_bool
265 trylock_me( void );
266
267 void
268 unlock_me( void );
269
270 // get error status for this file
271 Cyg_ErrNo
272 get_error( void );
273
274 // set error status for this file.
275 void
276 set_error( Cyg_ErrNo errno_to_set );
277
278 public:
279 // are we at EOF? true means we are, false means no
280 cyg_bool
281 get_eof_state( void );
282
283 private:
284
285 // Set whether we are at EOF.
286 void
287 set_eof_state( cyg_bool eof_to_set );
288
289 public:
290 // retrieve position
291 Cyg_ErrNo
292 get_position( fpos_t *pos );
293
294 // set absolute position. whence is SEEK_SET, SEEK_CUR, or SEEK_END
295 Cyg_ErrNo
296 set_position( fpos_t pos, int whence );
297
298 // Return my_device
299 cyg_stdio_handle_t get_dev() { return my_device; };
300
301 CYGDBG_DEFINE_CHECK_THIS
302 };
303
304 // INLINE FUNCTIONS
305
306 #include <cyg/libc/stdio/stream.inl>
307
308 #endif CYGONCE_LIBC_STDIO_STREAM_HXX multiple inclusion protection
309
310 // EOF stream.hxx