1 |
27 |
unneback |
#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 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
11 |
|
|
// -------------------------------------------
|
12 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
13 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
14 |
|
|
//
|
15 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
16 |
|
|
// the terms of the GNU General Public License as published by the Free
|
17 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
18 |
|
|
//
|
19 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
20 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
21 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
22 |
|
|
// for more details.
|
23 |
|
|
//
|
24 |
|
|
// You should have received a copy of the GNU General Public License along
|
25 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
26 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
27 |
|
|
//
|
28 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
29 |
|
|
// or inline functions from this file, or you compile this file and link it
|
30 |
|
|
// with other works to produce a work based on this file, this file does not
|
31 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
32 |
|
|
// License. However the source code for this file must still be made available
|
33 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
36 |
|
|
// this file might be covered by the GNU General Public License.
|
37 |
|
|
//
|
38 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
39 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
40 |
|
|
// -------------------------------------------
|
41 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
42 |
|
|
//========================================================================
|
43 |
|
|
//#####DESCRIPTIONBEGIN####
|
44 |
|
|
//
|
45 |
|
|
// Author(s): jlarmour
|
46 |
|
|
// Contributors:
|
47 |
|
|
// Date: 2001-03-16
|
48 |
|
|
// Purpose:
|
49 |
|
|
// Description:
|
50 |
|
|
// Usage: #include
|
51 |
|
|
//
|
52 |
|
|
//####DESCRIPTIONEND####
|
53 |
|
|
//
|
54 |
|
|
//========================================================================
|
55 |
|
|
|
56 |
|
|
// CONFIGURATION
|
57 |
|
|
|
58 |
|
|
#include // Configuration header
|
59 |
|
|
|
60 |
|
|
// INCLUDES
|
61 |
|
|
|
62 |
|
|
#include // Common project-wide type definitions
|
63 |
|
|
#include // Get assertion macros, as appropriate
|
64 |
|
|
#include // Cyg_ErrNo
|
65 |
|
|
#include // fpos_t and IOBUF defines
|
66 |
|
|
#include // Physical IO support
|
67 |
|
|
#include // Stdio stream file buffers
|
68 |
|
|
|
69 |
|
|
#ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
|
70 |
|
|
#include
|
71 |
|
|
#include // Cyg_Mutex
|
72 |
|
|
#endif
|
73 |
|
|
|
74 |
|
|
// TYPE DEFINITIONS
|
75 |
|
|
|
76 |
|
|
class Cyg_StdioStream;
|
77 |
|
|
__externC Cyg_ErrNo
|
78 |
|
|
cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
|
79 |
|
|
|
80 |
|
|
class Cyg_StdioStream
|
81 |
|
|
{
|
82 |
|
|
friend int setvbuf( FILE *, char *, int, size_t );
|
83 |
|
|
friend Cyg_ErrNo
|
84 |
|
|
cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
|
85 |
|
|
|
86 |
|
|
private:
|
87 |
|
|
|
88 |
|
|
// error status for this file
|
89 |
|
|
Cyg_ErrNo error;
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
cyg_stdio_handle_t my_device;
|
93 |
|
|
|
94 |
|
|
#ifdef CYGFUN_LIBC_STDIO_ungetc
|
95 |
|
|
cyg_uint8 unread_char_buf;
|
96 |
|
|
#endif
|
97 |
|
|
|
98 |
|
|
#ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
|
99 |
|
|
Cyg_StdioStreamBuffer io_buf; // read/write buffer
|
100 |
|
|
#endif
|
101 |
|
|
cyg_uint8 readbuf_char; // a one character emergency "buffer"
|
102 |
|
|
// only used when configured to not buffer
|
103 |
|
|
// (i.e. !CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO)
|
104 |
|
|
// or set at runtime to not buffer (i.e.
|
105 |
|
|
// buffering mode is _IONBF)
|
106 |
|
|
|
107 |
|
|
// some flags indicating the state of the file. Some of it is internal
|
108 |
|
|
// state, which should not be public. Use bitfields to save
|
109 |
|
|
// space, which means using "unsigned int" rather than cyg_uintX
|
110 |
|
|
struct {
|
111 |
|
|
unsigned int at_eof : 1; // Have we reached eof?
|
112 |
|
|
|
113 |
|
|
unsigned int opened_for_read : 1; // opened_for_read and
|
114 |
|
|
|
115 |
|
|
unsigned int opened_for_write : 1; // opened_for_write can
|
116 |
|
|
// be set simultaneously
|
117 |
|
|
|
118 |
|
|
unsigned int binary : 1; // opened for binary or
|
119 |
|
|
// text mode?
|
120 |
|
|
|
121 |
|
|
#ifdef CYGFUN_LIBC_STDIO_ungetc
|
122 |
|
|
unsigned int unread_char_buf_in_use : 1; // unget buf in use?
|
123 |
|
|
#endif
|
124 |
|
|
|
125 |
|
|
#ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
|
126 |
|
|
unsigned int buffering : 1; // Is this file buffered?
|
127 |
|
|
|
128 |
|
|
unsigned int line_buffering : 1; // If so, is it line
|
129 |
|
|
// buffered? If it is
|
130 |
|
|
// buffered, but NOT line
|
131 |
|
|
// buffered, it must be
|
132 |
|
|
// fully buffered
|
133 |
|
|
|
134 |
|
|
unsigned int last_buffer_op_was_read : 1; // did we last read from
|
135 |
|
|
// the buffer. If not we
|
136 |
|
|
// must have written
|
137 |
|
|
#endif
|
138 |
|
|
unsigned int readbuf_char_in_use : 1; // is the above
|
139 |
|
|
// readbuf_char in use?
|
140 |
|
|
|
141 |
|
|
} flags;
|
142 |
|
|
|
143 |
|
|
// current position for reading/writing
|
144 |
|
|
fpos_t position;
|
145 |
|
|
|
146 |
|
|
#ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
|
147 |
|
|
Cyg_Mutex stream_lock; // used for locking this stream
|
148 |
|
|
#endif // ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
|
149 |
|
|
|
150 |
|
|
#ifdef CYGDBG_USE_ASSERTS
|
151 |
|
|
// a value to check that this class is hopefully valid
|
152 |
|
|
cyg_ucount32 magic_validity_word;
|
153 |
|
|
#endif
|
154 |
|
|
|
155 |
|
|
public:
|
156 |
|
|
// different modes when constructing (i.e. opening).
|
157 |
|
|
typedef enum {
|
158 |
|
|
CYG_STREAM_READ,
|
159 |
|
|
CYG_STREAM_WRITE,
|
160 |
|
|
CYG_STREAM_READWRITE
|
161 |
|
|
} OpenMode;
|
162 |
|
|
|
163 |
|
|
// CONSTRUCTORS
|
164 |
|
|
|
165 |
|
|
// This constructs the stream - effectively opens the file. This is
|
166 |
|
|
// used for static initialisation, and actually calls construct below
|
167 |
|
|
//
|
168 |
|
|
// dev is a valid Cyg_Device_Table_t, although it does not have to
|
169 |
|
|
// be a member of the device table object itself
|
170 |
|
|
//
|
171 |
|
|
// open_mode is one of CYG_STREAM_READ, CYG_STREAM_WRITE or
|
172 |
|
|
// CYG_STREAM_READWRITE
|
173 |
|
|
//
|
174 |
|
|
// append is true if the file position should be set at EOF on opening
|
175 |
|
|
//
|
176 |
|
|
// binary is true if this is a binary stream. Otherwise it is a text
|
177 |
|
|
// stream and character conversions (especially newline) may occur
|
178 |
|
|
//
|
179 |
|
|
// buffer_mode is one of _IONBF, _IOLBF, _IOFBF (from )
|
180 |
|
|
// If buffer_mode is _IONBF, buffer_size should still be set to 0
|
181 |
|
|
// and buffer_addr to NULL. If buffering is not configured, none
|
182 |
|
|
// of buffer_mode, buffer_size and buffer_addr have any effect
|
183 |
|
|
//
|
184 |
|
|
// buffer_size is the size of buffer to use
|
185 |
|
|
//
|
186 |
|
|
// buffer_addr is the address of a user supplied buffer. By default
|
187 |
|
|
// (when NULL) a system one is provided.
|
188 |
|
|
//
|
189 |
|
|
// The "return code" is set by assignment to the error member of this
|
190 |
|
|
// stream - use the get_error() method to check
|
191 |
|
|
|
192 |
|
|
Cyg_StdioStream( cyg_stdio_handle_t dev, OpenMode open_mode,
|
193 |
|
|
cyg_bool append, cyg_bool binary, int buffer_mode,
|
194 |
|
|
cyg_ucount32 buffer_size=BUFSIZ,
|
195 |
|
|
cyg_uint8 *buffer_addr=NULL );
|
196 |
|
|
|
197 |
|
|
Cyg_StdioStream( OpenMode open_mode,
|
198 |
|
|
cyg_ucount32 buffer_size=BUFSIZ,
|
199 |
|
|
cyg_uint8 *buffer_addr=NULL );
|
200 |
|
|
|
201 |
|
|
private:
|
202 |
|
|
void initialize( cyg_stdio_handle_t dev, OpenMode open_mode,
|
203 |
|
|
cyg_bool append, cyg_bool binary, int buffer_mode,
|
204 |
|
|
cyg_ucount32 buffer_size=BUFSIZ,
|
205 |
|
|
cyg_uint8 *buffer_addr=NULL );
|
206 |
|
|
public:
|
207 |
|
|
|
208 |
|
|
// DESTRUCTOR
|
209 |
|
|
|
210 |
|
|
~Cyg_StdioStream();
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
// MEMBER FUNCTIONS
|
214 |
|
|
|
215 |
|
|
// Close the stream. This should be called before the destructor,
|
216 |
|
|
// so we can see and report any errors produced.
|
217 |
|
|
Cyg_ErrNo close();
|
218 |
|
|
|
219 |
|
|
// Refill read buffer from the stream - note this blocks until
|
220 |
|
|
// something arrives on the stream
|
221 |
|
|
Cyg_ErrNo
|
222 |
|
|
refill_read_buffer( void );
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
// Read not more than buffer_length bytes from the read buffer into the
|
226 |
|
|
// user buffer.
|
227 |
|
|
// The number of bytes put into the user buffer is written
|
228 |
|
|
// into *bytes_read
|
229 |
|
|
Cyg_ErrNo
|
230 |
|
|
read( cyg_uint8 *user_buffer, cyg_ucount32 buffer_length,
|
231 |
|
|
cyg_ucount32 *bytes_read );
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
// Read a single byte from the stream. Returns EAGAIN if no character
|
235 |
|
|
// available or EEOF if end of file (as well as setting the EOF state)
|
236 |
|
|
Cyg_ErrNo
|
237 |
|
|
read_byte( cyg_uint8 *c );
|
238 |
|
|
|
239 |
|
|
// Read a single byte from the stream, but don't remove it. Returns
|
240 |
|
|
// EAGAIN if no character available or EEOF if end of file (as well
|
241 |
|
|
// as setting the EOF state)
|
242 |
|
|
Cyg_ErrNo
|
243 |
|
|
peek_byte( cyg_uint8 *c );
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
// Return a byte into the stream - basically the same as ungetc()
|
247 |
|
|
Cyg_ErrNo
|
248 |
|
|
unread_byte( cyg_uint8 c );
|
249 |
|
|
|
250 |
|
|
// the number of bytes available to read without needing to refill the
|
251 |
|
|
// buffer
|
252 |
|
|
cyg_ucount32
|
253 |
|
|
bytes_available_to_read( void );
|
254 |
|
|
|
255 |
|
|
Cyg_ErrNo
|
256 |
|
|
write( const cyg_uint8 *buffer, cyg_ucount32 buffer_length,
|
257 |
|
|
cyg_ucount32 *bytes_written );
|
258 |
|
|
|
259 |
|
|
Cyg_ErrNo
|
260 |
|
|
write_byte( cyg_uint8 c );
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
Cyg_ErrNo
|
264 |
|
|
flush_output( void );
|
265 |
|
|
|
266 |
|
|
Cyg_ErrNo
|
267 |
|
|
flush_output_unlocked( void );
|
268 |
|
|
|
269 |
|
|
// prevent multiple access in thread safe mode
|
270 |
|
|
|
271 |
|
|
// lock_me() returns false if it couldn't be locked, which could
|
272 |
|
|
// happen if the file descriptor is bad
|
273 |
|
|
|
274 |
|
|
cyg_bool
|
275 |
|
|
lock_me( void );
|
276 |
|
|
|
277 |
|
|
// trylock_me() returns false if it couldn't be locked, probably
|
278 |
|
|
// because it is already locked
|
279 |
|
|
cyg_bool
|
280 |
|
|
trylock_me( void );
|
281 |
|
|
|
282 |
|
|
void
|
283 |
|
|
unlock_me( void );
|
284 |
|
|
|
285 |
|
|
// get error status for this file
|
286 |
|
|
Cyg_ErrNo
|
287 |
|
|
get_error( void );
|
288 |
|
|
|
289 |
|
|
// set error status for this file.
|
290 |
|
|
void
|
291 |
|
|
set_error( Cyg_ErrNo errno_to_set );
|
292 |
|
|
|
293 |
|
|
// are we at EOF? true means we are, false means no
|
294 |
|
|
cyg_bool
|
295 |
|
|
get_eof_state( void );
|
296 |
|
|
|
297 |
|
|
// Set whether we are at EOF.
|
298 |
|
|
void
|
299 |
|
|
set_eof_state( cyg_bool eof_to_set );
|
300 |
|
|
|
301 |
|
|
// retrieve position
|
302 |
|
|
Cyg_ErrNo
|
303 |
|
|
get_position( fpos_t *pos );
|
304 |
|
|
|
305 |
|
|
// set absolute position. whence is SEEK_SET, SEEK_CUR, or SEEK_END
|
306 |
|
|
Cyg_ErrNo
|
307 |
|
|
set_position( fpos_t pos, int whence );
|
308 |
|
|
|
309 |
|
|
// Return my_device
|
310 |
|
|
cyg_stdio_handle_t get_dev() { return my_device; };
|
311 |
|
|
|
312 |
|
|
CYGDBG_DEFINE_CHECK_THIS
|
313 |
|
|
};
|
314 |
|
|
|
315 |
|
|
// INLINE FUNCTIONS
|
316 |
|
|
|
317 |
|
|
#include
|
318 |
|
|
|
319 |
|
|
#endif // CYGONCE_LIBC_STDIO_STREAM_HXX multiple inclusion protection
|
320 |
|
|
|
321 |
|
|
// EOF stream.hxx
|