OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libc/] [stdio/] [current/] [include/] [stream.hxx] - Blame information for rev 865

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.