Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
This file is part of GNU CC.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the GNU CC General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License. A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies.
#include <sys/file.h> // needed to determine values of O_RDONLY...
#include <errno.h> // needed to get the psect magic loaded
void verbose_File_error_handler(const char* msg
)
void quiet_File_error_handler(const char*)
void fatal_File_error_handler(const char* msg
)
one_arg_error_handler_t File_error_handler
= verbose_File_error_handler
;
one_arg_error_handler_t
set_File_error_handler(one_arg_error_handler_t f
)
one_arg_error_handler_t old
= File_error_handler
;
fp
= 0; nm
= 0; stat
= 0; state
= _bad
; rw
= 0;
// reset class vars after open
// fp->_flag inspection is isolated here
void File::reinitialize(const char* filename
)
if (filename
!= 0) setname(filename
);
else if (fp
== stdin
) setname("(stdin)");
else if (fp
== stdout
) setname("(stdout)");
else if (fp
== stderr
) setname("(stderr)");
else if (rw
& 4) setname("(string)");
if (FP
->_flags
& (__SRD
|__SRW
))
if (FP
->_flags
& (__SWR
|__SRW
|__SAPP
))
if (FP
->_flag
& (_IOREAD
|_IORW
))
if (FP
->_flag
& (_IOWRT
|_IORW
|_IOAPPEND
))
File
& File::open(const char* filename
, io_mode m
, access_mode a
)
int open_arg
= open_cmd_arg(m
, a
);
int fd
= ::open(filename
, open_arg
, 0666);
fp
= fdopen(fd
, fopen_cmd_arg(m
));
File
& File::open(const char* filename
, const char* m
)
File
& File::open(FILE* fileptr
)
File
& File::open(int filedesc
, io_mode m
)
fp
= fdopen(filedesc
, fopen_cmd_arg(m
));
if (rw
& 4) // we own the iobuf, kill it
delete(*fp
); // kill the _iobuf
if (rw
& 4) // we own the iobuf, kill it
else if (fp
== stdin
|| fp
== stdout
|| fp
== stderr
)
return failif (nm
== 0 || unlink(nm
) != 0);
File::File(const char* filename
, io_mode m
, access_mode a
)
File::File(const char* filename
, const char* m
)
File::File(int filedesc
, io_mode m
)
File::File(FILE* fileptr
)
void File::setname(const char* newname
)
if (nm
== newname
) return;
nm
= new char[strlen(newname
) + 1];
File
& File::setbuf(int buffer_kind
)
setvbuf(fp
, 0, _IOFBF
, 0);
setvbuf(fp
, 0, _IOLBF
, 0);
File
& File::setbuf(int size
, char* buf
)
setvbuf(fp
, buf
, _IOFBF
, size
);
setbuffer(fp
, buf
, size
);
strcpy(error_string
, "\nerror in File ");
strcat(error_string
, nm
);
(*File_error_handler
)(error_string
);
//------------------------------------------------------------------
void File::check_state() // ensure fp & state agree about eof
File
& File::put(const char* s
)
return failif(!writable() || fputs(s
, fp
) == EOF
);
File
& File::get(char* s
, int n
, char terminator
)
stat
= 0; // This is not an error condition !
while (n
> 0 && (get(ch
)))
return failif((stat
!= 0) && ((stat
-= n
) == 0));
File
& File::getline(char* s
, int n
, char terminator
)
while (n
> 0 && (get(ch
)))
if ((*s
++ = ch
) == terminator
)
return failif((stat
!= 0) && ((stat
-= n
) == 0));
// This should probably be a page size....
/* Reads an arbitrarily long input line terminated by a user-specified
TERMINATOR. Super-nifty trick using recursion avoids unnecessary calls
char *File::readline (int chunk_number
, char terminator
)
register char *bufptr
= buf
;
while ((continu
= !!get(ch
)) && ch
!= terminator
) /* fill the current buffer */
if (bufptr
- buf
>= CHUNK_SIZE
) /* prepend remainder to ptr buffer */
if (ptr
= readline (chunk_number
+ 1, terminator
))
for (; bufptr
!= buf
; *--ptr
= *--bufptr
);
if (!continu
&& bufptr
== buf
)
int size
= (chunk_number
* CHUNK_SIZE
+ bufptr
- buf
) + 1;
if (ptr
= new char[stat
= size
])
for (*(ptr
+= (size
- 1)) = '\0'; bufptr
!= buf
; *--ptr
= *--bufptr
)
/* Reads an arbitrarily long input line terminated by TERMINATOR.
This routine allocates its own memory, so the user should
only supply the address of a (char *). */
File
& File::gets(char **s
, char terminator
)
return failif(!(*s
= readline (0, terminator
)));
File
& File::scan(const char* fmt
...)
stat
= _doscan(fp
, fmt
, args
);
stat
= vfscanf(fp
, fmt
, args
);
File
& File::form(const char* fmt
...)
stat
= _doprnt(fmt
, args
, fp
);
stat
= ferror(fp
) ? -1 : 0;
stat
= vfprintf(fp
, fmt
, args
);
int remove(const char *);