|
CX Framework
Cross-platform C utility framework
|
Typedefs | |
| typedef struct FSFile | FSFile |
| typedef enum FSSeekTypeEnum | FSSeekType |
Enumerations | |
| enum | FSOpenFlags { FS_Read = 1 , FS_Write = 2 , FS_Create = 4 , FS_Truncate = 8 , FS_Lock = 16 , FS_Overwrite = (FS_Write | FS_Create | FS_Truncate) } |
| enum | FSSeekTypeEnum { FS_Set = 0x00010000 , FS_Cur = 0x00020000 , FS_End = 0x00030000 } |
Functions | |
| FSFile * | fsOpen (strref path, flags_t flags) |
| bool | fsClose (FSFile *file) |
| bool | fsRead (FSFile *file, void *buf, size_t sz, size_t *bytesread) |
| bool | fsWrite (FSFile *file, void *buf, size_t sz, size_t *byteswritten) |
| int64 | fsTell (FSFile *file) |
| int64 | fsSeek (FSFile *file, int64 off, FSSeekType seektype) |
| bool | fsFlush (FSFile *file) |
Provides direct access to the operating system's file I/O APIs for synchronous read/write operations. This is a thin wrapper around platform-native file handles with minimal buffering.
Key characteristics:
For higher-level filesystem operations (directories, metadata), see fs.h. For virtual filesystem abstraction, see vfs.h.
| typedef enum FSSeekTypeEnum FSSeekType |
File seek origin
Specifies the reference point for fsSeek operations.
| enum FSOpenFlags |
File open flags
Flags controlling how a file is opened. Combine with bitwise OR. The FS_Overwrite flag is a convenience combination of common flags.
| enum FSSeekTypeEnum |
| bool fsClose | ( | FSFile * | file | ) |
Closes a file and releases resources
Flushes any pending writes, closes the underlying OS handle, and frees the FSFile structure. The file pointer becomes invalid after this call. Always call this when done with a file, even if errors occurred during I/O.
| file | File handle to close (must be valid) |
| bool fsFlush | ( | FSFile * | file | ) |
Flushes buffered writes to disk
Forces any buffered write data to be physically written to the storage device. This ensures data durability but may be slow. The OS may buffer writes for performance; this function ensures they reach the disk.
| file | Open file handle |
| FSFile * fsOpen | ( | strref | path, |
| flags_t | flags | ||
| ) |
Opens a file for I/O operations
Creates a file handle for reading, writing, or both. The file must be closed with fsClose() when done to release resources and flush buffers.
Common flag combinations:
FS_Read - Open existing file for readingFS_Write - Open existing file for writingFS_Read | FS_Write - Open existing file for read/writeFS_Write | FS_Create - Open or create file for writingFS_Overwrite - Create new or truncate existing fileFS_Read | FS_Write | FS_Create - Open or create for read/writeThe FS_Lock flag requests exclusive write access. On Windows, other processes can still read the file but cannot write. On Unix, this is advisory locking.
| path | Path to the file to open (can be relative or absolute) |
| flags | Combination of FSOpenFlags specifying open mode |
Example:
| bool fsRead | ( | FSFile * | file, |
| void * | buf, | ||
| size_t | sz, | ||
| size_t * | bytesread | ||
| ) |
Reads data from a file
Reads up to 'sz' bytes from the current file position into the buffer. The file position is advanced by the number of bytes actually read. Reading less than requested is not an error - it indicates end-of-file or incomplete data available.
| file | Open file handle (must have FS_Read flag) |
| buf | Buffer to receive the data (must be at least 'sz' bytes) |
| sz | Maximum number of bytes to read |
| bytesread | Receives the actual number of bytes read (can be 0 at EOF) |
Example:
| int64 fsSeek | ( | FSFile * | file, |
| int64 | off, | ||
| FSSeekType | seektype | ||
| ) |
Changes the current file position
Moves the file position to a new location for subsequent read/write operations. The position can be set relative to the beginning, current position, or end of the file.
| file | Open file handle |
| off | Offset in bytes (can be negative for FS_Cur and FS_End) |
| seektype | Reference point: FS_Set (start), FS_Cur (current), FS_End (end) |
Examples:
| int64 fsTell | ( | FSFile * | file | ) |
Gets the current file position
Returns the current byte offset within the file. This is the position where the next read or write operation will occur. The position starts at 0 (beginning of file) when the file is opened.
| file | Open file handle |
| bool fsWrite | ( | FSFile * | file, |
| void * | buf, | ||
| size_t | sz, | ||
| size_t * | byteswritten | ||
| ) |
Writes data to a file
Writes 'sz' bytes from the buffer to the file at the current position. The file position is advanced by the number of bytes written.
On most systems, this function will write all requested bytes or fail. However, on some systems (particularly networked filesystems), a short write may occur. Check byteswritten to verify the full write completed.
| file | Open file handle (must have FS_Write flag) |
| buf | Buffer containing data to write |
| sz | Number of bytes to write |
| byteswritten | Optional pointer to receive actual bytes written (can be NULL) |
Example: