An object of the class in_file_c
corresponds usually to an open input stream.
There can be multiple such input streams open at the same time
(e.g. for include files).
The file is opened via the constructor.
If the open should fail,
there is still an object left,
but the method is_open returns BOOL_FALSE
(indicating that one cannot read from this object).
Of course,
there is the standard library module "stream.h
".
I decided to implement my own input streams
for the following reasons:
Note that in CGI mode (when this program is executed via the web interface),
the files that can be opened must be restricted for security reasons.
E.g. it should not be possible to open /etc/passwd
.
Although this is no valid input for this program,
the error messages could at least give some user ID on the machine
that runs the web server.
Therefore,
when VER_CGI
is defined,
the CGI example directory (VER_CGI_EXDIR
)
is prepended to all filenames.
The filenames can contain only letters (lowercase and uppercase),
digits,
and the underscore
(so especially "../../../etc/passwd
" is not possible).
Its size is restricted to 8 characters.
In addition,
the extension ".txt
" is permitted.
The file names "core
" and "slp
"
are explicitly excluded.
in_file_c(str_t filename)
;bool_t close(void)
;BOOL_TRUE
if the close was successful.
Otherwise,
BOOL_FALSE
is returned
and an error message is printed.
The destructor of this class implicitly calls close
(if this was not done before).
However,
by using the implicit method,
one has no chance of checking the error status.
(Probably, for input files this is not very important,
but I don't like such limitations.)
int read(char *buf, int size)
;buf
and size size
.
It returns the number of characters actually read.
As long as this is greater than zero,
everything is ok.
If it is zero,
the end of file was reached.
If it is negative,
some error occurred.
The method prints an error message itself,
so the user doesn't has to worry about this.
Note that the input in the buffer is not null-terminated.
bool_t is_open(void)
;BOOL_TRUE
if this object is currently associated with an open input stream,
and BOOL_FALSE
otherwise
(i.e. the open during the construction failed
or close
was called in the meantime).
str_t filename(void)
;in_file_c
-object,
so it doesn't matter if the parameter filename
of the constructor is overwritten later.
However,
the length of the copied filename is limited
(currently to 128 characters).
So it might be that this method returns only a prefix
of the real filename.