1 // This may look like C code, but it is really -*- C++ -*-
3 Copyright (C) 1988 Free Software Foundation
4 written by Doug Lea (dl@rocky.oswego.edu)
6 This file is part of GNU CC.
8 GNU CC is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing. Refer to the GNU CC General Public
13 License for full details.
15 Everyone is granted permission to copy, modify and redistribute
16 GNU CC, but only under the conditions described in the
17 GNU CC General Public License. A copy of this license is
18 supposed to have been given to you along with GNU CC so you
19 can know your rights and responsibilities. It should be in a
20 file named COPYING. Among other things, the copyright notice
21 and this notice must be preserved on all copies.
43 FILE* fp
; // _iobuf file pointer
44 char* nm
; // file name (dynamically allocated)
45 char rw
; // 1 = read; 2 = write; 3 = readwrite
46 // bit 2 (4) means read/write into string
47 state_value state
; // _good/_eof/_fail/_bad
48 long stat
; // last read/write/... return value
51 void reinitialize(const char*);
52 char *readline (int chunk_number
, char terminator
);
56 File(const char* filename
, io_mode m
, access_mode a
);
57 File(const char* filename
, const char* m
);
58 File(int filedesc
, io_mode m
);
60 File(int sz
, char* buf
, io_mode m
);
64 // binding, rebinding, unbinding to physical files
66 File
& open(const char* filename
, io_mode m
, access_mode a
);
67 File
& open(const char* filename
, const char* m
);
68 File
& open(int filedesc
, io_mode m
);
69 File
& open(FILE* fileptr
);
74 // class variable access
78 void setname(const char* newname
);
87 // other status queries
98 void clear(state_value f
= _good
); // poorly named
99 void set(state_value f
); // set corresponding but
100 void unset(state_value f
); // clear corresponding bit
101 File
& failif(int cond
);
109 File
& putback(char c
); // a synonym for unget
113 File
& put(const char* s
);
114 File
& get (char* s
, int n
, char terminator
= '\n');
115 File
& getline(char* s
, int n
, char terminator
= '\n');
116 File
& gets (char **s
, char terminator
= '\n');
120 File
& read(void* x
, int sz
, int n
);
121 File
& write(void* x
, int sz
, int n
);
125 File
& form(const char* ...);
126 File
& scan(const char* ...);
134 File
& seek(long pos
, int seek_mode
=0); // default seek mode=absolute
139 File
& setbuf(int buffer_kind
); // legal vals: _IONBF, _IOFBF, _IOLBF
140 File
& setbuf(int size
, char* buf
);
147 extern void verbose_File_error_handler(const char*);
148 extern void quiet_File_error_handler(const char*);
149 extern void fatal_File_error_handler(const char*);
150 extern one_arg_error_handler_t File_error_handler
;
151 extern one_arg_error_handler_t
set_File_error_handler(one_arg_error_handler_t
);
153 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
157 inline int File::filedesc()
162 inline const char* File::name()
167 inline int File::iocount()
172 inline void File::clear(state_value flag
)
177 inline void File::set(state_value flag
)
179 state
= state_value(int(state
) | int(flag
));
182 inline void File::unset(state_value flag
)
184 state
= state_value(int(state
) & ~int(flag
));
187 inline int File::readable()
189 if (fp
!= 0) { if (feof(fp
)) set(_eof
); if (ferror(fp
)) set(_bad
);}
190 return (state
== _good
&& (rw
& 01));
193 inline int File::writable()
195 if (fp
!= 0 && ferror(fp
)) set(_bad
);
196 return ((int(state
) & (int(_fail
)|int(_bad
))) == 0 && (rw
& 02));
199 inline int File::is_open()
205 inline File
& File::raw()
207 return this->File::setbuf(_IONBF
);
211 inline File
& File::failif(int cond
)
213 if (cond
) set(_fail
); return *this;
216 inline File
& File::get(char& c
)
227 inline File
& File::put(char c
)
229 return failif (!writable() || putc(c
, fp
) == EOF
);
232 inline File
& File::unget(char c
)
234 return failif(!is_open() || !(rw
& 01) || ungetc(c
, fp
) == EOF
);
237 inline File
& File::putback(char c
)
239 return failif (!is_open() || !(rw
& 01) || ungetc(c
, fp
) == EOF
);
242 inline File
& File::read(void* x
, int sz
, int n
)
244 return failif (!readable() || (stat
= fread(x
, sz
, n
, fp
)) != n
);
247 inline File
& File::write(void* x
, int sz
, int n
)
249 return failif (!writable() || (stat
= fwrite(x
, sz
, n
, fp
)) != n
);
252 inline File
& File::flush()
254 return failif(!is_open() || fflush(fp
) == EOF
);
258 inline File
& File::seek(long pos
, int seek_mode
)
260 return failif (!is_open() || fseek(fp
, pos
, seek_mode
) < 0);
263 inline long File::tell()
265 failif (!is_open() || ((stat
= ftell(fp
)) < 0));
269 inline int File::rdstate()
271 check_state(); return state
; // check_state is necessary in rare but
272 } // possible circumstances
274 inline File::operator void*()
276 check_state(); return (int(state
) & (int(_bad
)|int(_fail
)))? 0 : this ;
279 inline int File::eof()
281 check_state(); return state
& _eof
;
284 inline int File::fail()
286 check_state(); return state
& _fail
;
289 inline int File::bad()
291 check_state(); return state
& _bad
;
294 inline int File::good()
296 check_state(); return rdstate() == _good
;