include/Cnx/filesystem/File.h file

CnxFile provides various functions for working with type safe, uniquely owned files.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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 managing FILE*s
CnxResult(i32) cnx_file_write_bytes(CnxFile *restrict file
Writes the given bytes to the file.
CnxResult(CnxString) cnx_file_read(CnxFile *restrict file
Reads num_chars characters from file and returns them in a CnxString
CnxResult(usize) cnx_file_read_bytes(CnxFile *restrict file
Reads up to max_num_bytes bytes from file and writes them to bytes, 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
void cnx_file_free(void* file)
Frees 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 CnxFiles.
#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.

Function documentation

void cnx_file_free(void* file)

Frees the given file

Parameters
file - The CnxFile to free ingroup cnx_file

Frees the given file. Closes the file, freeing the allocated buffer and any operating system level resources associated with the file. This should not be called manually, instead prefer to use cnx_file_close, or declare your file as a CnxScopedFile so that is is closed automatically when it leaves scope