bio
 
Loading...
Searching...
No Matches
file.h
1#ifndef BIO_FILE_H
2#define BIO_FILE_H
3
4#include "bio.h"
5
15typedef struct {
16 bio_handle_t handle;
18
25
27typedef struct {
28 uint64_t size;
30
51bool
52bio_fdopen(bio_file_t* file_ptr, uintptr_t fd, bio_error_t* error);
53
70uintptr_t
72
85bool
87 bio_file_t* file_ptr,
88 const char* restrict filename,
89 const char* restrict mode,
90 bio_error_t* error
91);
92
101size_t
103 bio_file_t file,
104 const void* buf,
105 size_t size,
106 bio_error_t* error
107);
108
117size_t
119 bio_file_t file,
120 void* buf,
121 size_t size,
122 bio_error_t* error
123);
124
134bool
136 bio_file_t file,
137 int64_t offset,
138 int origin,
139 bio_error_t* error
140);
141
147int64_t
149 bio_file_t file,
150 bio_error_t* error
151);
152
162bool
164
166bool
168
170bool
172
181static inline size_t
182bio_fwrite_exactly(bio_file_t file, const void* buf, size_t size, bio_error_t* error) {
183 size_t total_bytes_written = 0;
184 while (total_bytes_written < size) {
185 size_t bytes_written = bio_fwrite(
186 file,
187 (char*)buf + total_bytes_written,
188 size - total_bytes_written,
189 error
190 );
191 if (bytes_written == 0) { break; }
192 total_bytes_written += bytes_written;
193 }
194
195 return total_bytes_written;
196}
197
206static inline size_t
207bio_fread_exactly(bio_file_t file, void* buf, size_t size, bio_error_t* error) {
208 size_t total_bytes_read = 0;
209 while (total_bytes_read < size) {
210 size_t bytes_read = bio_fread(
211 file,
212 (char*)buf + total_bytes_read,
213 size - total_bytes_read,
214 error
215 );
216 if (bytes_read == 0) { break; }
217 total_bytes_read += bytes_read;
218 }
219
220 return total_bytes_read;
221}
222
225#endif
uintptr_t bio_funwrap(bio_file_t file)
Retrieve an OS handle from a bio_file_t.
bio_file_t BIO_STDIN
Standard input.
size_t bio_fread(bio_file_t file, void *buf, size_t size, bio_error_t *error)
Read from a file.
static size_t bio_fread_exactly(bio_file_t file, void *buf, size_t size, bio_error_t *error)
Convenient function to read exactly a number of bytes from a file without short read.
Definition file.h:207
bool bio_fclose(bio_file_t file, bio_error_t *error)
Close a file handle.
size_t bio_fwrite(bio_file_t file, const void *buf, size_t size, bio_error_t *error)
Write to a file.
int64_t bio_ftell(bio_file_t file, bio_error_t *error)
Retrieve the file offset.
bio_file_t BIO_STDERR
Standard error.
bool bio_fflush(bio_file_t file, bio_error_t *error)
Flush the file buffer.
bool bio_fstat(bio_file_t file, bio_stat_t *stat, bio_error_t *error)
Retrieves statistics about a file.
bool bio_fseek(bio_file_t file, int64_t offset, int origin, bio_error_t *error)
Modify the file offset.
static size_t bio_fwrite_exactly(bio_file_t file, const void *buf, size_t size, bio_error_t *error)
Convenient function to write exactly a number of bytes to a file without short write.
Definition file.h:182
bio_file_t BIO_STDOUT
Standard output.
bool bio_fdopen(bio_file_t *file_ptr, uintptr_t fd, bio_error_t *error)
Wraps a handle from the OS into a bio_file_t.
bool bio_fopen(bio_file_t *file_ptr, const char *restrict filename, const char *restrict mode, bio_error_t *error)
Open a file for reading or writing.
An error returned from a function.
Definition bio.h:468
Handle to a file.
Definition file.h:15
Definition bio.h:126
Statistics about a file.
Definition file.h:27
uint64_t size
The file size in bytes.
Definition file.h:28