1 #ifndef CYGONCE_LIBC_STDIO_STREAM_HXX
2 #define CYGONCE_LIBC_STDIO_STREAM_HXX
3 //========================================================================
7 // Internal C library stdio stream interface definitions
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.
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.
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
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.
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.
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.
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####
45 // Author(s): jlarmour
50 // Usage: #include <cyg/libc/stdio/stream.hxx>
52 //####DESCRIPTIONEND####
54 //========================================================================
58 #include <pkgconf/libc_stdio.h> // Configuration header
62 #include <cyg/infra/cyg_type.h> // Common project-wide type definitions
63 #include <cyg/infra/cyg_ass.h> // Get assertion macros, as appropriate
64 #include <errno.h> // Cyg_ErrNo
65 #include <stdio.h> // fpos_t and IOBUF defines
66 #include <cyg/libc/stdio/io.hxx> // Physical IO support
67 #include <cyg/libc/stdio/streambuf.hxx> // Stdio stream file buffers
69 #ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
70 #include <pkgconf/kernel.h>
71 #include <cyg/kernel/mutex.hxx> // Cyg_Mutex
76 class Cyg_OutputStream
80 // Provide empty virtual destructor
81 virtual ~Cyg_OutputStream() {}
83 // The following two functions aren't made pure virtual not to bring
84 // dependency on C++ runtime to every application.
86 virtual Cyg_ErrNo write( const cyg_uint8 *buffer, cyg_ucount32 buffer_length,
87 cyg_ucount32 *bytes_written );
89 virtual Cyg_ErrNo get_error( void );
93 class Cyg_StdioStream;
95 cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
97 class Cyg_StdioStream: public Cyg_OutputStream
99 friend int setvbuf( FILE *, char *, int, size_t ) __THROW;
101 cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
105 // error status for this file
109 cyg_stdio_handle_t my_device;
111 #ifdef CYGFUN_LIBC_STDIO_ungetc
112 cyg_uint8 unread_char_buf;
115 #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
116 Cyg_StdioStreamBuffer io_buf; // read/write buffer
118 cyg_uint8 readbuf_char; // a one character emergency "buffer"
119 // only used when configured to not buffer
120 // (i.e. !CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO)
121 // or set at runtime to not buffer (i.e.
122 // buffering mode is _IONBF)
124 // some flags indicating the state of the file. Some of it is internal
125 // state, which should not be public. Use bitfields to save
126 // space, which means using "unsigned int" rather than cyg_uintX
128 unsigned int at_eof : 1; // Have we reached eof?
130 unsigned int opened_for_read : 1; // opened_for_read and
132 unsigned int opened_for_write : 1; // opened_for_write can
133 // be set simultaneously
135 unsigned int binary : 1; // opened for binary or
138 #ifdef CYGFUN_LIBC_STDIO_ungetc
139 unsigned int unread_char_buf_in_use : 1; // unget buf in use?
142 #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
143 unsigned int buffering : 1; // Is this file buffered?
145 unsigned int line_buffering : 1; // If so, is it line
146 // buffered? If it is
147 // buffered, but NOT line
148 // buffered, it must be
151 unsigned int last_buffer_op_was_read : 1; // did we last read from
152 // the buffer. If not we
155 unsigned int readbuf_char_in_use : 1; // is the above
156 // readbuf_char in use?
160 // current position for reading/writing
163 #ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
164 Cyg_Mutex stream_lock; // used for locking this stream
165 #endif // ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
167 #ifdef CYGDBG_USE_ASSERTS
168 // a value to check that this class is hopefully valid
169 cyg_ucount32 magic_validity_word;
173 // different modes when constructing (i.e. opening).
177 CYG_STREAM_READWRITE_NOCREATE,
178 CYG_STREAM_READWRITE_CREATE
183 // This constructs the stream - effectively opens the file. This is
184 // used for static initialisation, and actually calls construct below
186 // dev is a valid Cyg_Device_Table_t, although it does not have to
187 // be a member of the device table object itself
189 // open_mode is one of CYG_STREAM_READ, CYG_STREAM_WRITE,
190 // CYG_STREAM_READWRITE_NOCREATE or CYG_STREAM_READWRITE_CREATE
192 // append is true if the file position should be set at EOF on opening
194 // binary is true if this is a binary stream. Otherwise it is a text
195 // stream and character conversions (especially newline) may occur
197 // buffer_mode is one of _IONBF, _IOLBF, _IOFBF (from <stdio.h>)
198 // If buffer_mode is _IONBF, buffer_size should still be set to 0
199 // and buffer_addr to NULL. If buffering is not configured, none
200 // of buffer_mode, buffer_size and buffer_addr have any effect
202 // buffer_size is the size of buffer to use
204 // buffer_addr is the address of a user supplied buffer. By default
205 // (when NULL) a system one is provided.
207 // The "return code" is set by assignment to the error member of this
208 // stream - use the get_error() method to check
210 Cyg_StdioStream( cyg_stdio_handle_t dev, OpenMode open_mode,
211 cyg_bool append, cyg_bool binary, int buffer_mode,
212 cyg_ucount32 buffer_size=BUFSIZ,
213 cyg_uint8 *buffer_addr=NULL );
215 Cyg_StdioStream( OpenMode open_mode,
216 cyg_ucount32 buffer_size=BUFSIZ,
217 cyg_uint8 *buffer_addr=NULL );
220 void initialize( cyg_stdio_handle_t dev, OpenMode open_mode,
221 cyg_bool append, cyg_bool binary, int buffer_mode,
222 cyg_ucount32 buffer_size=BUFSIZ,
223 cyg_uint8 *buffer_addr=NULL );
233 // Close the stream. This should be called before the destructor,
234 // so we can see and report any errors produced.
237 // Refill read buffer from the stream - note this blocks until
238 // something arrives on the stream
240 refill_read_buffer( void );
243 // Read not more than buffer_length bytes from the read buffer into the
245 // The number of bytes put into the user buffer is written
248 read( cyg_uint8 *user_buffer, cyg_ucount32 buffer_length,
249 cyg_ucount32 *bytes_read );
252 // Read a single byte from the stream. Returns EAGAIN if no character
253 // available or EEOF if end of file (as well as setting the EOF state)
255 read_byte( cyg_uint8 *c );
257 // Read a single byte from the stream, but don't remove it. Returns
258 // EAGAIN if no character available or EEOF if end of file (as well
259 // as setting the EOF state)
261 peek_byte( cyg_uint8 *c );
264 // Return a byte into the stream - basically the same as ungetc()
266 unread_byte( cyg_uint8 c );
268 // the number of bytes available to read without needing to refill the
271 bytes_available_to_read( void );
274 write( const cyg_uint8 *buffer, cyg_ucount32 buffer_length,
275 cyg_ucount32 *bytes_written );
278 write_byte( cyg_uint8 c );
282 flush_output( void );
285 flush_output_unlocked( void );
287 // prevent multiple access in thread safe mode
289 // lock_me() returns false if it couldn't be locked, which could
290 // happen if the file descriptor is bad
295 // trylock_me() returns false if it couldn't be locked, probably
296 // because it is already locked
303 // get error status for this file
307 // set error status for this file.
309 set_error( Cyg_ErrNo errno_to_set );
311 // are we at EOF? true means we are, false means no
313 get_eof_state( void );
315 // Set whether we are at EOF.
317 set_eof_state( cyg_bool eof_to_set );
321 get_position( fpos_t *pos );
323 // set absolute position. whence is SEEK_SET, SEEK_CUR, or SEEK_END
325 set_position( fpos_t pos, int whence );
328 cyg_stdio_handle_t get_dev() { return my_device; };
330 CYGDBG_DEFINE_CHECK_THIS
335 #include <cyg/libc/stdio/stream.inl>
337 #endif // CYGONCE_LIBC_STDIO_STREAM_HXX multiple inclusion protection