]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/language/c/libc/stdio/v2_0/include/stream.hxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / language / c / libc / stdio / v2_0 / include / stream.hxx
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 //####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 <cyg/libc/stdio/stream.hxx>
51 //
52 //####DESCRIPTIONEND####
53 //
54 //========================================================================
55
56 // CONFIGURATION
57
58 #include <pkgconf/libc_stdio.h>   // Configuration header
59
60 // INCLUDES
61
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
68
69 #ifdef CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS
70 #include <pkgconf/kernel.h>
71 #include <cyg/kernel/mutex.hxx>    // Cyg_Mutex
72 #endif
73
74 // TYPE DEFINITIONS
75
76 class Cyg_OutputStream
77 {
78 public:
79
80     // Provide empty virtual destructor
81     virtual ~Cyg_OutputStream() {}
82
83     // The following two functions aren't made pure virtual not to bring
84     // dependency on C++ runtime to every application.
85
86     virtual Cyg_ErrNo write( const cyg_uint8 *buffer, cyg_ucount32 buffer_length,
87         cyg_ucount32 *bytes_written );
88
89     virtual Cyg_ErrNo get_error( void );
90
91 };
92
93 class Cyg_StdioStream;
94 __externC Cyg_ErrNo
95 cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
96
97 class Cyg_StdioStream: public Cyg_OutputStream
98 {
99     friend int setvbuf( FILE *, char *, int, size_t ) __THROW;
100     friend Cyg_ErrNo
101     cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
102
103 private:
104
105     // error status for this file
106     Cyg_ErrNo error;
107
108
109     cyg_stdio_handle_t my_device;
110
111 #ifdef CYGFUN_LIBC_STDIO_ungetc
112     cyg_uint8 unread_char_buf;
113 #endif
114
115 #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
116     Cyg_StdioStreamBuffer io_buf; // read/write buffer
117 #endif
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)
123
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
127     struct {
128         unsigned int at_eof                  : 1; // Have we reached eof?
129
130         unsigned int opened_for_read         : 1; // opened_for_read and
131
132         unsigned int opened_for_write        : 1; // opened_for_write can
133                                                   // be set simultaneously
134
135         unsigned int binary                  : 1; // opened for binary or
136                                                   // text mode?
137         
138 #ifdef CYGFUN_LIBC_STDIO_ungetc
139         unsigned int unread_char_buf_in_use  : 1; // unget buf in use?
140 #endif
141
142 #ifdef CYGSEM_LIBC_STDIO_WANT_BUFFERED_IO
143         unsigned int buffering               : 1; // Is this file buffered?
144
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
149                                                   // fully buffered
150
151         unsigned int last_buffer_op_was_read : 1; // did we last read from
152                                                   // the buffer. If not we
153                                                   // must have written
154 #endif
155         unsigned int readbuf_char_in_use     : 1; // is the above
156                                                   // readbuf_char in use?
157         
158     } flags;
159
160     // current position for reading/writing
161     fpos_t position;
162
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
166
167 #ifdef CYGDBG_USE_ASSERTS
168     // a value to check that this class is hopefully valid
169     cyg_ucount32 magic_validity_word;
170 #endif
171
172 public:
173     // different modes when constructing (i.e. opening).
174     typedef enum {
175         CYG_STREAM_READ,
176         CYG_STREAM_WRITE,
177         CYG_STREAM_READWRITE_NOCREATE,
178         CYG_STREAM_READWRITE_CREATE
179     } OpenMode;
180
181     // CONSTRUCTORS
182
183     // This constructs the stream - effectively opens the file. This is
184     // used for static initialisation, and actually calls construct below
185     //
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
188     //
189     // open_mode is one of CYG_STREAM_READ, CYG_STREAM_WRITE,
190     // CYG_STREAM_READWRITE_NOCREATE or CYG_STREAM_READWRITE_CREATE
191     //
192     // append is true if the file position should be set at EOF on opening
193     //
194     // binary is true if this is a binary stream. Otherwise it is a text
195     // stream and character conversions (especially newline) may occur
196     //
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
201     //
202     // buffer_size is the size of buffer to use
203     //
204     // buffer_addr is the address of a user supplied buffer. By default
205     // (when NULL) a system one is provided.
206     //
207     // The "return code" is set by assignment to the error member of this
208     // stream - use the get_error() method to check
209
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 );
214
215     Cyg_StdioStream( OpenMode open_mode, 
216                      cyg_ucount32 buffer_size=BUFSIZ,
217                      cyg_uint8 *buffer_addr=NULL );
218
219 private:    
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 );
224 public:
225     
226     // DESTRUCTOR
227     virtual
228     ~Cyg_StdioStream();
229
230
231     // MEMBER FUNCTIONS
232
233     // Close the stream. This should be called before the destructor,
234     // so we can see and report any errors produced.
235     Cyg_ErrNo close();
236
237     // Refill read buffer from the stream - note this blocks until
238     // something arrives on the stream
239     Cyg_ErrNo
240     refill_read_buffer( void );
241
242
243     // Read not more than buffer_length bytes from the read buffer into the
244     // user buffer.
245     // The number of bytes put into the user buffer is written
246     // into *bytes_read
247     Cyg_ErrNo
248     read( cyg_uint8 *user_buffer, cyg_ucount32 buffer_length,
249           cyg_ucount32 *bytes_read );
250
251
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)
254     Cyg_ErrNo
255     read_byte( cyg_uint8 *c );
256
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)
260     Cyg_ErrNo
261     peek_byte( cyg_uint8 *c );
262
263
264     // Return a byte into the stream - basically the same as ungetc()
265     Cyg_ErrNo
266     unread_byte( cyg_uint8 c );
267
268     // the number of bytes available to read without needing to refill the
269     // buffer
270     cyg_ucount32
271     bytes_available_to_read( void );
272
273     virtual Cyg_ErrNo
274     write( const cyg_uint8 *buffer, cyg_ucount32 buffer_length,
275            cyg_ucount32 *bytes_written );
276
277     Cyg_ErrNo
278     write_byte( cyg_uint8 c );
279
280
281     Cyg_ErrNo
282     flush_output( void );
283
284     Cyg_ErrNo
285     flush_output_unlocked( void );
286
287     // prevent multiple access in thread safe mode
288
289     // lock_me() returns false if it couldn't be locked, which could
290     // happen if the file descriptor is bad
291
292     cyg_bool
293     lock_me( void );
294
295     // trylock_me() returns false if it couldn't be locked, probably
296     // because it is already locked
297     cyg_bool
298     trylock_me( void );
299
300     void
301     unlock_me( void );
302
303     // get error status for this file 
304     virtual Cyg_ErrNo
305     get_error( void );
306
307     // set error status for this file.
308     void
309     set_error( Cyg_ErrNo errno_to_set );
310
311     // are we at EOF? true means we are, false means no
312     cyg_bool
313     get_eof_state( void );
314
315     // Set whether we are at EOF.
316     void
317     set_eof_state( cyg_bool eof_to_set );
318
319     // retrieve position
320     Cyg_ErrNo
321     get_position( fpos_t *pos );
322
323     // set absolute position. whence is SEEK_SET, SEEK_CUR, or SEEK_END
324     Cyg_ErrNo
325     set_position( fpos_t pos, int whence );
326
327     // Return my_device
328     cyg_stdio_handle_t get_dev() { return my_device; };
329     
330     CYGDBG_DEFINE_CHECK_THIS
331 };
332
333 // INLINE FUNCTIONS
334
335 #include <cyg/libc/stdio/stream.inl>
336
337 #endif // CYGONCE_LIBC_STDIO_STREAM_HXX multiple inclusion protection
338
339 // EOF stream.hxx