Qore Programming Language Reference Manual
1.11.0
|
The ReadOnlyFile class allows Qore programs to read existing files. More...
Public Member Methods | |
int | close () |
Closes the ReadOnlyFile object. More... | |
constructor (string path, *string encoding) | |
Creates the ReadOnlyFile object. More... | |
copy () | |
Creates a new ReadOnlyFile object with the same character encoding specification as the original, otherwise no other information is copied. More... | |
destructor () | |
Closes the ReadOnlyFile if it is open and destroys the ReadOnlyFile object. More... | |
string | getEncoding () |
Returns the character encoding for the ReadOnlyFile. More... | |
*string | getFileName () |
returns the file path/name used to open the file if the file is open, otherwise NOTHING More... | |
int | getPos () |
Returns the current file position as an integer giving the offset in bytes from the beginning of the file (starting from zero) More... | |
*string | getchar () |
Reads one character from the file and returns it as a string; returns NOTHING if no data can be read from the file. More... | |
hash< StatInfo > | hstat () |
Returns a StatInfo hash about the file's status or throws an exception if any errors occur. More... | |
bool | isDataAvailable (timeout timeout_ms=0) |
Returns True if there is data available for reading from the file within the timeout period. More... | |
bool | isOpen () |
returns True if the File is open, False if not More... | |
bool | isTty () |
returns True if the File is connected to a terminal device, False if not More... | |
nothing | open (string path, *string encoding) |
Opens a file in a particular mode; throws an exception on failure. More... | |
*string | read (softint size, timeout timeout_ms=-1) |
Reads a certain number of characters from the ReadOnlyFile within an optional timeout period and returns a string of the data read or NOTHING if no data can be read. More... | |
*binary | readBinary (softint size, timeout timeout_ms=-1) |
Reads a certain number of bytes from the file within an optional timeout period and returns a binary object of the data read or NOTHING if no data can be read. More... | |
*string | readLine (bool incl_eol=True, *string eol) |
Reads until an EOL marker is found and returns the string read or NOTHING if no data can be read. More... | |
*int | readi1 () |
Reads a 1-byte signed integer from the file in binary format or NOTHING if no data can be read. More... | |
*int | readi2 () |
Reads a 2-byte (16 bit) signed integer from the file in binary big-endian format or NOTHING if no data can be read. More... | |
*int | readi2LSB () |
Reads a 2-byte (16 bit) signed integer from the file in binary little-endian format or NOTHING if no data can be read. More... | |
*int | readi4 () |
Reads a 4-byte (32 bit) signed integer from the file in binary big-endian format or NOTHING if no data can be read. More... | |
*int | readi4LSB () |
Reads a 4-byte (32 bit) signed integer from the file in binary little-endian format or NOTHING if no data can be read. More... | |
*int | readi8 () |
Reads an 8-byte (64 bit) signed integer from the file in binary big-endian format or NOTHING if no data can be read. More... | |
*int | readi8LSB () |
Reads an 8-byte (64 bit) signed integer from the file in binary little-endian format or NOTHING if no data can be read. More... | |
*int | readu1 () |
Reads a 1-byte unsigned integer from the ReadOnlyFile in binary format or NOTHING if no data can be read. More... | |
*int | readu2 () |
Reads a 2-byte (16 bit) unsigned integer from the ReadOnlyFile in binary big-endian format or NOTHING if no data can be read. More... | |
*int | readu2LSB () |
Reads a 2-byte (16 bit) unsigned integer from the file in binary little-endian format or NOTHING if no data can be read. More... | |
*int | readu4 () |
Reads a 4-byte (32 bit) unsigned integer from the file in big-endian format or NOTHING if no data can be read. More... | |
*int | readu4LSB () |
Reads a 4-byte (32 bit) unsigned integer from the file in binary little-endian format or NOTHING if no data can be read. More... | |
nothing | setEncoding (*string encoding) |
Sets the character encoding for the ReadOnlyFile; if called with no argument, the default encoding is set. More... | |
nothing | setEventQueue (Queue queue, auto arg, *bool with_data) |
Sets a Queue object to receive file events. More... | |
nothing | setEventQueue () |
Removes any Queue object from the ReadOnlyFile object so that file events are no longer added to the Queue. More... | |
int | setPos (int pos=0) |
Sets the current file position (in bytes from the beginning of the file) More... | |
list< auto > | stat () |
Returns a Stat List about the file's status or throws an exception if any errors occur. More... | |
hash< FilesystemInfo > | statvfs () |
Returns a FilesystemInfo hash about the file's filesystem status or throws an exception if any errors occur. More... | |
Static Public Member Methods | |
static hash< StatInfo > | hlstat (string path) |
Returns a StatInfo hash about the file's status (does not follow symbolic links) or throws an exception if any errors occur. More... | |
static hash< StatInfo > | hstat (string path) |
Returns a StatInfo hash about the file's status (follows symbolic links) or throws an exception if any errors occur. More... | |
static list< auto > | lstat (string path) |
Returns a Stat List about the given path's status (does not follow symbolic links) or throws an exception if any errors occur. More... | |
static binary | readBinaryFile (string path, int max_file_len=-1) |
returns the contents of a binary file as a binary object More... | |
static string | readTextFile (string path, *string encoding, int max_file_len=-1) |
returns the contents of a text file as a string optionally tagged with the given character encoding More... | |
static list< auto > | stat (string path) |
Returns a Stat List about the file's status (follows symbolic links) or throws an exception if any errors occur. More... | |
static hash< FilesystemInfo > | statvfs (string path) |
Returns a FilesystemInfo hash about filesystem status of the given path; throws an exception if any errors occur. More... | |
The ReadOnlyFile class allows Qore programs to read existing files.
ReadOnlyFile objects are opened with a specific character encoding, meaning that any string read from the file will be tagged with the file's character encoding. If no character encoding is specified, then the default character encoding is assumed for the file.
This class supports posting read events to a Queue. See I/O Event Handling for more information.
int Qore::ReadOnlyFile::close | ( | ) |
Closes the ReadOnlyFile object.
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
Creates the ReadOnlyFile object.
The constructor takes the file path and one optional argument that will set the default character encoding for the file (only affects reading string data) To open the file again with another file, call ReadOnlyFile::open(); the character encoding can also be set or changed by the ReadOnlyFile::open() or ReadOnlyFile::setEncoding() methods.
path | the path to open for reading |
encoding | The character encoding for the ReadOnlyFile. Strings data read from the file will be tagged with this character encoding; if this argument is not given then the ReadOnlyFile will receive the default encoding |
READONLYFILE-OPEN-ERROR | the given file cannot be opened for reading (arg will be assigned to the errno value) |
ILLEGAL-EXPRESSION | ReadOnlyFile::constructor() cannot be called with a TTY target when %no-terminal-io is set |
Qore::ReadOnlyFile::copy | ( | ) |
Creates a new ReadOnlyFile object with the same character encoding specification as the original, otherwise no other information is copied.
Qore::ReadOnlyFile::destructor | ( | ) |
Closes the ReadOnlyFile if it is open and destroys the ReadOnlyFile object.
Closes the ReadOnlyFile if it is open and destroys the ReadOnlyFile object
*string Qore::ReadOnlyFile::getchar | ( | ) |
Reads one character from the file and returns it as a string; returns NOTHING if no data can be read from the file.
Multi-byte characters are also read; use ReadOnlyFile::readu1() or ReadOnlyFile::readi1() to read a single byte from a file regardless of the ReadOnlyFile's character encoding.
FILE-READ-ERROR | file is not open |
FILE-GETCHAR-ERROR | invalid multi-byte character read or EOF received while reading a multi-byte character |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
string Qore::ReadOnlyFile::getEncoding | ( | ) |
Returns the character encoding for the ReadOnlyFile.
*string Qore::ReadOnlyFile::getFileName | ( | ) |
int Qore::ReadOnlyFile::getPos | ( | ) |
Returns the current file position as an integer giving the offset in bytes from the beginning of the file (starting from zero)
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
Returns a StatInfo hash about the file's status (does not follow symbolic links) or throws an exception if any errors occur.
Does not follow symbolic links, but rather returns filesystem information for symbolic links. If any errors occur, a FILE-HSTAT-ERROR
exception is thrown
path | the file to stat() |
FILE-HSTAT-ERROR | stat() call failed |
Returns a StatInfo hash about the file's status or throws an exception if any errors occur.
If any errors occur, a FILE-HSTAT-ERROR
exception is thrown
FILE-HSTAT-ERROR | stat() call failed or file not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
Returns a StatInfo hash about the file's status (follows symbolic links) or throws an exception if any errors occur.
This method will follow symbolic links and return information about the target. If any errors occur, a FILE-HSTAT-ERROR
exception is thrown
path | the file to stat() |
FILE-HSTAT-ERROR | stat() call failed |
bool Qore::ReadOnlyFile::isDataAvailable | ( | timeout | timeout_ms = 0 | ) |
Returns True if there is data available for reading from the file within the timeout period.
With a timeout of zero (the default if no timeout value is passed), this method can be used for non-blocking polling the file for data. Like all Qore functions and methods taking timeout values, a relative date/time value may be passed instead of an integer to make the timeout units clear (ex: 25ms
).
timeout_ms | An optional timeout in milliseconds (1/1000 second); relative date/time values can be given instead of an integer in milliseconds to make the source more readable; ex: 20s |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
bool Qore::ReadOnlyFile::isOpen | ( | ) |
bool Qore::ReadOnlyFile::isTty | ( | ) |
Returns a Stat List about the given path's status (does not follow symbolic links) or throws an exception if any errors occur.
Does not follow symbolic links, but rather returns filesystem information for symbolic links. If any errors occur, a FILE-LSTAT-ERROR
exception is thrown
path | the file to stat() |
FILE-LSTAT-ERROR | stat() call failed |
Opens a file in a particular mode; throws an exception on failure.
Opens the file in the mode given; if the ReadOnlyFile was previously open, it is closed first. Optionally the ReadOnlyFile's default character encoding can be specified.
Note that if no encoding is specified, the ReadOnlyFile will be tagged with the character encoding set in the ReadOnlyFile's constructor. Any string data written to the ReadOnlyFile will be converted to the ReadOnlyFile's encoding, and any string data read from the ReadOnlyFile will be automatically tagged with the ReadOnlyFile's encoding.
If an error occurs, a READONLYFILE-OPEN-ERROR
exception is thrown.
path | the path to the file |
encoding | the name of the character encoding for the ReadOnlyFile; if this argument is not given, the ReadOnlyFile will be tagged with the character encoding given in the constructor |
READONLYFILE-OPEN-ERROR | an error occurred opening the file |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*string Qore::ReadOnlyFile::read | ( | softint | size, |
timeout | timeout_ms = -1 |
||
) |
Reads a certain number of characters from the ReadOnlyFile within an optional timeout period and returns a string of the data read or NOTHING if no data can be read.
Reads a certain amount of string data from the ReadOnlyFile; the size argument is required. To read binary data, use the ReadOnlyFile::readBinary() method.
Note that the amount of data read from the file may be less than the size given, for example if the file does not contain enough data to fulfill the request. In this case, only the data available in the file is returned.
An optional timeout period in milliseconds can be passed as well (or a relative date/time value may be passed instead of an integer to make the timeout units clear; ex: 25ms
). If a timeout value is passed and the data cannot be read within the timeout period, then a READONLYFILE-READ-TIMEOUT
exception is thrown. If no timeout value is passed or a negative value is given, then the call will never timeout until either the requested amount of data has been read from the ReadOnlyFile or an end-of-file condition has been reached.
size | the number of characters to read of the file, -1 will read the entire file |
timeout_ms | a timeout period with a resolution of milliseconds (a relative date/time value; integer arguments will be assumed to be milliseconds); if not given or negative the call will never time out and will only return when the data has been read |
READONLYFILE-READ-PARAMETER-ERROR | zero size argument passed |
FILE-READ-ERROR | file is not open |
FILE-READ-TIMEOUT | timeout limit exceeded |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*binary Qore::ReadOnlyFile::readBinary | ( | softint | size, |
timeout | timeout_ms = -1 |
||
) |
Reads a certain number of bytes from the file within an optional timeout period and returns a binary object of the data read or NOTHING if no data can be read.
Reads a certain amount of string data from the file; the size argument is required. To read string data, use the ReadOnlyFile::read() method.
Note that the amount of data read from the file may be less than the size given, for example if the file does not contain enough data to fulfill the request. In this case, only the data available in the file is returned.
An optional timeout period in milliseconds can be passed as well (or a relative date/time value may be passed instead of an integer to make the timeout units clear; ex: 25ms
). If a timeout value is passed and the data cannot be read within the timeout period, then a FILE-READ-TIMEOUT
exception is thrown. If no timeout value is passed or a negative value is given, then the call will never timeout until either the requested amount of data has been read from the file or an end-of-file condition has been reached.
size | the number of bytes to read of the file, -1 will read the entire file |
timeout_ms | a timeout period with a resolution of milliseconds (a relative date/time value; integer arguments will be assumed to be milliseconds); if not given or negative the call will never time out and will only return when the data has been read |
READONLYFILE-READ-BINARY-PARAMETER-ERROR | zero size argument passed |
FILE-READ-ERROR | file is not open, or an I/O or other error occurred when reading the file |
FILE-READ-TIMEOUT | timeout limit exceeded |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
returns the contents of a binary file as a binary object
path | the path of the file to retrieve |
max_file_len | the maximum size of the file returned; if the data on the filesystem exceeds this value, the data returned will be truncated up to this byte length |
FILE-READ-ERROR | an I/O or other error occurred when reading the file |
*int Qore::ReadOnlyFile::readi1 | ( | ) |
Reads a 1-byte signed integer from the file in binary format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readi2 | ( | ) |
Reads a 2-byte (16 bit) signed integer from the file in binary big-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readi2LSB | ( | ) |
Reads a 2-byte (16 bit) signed integer from the file in binary little-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readi4 | ( | ) |
Reads a 4-byte (32 bit) signed integer from the file in binary big-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readi4LSB | ( | ) |
Reads a 4-byte (32 bit) signed integer from the file in binary little-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readi8 | ( | ) |
Reads an 8-byte (64 bit) signed integer from the file in binary big-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readi8LSB | ( | ) |
Reads an 8-byte (64 bit) signed integer from the file in binary little-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
Reads until an EOL marker is found and returns the string read or NOTHING if no data can be read.
Any string returned will be tagged with the ReadOnlyFile's character encoding.
incl_eol | if this argument is True, then the end of line characters read are included in the string returned |
eol | the end of line character(s); if not given, then the end of line character(s) are detected automatically, and can be either "\n" , "\r" , or "\r\n" (the last one is only automatically detected when not connected to a terminal device in order to keep the I/O from stalling); if this string is passed and has a different character encoding from the File's, then it will be converted to the File's character encoding |
READONLYFILE-READLINE-ERROR | the file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
|
static |
returns the contents of a text file as a string optionally tagged with the given character encoding
path | the path of the file to retrieve |
encoding | the name of the character encoding for the string returned; if this argument is not given, then the default character encoding is assumed |
max_file_len | the maximum size of the file returned; if the data on the filesystem exceeds this value, the data returned will be truncated up to this byte length |
FILE-READ-ERROR | an I/O or other error occurred when reading the file |
*int Qore::ReadOnlyFile::readu1 | ( | ) |
Reads a 1-byte unsigned integer from the ReadOnlyFile in binary format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readu2 | ( | ) |
Reads a 2-byte (16 bit) unsigned integer from the ReadOnlyFile in binary big-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readu2LSB | ( | ) |
Reads a 2-byte (16 bit) unsigned integer from the file in binary little-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readu4 | ( | ) |
Reads a 4-byte (32 bit) unsigned integer from the file in big-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
*int Qore::ReadOnlyFile::readu4LSB | ( | ) |
Reads a 4-byte (32 bit) unsigned integer from the file in binary little-endian format or NOTHING if no data can be read.
FILE-READ-ERROR | file is not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
nothing Qore::ReadOnlyFile::setEncoding | ( | *string | encoding | ) |
Sets the character encoding for the ReadOnlyFile; if called with no argument, the default encoding is set.
encoding | the character encoding for the file; if called with no argument, the default encoding is set |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
nothing Qore::ReadOnlyFile::setEventQueue | ( | ) |
Removes any Queue object from the ReadOnlyFile object so that file events are no longer added to the Queue.
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
nothing Qore::ReadOnlyFile::setEventQueue | ( | Queue | queue, |
auto | arg, | ||
*bool | with_data | ||
) |
Sets a Queue object to receive file events.
queue | the Queue object to receive file events; note that the Queue passed cannot have any maximum size set or a QUEUE-ERROR will be thrown |
arg | an argument that will be included in each event hash in the arg key |
with_data | if True, then the actual raw data transferred / received is also included in the events |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
QUEUE-ERROR | the Queue passed has a maximum size set |
Sets the current file position (in bytes from the beginning of the file)
pos | the position in the file as offset from position 0 |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
list<auto> Qore::ReadOnlyFile::stat | ( | ) |
Returns a Stat List about the file's status or throws an exception if any errors occur.
If any errors occur, a FILE-STAT-ERROR
exception is thrown.
FILE-STAT-ERROR | stat() call failed or file not open |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
Returns a Stat List about the file's status (follows symbolic links) or throws an exception if any errors occur.
This method will follow symbolic links and return information about the target. If any errors occur, a FILE-STAT-ERROR
exception is thrown
path | the file to stat() |
FILE-STAT-ERROR | stat() call failed |
hash<FilesystemInfo> Qore::ReadOnlyFile::statvfs | ( | ) |
Returns a FilesystemInfo hash about the file's filesystem status or throws an exception if any errors occur.
If any errors occur, a FILE-STATVFS-ERROR
exception is thrown
FILE-STATVFS-ERROR | statvfs() call failed or file is not open |
MISSING-FEATURE-ERROR | this method is not supported on this platform; check Option::HAVE_STATVFS before calling this method to avoid this exception |
ILLEGAL-EXPRESSION | this exception is only thrown if called with a system constant object (stdin, stdout, stderr) when no-terminal-io is set |
|
static |
Returns a FilesystemInfo hash about filesystem status of the given path; throws an exception if any errors occur.
If any errors occur, a FILE-STATVFS-ERROR
exception is thrown
path | the path to the filesystem to check |
FILE-STATVFS-ERROR | statvfs() call failed |
MISSING-FEATURE-ERROR | this method is not supported on this platform; check Option::HAVE_STATVFS before calling this method to avoid this exception |