[SLP-Homepage]    [Source Modules]    [Manual]    [Run]    [Examples]
 

File Input

This module defines a subclass in_file_c of the abstract input stream in_c. In case the super logic program comes from a file, it is read via an object of this class.

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.


Initialisation and Closing:
in_file_c:
in_file_c(str_t filename);
This constructor method opens the named file. In case something wents wrong, an error message is printed by calling the appropriate method from out_c. Afterwards, it is possible to check with is_open whether the file was successfully opened.
close:
bool_t close(void);
This method closes the current input stream. The method returns 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.)


Function for Getting Input:

read:
int read(char *buf, int size);
This function reads characters from the input file into the given buffer with starting address 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.


Diagnosis Functions:

is_open:
bool_t is_open(void);
This method returns 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).
filename:
str_t filename(void);
This method returns the name of the file associated with this input stream (e.g. for syntax error messages). The filename is copied during the construction of this 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.


Implementation:


Stefan Brass (sbrass@sis.pitt.edu), March 6, 2002.    [HTML 3.2 Checked]