module
CnxFileCnxFile
provides an abstraction for reading and writing to files that manages buffering, string formatting, and automatic closing of the file when it leaves scope.
Example:
#include <Cnx/filesystem/File.h> void example(void) { let_mut maybe_file = cnx_file_open("my_file.txt"); cnx_assert(cnx_result_is_ok(maybe_file), "Failed to open my_file.txt"); CnxScopedFile file = cnx_result_unwrap(maybe_file); let my_important_number = 42; let my_important_float 24.0F; cnx_file_println(&file, "number: {}, float: {}", my_important_number, my_important_float); let_mut maybe_file2 = cnx_file_open("my_other_file.txt"); cnx_assert(cnx_result_is_ok(maybe_file2), "Failed to open my_other_file.txt"); CnxScopedFile file2 = cnx_result_unwrap(maybe_file2); let_mut maybe_read = cnx_file_read_line(&file2); if(cnx_result_is_ok(maybe_read)) { CnxScopedString str = cnx_result_unwrap(maybe_read); // do something with the line read from `my_other_file.txt` } // files automatically closed when they leave scope because they were declared as // `CnxScopedFile` }
Classes
- struct CnxFileOptions
- Use to specify the access mode and behavior a file should be opened with.
- struct CnxFile
- Cnx type used to manage reading and writing to/from a file, the buffering associated with the file, and closing it when it's no longer used.
Enums
- enum CnxFileAccessMode { CnxFileRead = 1, CnxFileWrite = 2, CnxFileReadWrite = 3 }
- Use in a
CnxFileOptions
to specify the access mode a file should be opened with. - enum CnxFileOpenBehavior { CnxFileNone = 0, CnxFileAppend = 1, CnxFileTruncate = 2, CnxFileBinary = 4, CnxFileFailIfExists = 8 }
- Use in a
CnxFileOptions
to specify the behavior a file should be opened with. - enum CnxFileSeekOrigin { CnxFileSeekBegin = SEEK_SET, CnxFileSeekCurrent = SEEK_CUR, CnxFileSeekEnd = SEEK_END }
- Use to identify where a seek should begin from when seeking in a
CnxFile
Typedefs
- using FileBuffer = char[]
- Basic buffer type used to provide buffered for a
CnxFile
- using CnxFileAccessMode = enum CnxFileAccessMode
- Use in a
CnxFileOptions
to specify the access mode a file should be opened with. - using CnxFileOpenBehavior = enum CnxFileOpenBehavior
- Use in a
CnxFileOptions
to specify the behavior a file should be opened with. - using CnxFileOptions = struct CnxFileOptions
- Use to specify the access mode and behavior a file should be opened with.
- using CnxFile = struct CnxFile
- Cnx type used to manage reading and writing to/from a file, the buffering associated with the file, and closing it when it's no longer used.
- using CnxFileSeekOrigin = enum CnxFileSeekOrigin
- Use to identify where a seek should begin from when seeking in a
CnxFile
Functions
- static void file_deleter(FILE*restrict file, CnxAllocator allocator)
CnxDeleter
function for smart pointers managingFILE*
s- CnxResult(i32) cnx_file_write_bytes(CnxFile *restrict file
- Writes the given
bytes
to thefile
. - CnxResult(CnxString) cnx_file_read(CnxFile *restrict file
- Reads
num_chars
characters fromfile
and returns them in aCnxString
- CnxResult(usize) cnx_file_read_bytes(CnxFile *restrict file
- Reads up to
max_num_bytes
bytes fromfile
and writes them tobytes
, returning the number of bytes read. - CnxResult cnx_file_flush(CnxFile*restrict file)
- Flushes the given
file
. - CnxResult cnx_file_seek(CnxFile*restrict file, i64 offset, CnxFileSeekOrigin origin)
- Seeks to the given location in the
file
- void cnx_file_close(CnxFile*restrict file)
- Closes the given
file
Defines
- #define CnxScopedFile
- Declare a
CnxFile
with this to ensure that it's automatically closed when it leaves its containing scope. - #define CNX_FILE_DEFAULT_OPTIONS
- The default options used to open a
CnxFile
- #define CNX_FILE_DEFAULT_BUFFER_SIZE
- The default buffer size used for newly opened
CnxFile
s. - #define cnx_file_open(...)
- Opens the file at the given
path
, using the default allocator to allocate the memory used for buffering. - #define cnx_file_open_with_allocator(...)
- Opens the file at the given
path
, using the given allocator to allocate the memory used for buffering. - #define cnx_file_print(file, format_string, ...)
- Prints the string resulting from formatting
format_string
and the formatting arguments to the given file. - #define cnx_file_println(file, format_string, ...)
- Prints the string resulting from formatting
format_string
and the formatting arguments to the given file, followed by a newline. - #define cnx_file_print_with_allocator(file, allocator, format_string, ...)
- Prints the string resulting from formatting
format_string
and the formatting arguments to the given file, using the provided allocator for any memory allocations necessary to perform the string formatting. - #define cnx_file_println_with_allocator(file, allocator, format_string, ...)
- Prints the string resulting from formatting
format_string
and the formatting arguments to the given file, followed by a newline. Uses the provided allocator for any memory allocations necessary to perform the string formatting. - #define cnx_file_is_symlink(file)
- Returns whether the given
file
is actually symbolic link. - #define cnx_file_has_extension(file, extension)
- Returns whether the given
file
has the given file extension. - #define cnx_file_get_extension(file)
- Returns the file extension of the given
file
- #define cnx_file_get_name(file)
- Returns the file name of the given
file
, including the file extension, if it has one. - #define cnx_file_get_name_without_extension(file)
- Returns the file name of the given
file
, excluding the file extension, if it has one. - #define cnx_file_get_parent_directory(file)
- Returns the parent directory of the given
file
as an absolute path.
Enum documentation
enum CnxFileAccessMode
#include <include/Cnx/filesystem/File.h>
Use in a CnxFileOptions
to specify the access mode a file should be opened with.
enum CnxFileOpenBehavior
#include <include/Cnx/filesystem/File.h>
Use in a CnxFileOptions
to specify the behavior a file should be opened with.
enum CnxFileSeekOrigin
#include <include/Cnx/filesystem/File.h>
Use to identify where a seek should begin from when seeking in a CnxFile
Enumerators | |
---|---|
CnxFileSeekBegin |
Indicates a file seek should originate from the beginning of the file. |
CnxFileSeekCurrent |
Indicates a file seek should originate from the current position in the file. |
CnxFileSeekEnd |
Indicates a file seek should originate from the end of the file. |
Typedef documentation
typedef charFileBuffer[]
#include <include/Cnx/filesystem/File.h>
Basic buffer type used to provide buffered for a CnxFile
typedef enum CnxFileAccessMode CnxFileAccessMode
#include <include/Cnx/filesystem/File.h>
Use in a CnxFileOptions
to specify the access mode a file should be opened with.
typedef enum CnxFileOpenBehavior CnxFileOpenBehavior
#include <include/Cnx/filesystem/File.h>
Use in a CnxFileOptions
to specify the behavior a file should be opened with.
typedef struct CnxFileOptions CnxFileOptions
#include <include/Cnx/filesystem/File.h>
Use to specify the access mode and behavior a file should be opened with.
typedef struct CnxFile CnxFile
#include <include/Cnx/filesystem/File.h>
Cnx type used to manage reading and writing to/from a file, the buffering associated with the file, and closing it when it's no longer used.
Example:
#include <Cnx/filesystem/File.h> void example(void) { let_mut maybe_file = cnx_file_open("my_file.txt"); cnx_assert(cnx_result_is_ok(maybe_file), "Failed to open my_file.txt"); CnxScopedFile file = cnx_result_unwrap(maybe_file); let my_important_number = 42; let my_important_float 24.0F; cnx_file_println(&file, "number: {}, float: {}", my_important_number, my_important_float); let_mut maybe_file2 = cnx_file_open("my_other_file.txt"); cnx_assert(cnx_result_is_ok(maybe_file2), "Failed to open my_other_file.txt"); CnxScopedFile file2 = cnx_result_unwrap(maybe_file2); let_mut maybe_read = cnx_file_read_line(&file2); if(cnx_result_is_ok(maybe_read)) { CnxScopedString str = cnx_result_unwrap(maybe_read); // do something with the line read from `my_other_file.txt` } // files automatically closed when they leave scope because they were declared as // `CnxScopedFile` }
typedef enum CnxFileSeekOrigin CnxFileSeekOrigin
#include <include/Cnx/filesystem/File.h>
Use to identify where a seek should begin from when seeking in a CnxFile
Function documentation
static void file_deleter(FILE*restrict file,
CnxAllocator allocator)
#include <include/Cnx/filesystem/File.h>
CnxDeleter
function for smart pointers managing FILE*
s
CnxResult(i32) cnx_file_write_bytes(CnxFile *restrict file
#include <include/Cnx/filesystem/File.h>
Writes the given bytes
to the file
.
Returns | CnxResult(i32) - The number of bytes written on success, otherwise an error |
---|
Attempts to write bytes in the given byte array, bytes
to the file. Writing will fail if file
was not opened with write access permissions or for any reason fwrite
or similar libc functions may fail
CnxResult(CnxString) cnx_file_read(CnxFile *restrict file
#include <include/Cnx/filesystem/File.h>
Reads num_chars
characters from file
and returns them in a CnxString
Returns | CnxResult(CnxString) - The string containing the characters read from the file on success, otherwise an error |
---|
Reads a line of text from file
and returns it in a CnxString
Reads num_chars
characters from file
and returns them in a CnxString
allocated with the given memory allocator.
Attempts to read num_chars
characters from file
, returning the result in a CnxString
. May read less than num_chars
characters if EOF
is reached. Reading will fail if the file
was not opened with read access permissions or for any reason fread
or similar libc functions may fail.
Attempts to read num_chars
characters from file
, returning the result in a CnxString
allocated with the given memory allocator. May read less than num_chars
characters if EOF
is reached. Reading will fail if the file
was not opened with read access permissions or for any reason fread
or similar libc functions may fail.
Attempts to read a line of text from file
, returning the result in a CnxString
. May read less than a line if EOF
is reached before a newline is encountered. Reading will fail if the file
was not opened with read access permissions or for any reason fread
or similar libc functions may fail.
Attempts to read a line of text from file
, returning the result in a CnxString
. May read less than a line if EOF
is reached before a newline is encountered. Reading will fail if the file
was not opened with read access permissions or for any reason fread
or similar libc functions may fail.
CnxResult(usize) cnx_file_read_bytes(CnxFile *restrict file
#include <include/Cnx/filesystem/File.h>
Reads up to max_num_bytes
bytes from file
and writes them to bytes
, returning the number of bytes read.
Returns | CnxResult(usize) - The number of bytes read from the file and written to bytes on success, otherwise an error |
---|
Attempts to read up to max_num_bytes
bytes from file
, and writes them to bytes
. May read less than max_num_bytes
bytes if EOF
is reached. Returns the number of bytes read when successful. Reading will fail if the file
was not opened with read access permissions or for any reason fread
or similar libc functions may fail.
CnxResult cnx_file_flush(CnxFile*restrict file)
#include <include/Cnx/filesystem/File.h>
Flushes the given file
.
Parameters | |
---|---|
file | - The CnxFile to flush |
Returns | CnxResult - Ok() on success, otherwise an error |
Attempts to flush the given file
. Flushing may fail for any reason fflush
from libc may fail.
CnxResult cnx_file_seek(CnxFile*restrict file,
i64 offset,
CnxFileSeekOrigin origin)
#include <include/Cnx/filesystem/File.h>
Seeks to the given location in the file
Parameters | |
---|---|
file | - The CnxFile to seek in |
offset | - The offset from origin to seek to. After successful seeking, the file will be at the effective location of origin + offset |
origin | - The location in the file the seek should originate from |
Returns | CnxResult - Ok() on success, otherwise an error |
Attempts to seek to the location indicated by origin
and offset
in the file
. May fail for any reason fseek
from libc may fail.
void cnx_file_close(CnxFile*restrict file)
#include <include/Cnx/filesystem/File.h>
Closes the given file
Parameters | |
---|---|
file | - The CnxFile to close |
Closes the given file
, freeing the allocated buffer and any operating system level resources associated with the file.
Define documentation
#define CnxScopedFile
#include <include/Cnx/filesystem/File.h>
Declare a CnxFile
with this to ensure that it's automatically closed when it leaves its containing scope.
Example:
#include <Cnx/filesystem/File.h> void example(void) { let_mut maybe_file = cnx_file_open("my_file.txt"); cnx_assert(cnx_result_is_ok(maybe_file), "Failed to open my_file.txt"); CnxScopedFile file = cnx_result_unwrap(maybe_file); let my_important_number = 42; let my_important_float 24.0F; cnx_file_println(&file, "number: {}, float: {}", my_important_number, my_important_float); let_mut maybe_file2 = cnx_file_open("my_other_file.txt"); cnx_assert(cnx_result_is_ok(maybe_file2), "Failed to open my_other_file.txt"); CnxScopedFile file2 = cnx_result_unwrap(maybe_file2); let_mut maybe_read = cnx_file_read_line(&file2); if(cnx_result_is_ok(maybe_read)) { CnxScopedString str = cnx_result_unwrap(maybe_read); // do something with the line read from `my_other_file.txt` } // files automatically closed when they leave scope because they were declared as // `CnxScopedFile` }
#define CNX_FILE_DEFAULT_OPTIONS
#include <include/Cnx/filesystem/File.h>
The default options used to open a CnxFile
The default behavior for opening a file with Cnx is to open in read/write mode, truncate contents on opening, and create a new file if one didn't already exist (equivalent to "w+")
#define CNX_FILE_DEFAULT_BUFFER_SIZE
#include <include/Cnx/filesystem/File.h>
The default buffer size used for newly opened CnxFile
s.
This is the default buffer size used for newly opened CnxFile
s. It can be overridden by explicitly passing buffer_size
to cnx_file_open
.
On Windows or MacOS, this is 8K. On other platforms this will be equivalent to that platform's BUFSIZ
#define cnx_file_open(...)
#include <include/Cnx/filesystem/File.h>
Opens the file at the given path
, using the default allocator to allocate the memory used for buffering.
Parameters | |
---|---|
... |
|
Returns | CnxResult(CnxFile) - a CnxFile at path if successful, otherwise an error. |
Attempts to open the file at the given path
. path
must be a valild path for a file. By default, this uses CNX_FILE_DEFAULT_OPTIONS
for options
, the CnxFileOptions
indicating file opening permissions and behavior, and CNX_FILE_DEFAULT_BUFFER_SIZE
for buffer_size
, the buffer size to allocate and use for buffering in the file. Both of these can be passed explicit values to override this behavior, however.
options
must be a valid combination of CnxFileAccessMode
and CnxFileOpenBehavior
. For example, CnxFileRead
combined with CnxFileTruncate
would be an invalid pairing, and would result in this returning an error. Valid combinations are those equivalent to a valid mode
for fopen
.
#define cnx_file_open_with_allocator(...)
#include <include/Cnx/filesystem/File.h>
Opens the file at the given path
, using the given allocator to allocate the memory used for buffering.
Parameters | |
---|---|
... |
|
Returns | CnxResult(CnxFile) - a CnxFile at path if successful, otherwise an error. |
Attempts to open the file at the given path
. path
must be a valild path for a file. By default, this uses CNX_FILE_DEFAULT_OPTIONS
for options
, the CnxFileOptions
indicating file opening permissions and behavior, and CNX_FILE_DEFAULT_BUFFER_SIZE
for buffer_size
, the buffer size to allocate and use for buffering in the file. Both of these can be passed explicit values to override this behavior, however.
options
must be a valid combination of CnxFileAccessMode
and CnxFileOpenBehavior
. For example, CnxFileRead
combined with CnxFileTruncate
would be an invalid pairing, and would result in this returning an error. Valid combinations are those equivalent to a valid mode
for fopen
.
#define cnx_file_print(file,
format_string,
...)
#include <include/Cnx/filesystem/File.h>
Prints the string resulting from formatting format_string
and the formatting arguments to the given file.
Parameters | |
---|---|
file | - The CnxFile to print the string to |
format_string | - The cstring containing the text along with how to format the formatting arguments into the text. Uses the CnxFormat formatting syntax |
... | - The formatting arguments to create formatted strings of to insert in format_string |
Returns | CnxResult(i32) - The number of characters written on success, otherwise an error |
Attempts to write the string resulting from formatting to the file. Writing will fail if file
was not opened with write access permissions or for any reason fwrite
or similar libc functions may fail
#define cnx_file_println(file,
format_string,
...)
#include <include/Cnx/filesystem/File.h>
Prints the string resulting from formatting format_string
and the formatting arguments to the given file, followed by a newline.
Parameters | |
---|---|
file | - The CnxFile to print the string to |
format_string | - The cstring containing the text along with how to format the formatting arguments into the text. Uses the CnxFormat formatting syntax |
... | - The formatting arguments to create formatted strings of to insert in format_string |
Returns | CnxResult(i32) - The number of characters written on success, otherwise an error |
Attempts to write the string resulting from formatting, followed by a newline, to the file. Writing will fail if file
was not opened with write access permissions or for any reason fwrite
or similar libc functions may fail
#define cnx_file_print_with_allocator(file,
allocator,
format_string,
...)
#include <include/Cnx/filesystem/File.h>
Prints the string resulting from formatting format_string
and the formatting arguments to the given file, using the provided allocator for any memory allocations necessary to perform the string formatting.
Parameters | |
---|---|
file | - The CnxFile to print the string to |
allocator | - The CnxAllocator to use for memory allocations, if necessary |
format_string | - The cstring containing the text along with how to format the formatting arguments into the text. Uses the CnxFormat formatting syntax |
... | - The formatting arguments to create formatted strings of to insert in format_string |
Returns | CnxResult(i32) - The number of characters written on success, otherwise an error |
Attempts to write the string resulting from formatting to the file. Writing will fail if file
was not opened with write access permissions or for any reason fwrite
or similar libc functions may fail
#define cnx_file_println_with_allocator(file,
allocator,
format_string,
...)
#include <include/Cnx/filesystem/File.h>
Prints the string resulting from formatting format_string
and the formatting arguments to the given file, followed by a newline. Uses the provided allocator for any memory allocations necessary to perform the string formatting.
Parameters | |
---|---|
file | - The CnxFile to print the string to |
allocator | - The CnxAllocator to use for memory allocations, if necessary |
format_string | - The cstring containing the text along with how to format the formatting arguments into the text. Uses the CnxFormat formatting syntax |
... | - The formatting arguments to create formatted strings of to insert in format_string |
Returns | CnxResult(i32) - The number of characters written on success, otherwise an error |
Attempts to write the string resulting from formatting, followed by a newline, to the file. Writing will fail if file
was not opened with write access permissions or for any reason fwrite
or similar libc functions may fail
#define cnx_file_is_symlink(file)
#include <include/Cnx/filesystem/File.h>
Returns whether the given file
is actually symbolic link.
Parameters | |
---|---|
file | - The pointer to the CnxFile to test |
Returns | bool - Whether file is a symlink |
#define cnx_file_has_extension(file,
extension)
#include <include/Cnx/filesystem/File.h>
Returns whether the given file
has the given file extension.
Parameters | |
---|---|
file | - The pointer to the CnxFile to test |
extension | - The extension to test for. This can be a pointer to any string or string-like type (i.e. it can be CnxString* , CnxStringView* , cstring , or a string literal) |
Returns | bool - Whether file is a symlink |
#define cnx_file_get_extension(file)
#include <include/Cnx/filesystem/File.h>
Returns the file extension of the given file
Parameters | |
---|---|
file | - The pointer to the CnxFile to get the file extension of |
Returns | CnxOption(CnxString) - If file is a file with a file extension, the file extension of file . Otherwise, None(CnxString) . |
Attempts to get the file extension of the given file
. If it does not have a file extension, then this will return None()
.
#define cnx_file_get_name(file)
#include <include/Cnx/filesystem/File.h>
Returns the file name of the given file
, including the file extension, if it has one.
Parameters | |
---|---|
file | - The pointer to the CnxFile to get the file name of |
Returns | CnxString - the file name of file . |
#define cnx_file_get_name_without_extension(file)
#include <include/Cnx/filesystem/File.h>
Returns the file name of the given file
, excluding the file extension, if it has one.
Parameters | |
---|---|
file | - The pointer to the CnxFile to get the file name of |
Returns | CnxString - the file name of file . |
#define cnx_file_get_parent_directory(file)
#include <include/Cnx/filesystem/File.h>
Returns the parent directory of the given file
as an absolute path.
Parameters | |
---|---|
file | - The pointer to the CnxFile to get the parent directory of |
Returns | CnxPath - the parent directory of file as an absolute path. |