File Functions

Summary
Use this function to append a line of text to a file using a ‘log’ format.
Recursively create a directory.
Rename (move) a file within the same filesystem.
This will erase a file or directory recursively.
Open a file and return its handle.
Close a previously opened file.
Check the status of the EOF flag on the file handle.
Read a signed 4 byte integer from the file.
Read an unsigned 2 byte integer from the file.
Read an unsigned byte from the file.
Read null-terminated string from the file.
Read a newline-terminated string from the file.
Write a signed 4 byte integer to the file.
Write an unsigned 2 byte integer to the file.
Write an unsigned byte to the file.
Write a null-terminated string to the file.
Write a newline terminated string to the file.
Read a block of data from a file into an allocated block of memory.
Write a block of data to a file from an allocated block of memory.
Determine the length of a file.
Quickly scan the file to find out the number of lines.
Query the current position of writing/reading in a file.
Position the current file pointer in an open file.
Position the file at the start of a given line.
Generate a temporary file name.

append_log

Use this function to append a line of text to a file using a ‘log’ format.  It will put a timestamp on the front of the line automatically.  This is an atomic write (full open, append, and close operations done).

Params

filename$file to append to
str$text to append

Returns

Nothing

make_path

Recursively create a directory.  This will create a directory (and every parent directory it needs to) with the default permissions.  If the directory exists, nothing is done.  If there is a problem creating the directory (for instance a regular already exists with the same name), it will quietly fail.

Params

path$full path to create (e.g.  “foo/bar/baz”)

Returns

Nothing

frename

Rename (move) a file within the same filesystem.  This is the equivalent of the ‘mv’ shell command except that it does not cross filesystem boundaries.  Use it to atomically change the name of a file without dropping to a shell.  Obviously permission checks and everything may prevent this operation, so be sure to check the return code.

Params

curname$name of the existing file to rename
newname$the new filename

Returns

This returns true if successfully renamed, or 0 otherwise.

fremove

This will erase a file or directory recursively.  It erases the link to a file on the disk, if this is the lask link to that file and it’s not currently open the file will be deleted.  This is the equivalent of an ‘rm -rf’ shell command, so USE WITH CAUTION!

Note, this operation can have different levels of success.  If recursively deleting files, some may be deleted fine, but others may have permission problems preventing it.  It will try to delete all it can, and will return false if ANY errors are encountered even though some files may have been deleted fine.

Params

path$path to the file to delete (e.g.  ‘foo/bar/blah.txt’)

Returns

True if sucessfully deleted, or false if there were errors.

fopen

Open a file and return its handle.  The file can be opened in one of several modes, as indicated by the second parameter, which can have the values in the following table.

rOpen for reading only, position at front.
wOpen file for writing, create if necessary, position at front.
aOpen file for writing, create if necessary, position at end.

The file handle returned (if successful) is used in any further file operations (including closing the file).  Any files that are open upon <reloading> will automatically be closed (and any remaining file handles made invalid).

Params

filename$file to open
mode$mode to open the file

Returns

An integer file handle of the newly opened file, or 0 if unable to do so.

fclose

Close a previously opened file.  The file must’ve been already opened using fopen.

Params

fhfile handle

Returns

Nothing

feof

Check the status of the EOF flag on the file handle.  This flag is set after a read attempt has reached the end of the file.  The proper way to check this is shown in the following.

f = fopen("foo.txt", "r");
while( !feof(f) )do
{
str$ = fget_line$(f);
...
}

In the example above, feof() will return true only after an attempt to fetch a line past the last.  If there are some characters, it will return them, but if not, it will return the empty string.

Params

fhfile handle

Returns

True if the EOF flag has been set on the file handle.

fget_data

Read a signed 4 byte integer from the file.

Params

fhfile handle

Returns

The read value.

fget_variable

Read an unsigned 2 byte integer from the file.

Params

fhfile handle

Returns

The read value.

fget_byte

Read an unsigned byte from the file.

Params

fhfile handle

Returns

The read value.

fget_string$

Read null-terminated string from the file.

Params

fhfile handle

Returns

The read value.

fget_line$

Read a newline-terminated string from the file.

Params

fhfile handle

Returns

The read value.

fput_data

Write a signed 4 byte integer to the file.

Params

fhfile handle
valuethe value to write

Returns

Whether it was written successfully.

fput_variable

Write an unsigned 2 byte integer to the file.

Params

fhfile handle
valuethe value to write

Returns

Whether it was written successfully.

fput_byte

Write an unsigned byte to the file.

Params

fhfile handle
valuethe value to write

Returns

Whether it was written successfully.

fput_string

Write a null-terminated string to the file.

Params

fhfile handle
value$the value to write

Returns

Whether it was written successfully.

fput_line

Write a newline terminated string to the file.

Params

fhfile handle
value$the value to write

Returns

Whether it was written successfully.

fget_memory

Read a block of data from a file into an allocated block of memory.  Note that this can be used to read large blocks at a time (very efficiently).  But it does not guarantee that it read as many bytes as requested.  Always check the return value to see how many were read.  A return value of 0 actually indicates that the EOF has been reached.

Params

fhfile handle
mhmemory handle (see alloc)
offsetoffset into mh to put the data that is read
lennumber of bytes to read

Returns

The number of bytes actually read (or -1 if there was an error).

fput_memory

Write a block of data to a file from an allocated block of memory.  This will efficiently write large chunks of data from memory into a file.

Params

fhfile handle
mhmemory handle (see alloc)
offsetoffset into mh of data to be written
lennumber of bytes to read

Returns

True if the write operation succeeds.

file_length

Determine the length of a file.

Params

fhfile handle

Returns

An integer indicating the number of bytes in the file.

file_lines

Quickly scan the file to find out the number of lines.  The current file position is unchanged after this operation.  Note that this can be a very costly operation on large files.

Params

fhfile handle

Returns

An integer indicating the number of lines in the file.

fpos

Query the current position of writing/reading in a file.  The value returned from this is actually the position after the last byte in the file.  So if you’ve written 6 bytes, this function returns 7 (assuming you haven’t repositioned the file using fseek).

Params

fhfile handle

Returns

An integer indicating the number of bytes from the beginning of the file.

fseek

Position the current file pointer in an open file.  Use this routine to advance forward or backward in a file.  This type of operation should be used sparingly however, as it can be quite a lengthy operation on large files.

You can seek using three different modes, and each determines where in the file you’re seeking ‘from’. anything else has undefined behavior.

0Seek from beginning of file
1Seek from current position
2Seek from end of file

Params

fhfile handle
offsetan offset (who’s meaning is dependent on whence)
whencewhat reference point to use when calculating an offset in the file

Returns

Whether the file was successfully repositioned.

fseek_line

Position the file at the start of a given line.  This will ready the file to read the Nth line in the file, however at quite a large cost.  Use this function sparingly.  The line numbering is 1 based (the first line in the file is line 1).

Params

fhfile handle
lineline to seek to

Returns

Whether the file was successfully repositioned.

ftempname$

Generate a temporary file name.  This returns a filename of a file that did not exist at the time this routine was called.  Generally in the form /tmp/rapture.XXXXXXXX.  The generation of suffixes is quite complicated and not likely guessable (because of the following problem).

This routine should be used with caution.  It’s susceptible to certain forms of malicious attack.  Namely, the span of time between generating a filename and actually opening it is vulnerable to such things as having a symbolic link placed there before the file is opened.  In this case, a malicious attacker could point your temporary file name at a sensitive file such as /etc/passwd and if permissions are set to allow it, you could begin overwriting or reading this file.  This will likely be fixed in future versions of Rapture, but as this form of attack is unlikely, it is not a priority.

Params

none

Returns

A string containing the name of a file that did not exist at the time of calling.

Open a file and return its handle.
Allocate a block of memory.
Position the current file pointer in an open file.