added fwrite helpers

This commit is contained in:
DaniTheSkunk 2023-02-09 15:25:38 +00:00
parent fe22fe0895
commit 732acde1e6
2 changed files with 97 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#define GUARD_5B2A3676BBB85E5075C01FF213126C89
#include "types.h"
#include <stdio.h>
struct sw_filebuffer {
u8 *_data;
@ -9,6 +10,7 @@ struct sw_filebuffer {
u32 size;
};
/* TODO: use skunkworks overlay paths */
u8 *sw_file_load(char *path, u32 *out_size);
struct sw_filebuffer *sw_filebuffer_create(u32 size);
@ -17,4 +19,24 @@ void sw_filebuffer_destroy(struct sw_filebuffer *buf);
void sw_filebuffer_read(struct sw_filebuffer *buf, void *dst, u32 count);
/* fwrite helpers */
void sw_fwrite_u8(FILE *fil, u8 val);
void sw_fwrite_i8(FILE *fil, i8 val);
void sw_fwrite_u16(FILE *fil, u16 val);
void sw_fwrite_i16(FILE *fil, u16 val);
void sw_fwrite_u32(FILE *fil, u32 val);
void sw_fwrite_i32(FILE *fil, u32 val);
void sw_fwrite_u64(FILE *fil, u64 val);
void sw_fwrite_i64(FILE *fil, u64 val);
void sw_fwrite_f32(FILE *fil, f32 val);
void sw_fwrite_f64(FILE *fil, f64 val);
void sw_fwrite_bool(FILE *fil, bool val);
void sw_fwrite_str_nozero(FILE *fil, char *str);
void sw_fwrite_str_zero(FILE *fil, char *str);
void sw_fwrite_str_prefix8(FILE *fil, char *str);
void sw_fwrite_str_prefix16(FILE *fil, char *str);
void sw_fwrite_str_prefix32(FILE *fil, char *str);
#endif /* GUARD_5B2A3676BBB85E5075C01FF213126C89 */

View File

@ -61,3 +61,78 @@ void sw_filebuffer_read(struct sw_filebuffer *buf, void *dst, u32 count) {
memcpy(dst, buf->_data + buf->pos, count);
buf->pos += count;
}
/* fwrite helpers */
void sw_fwrite_u8(FILE *fil, u8 val) {
fwrite(&val, 1, 1, fil);
}
void sw_fwrite_i8(FILE *fil, i8 val) {
fwrite(&val, 1, 1, fil);
}
void sw_fwrite_u16(FILE *fil, u16 val) {
fwrite(&val, 1, 2, fil);
}
void sw_fwrite_i16(FILE *fil, u16 val) {
fwrite(&val, 1, 2, fil);
}
void sw_fwrite_u32(FILE *fil, u32 val) {
fwrite(&val, 1, 4, fil);
}
void sw_fwrite_i32(FILE *fil, u32 val) {
fwrite(&val, 1, 4, fil);
}
void sw_fwrite_u64(FILE *fil, u64 val) {
fwrite(&val, 1, 8, fil);
}
void sw_fwrite_i64(FILE *fil, u64 val) {
fwrite(&val, 1, 8, fil);
}
void sw_fwrite_f32(FILE *fil, f32 val) {
fwrite(&val, 1, 4, fil);
}
void sw_fwrite_f64(FILE *fil, f64 val) {
fwrite(&val, 1, 8, fil);
}
void sw_fwrite_bool(FILE *fil, bool val) {
fwrite(&val, 1, 1, fil);
}
void sw_fwrite_str_nozero(FILE *fil, char *str) {
fwrite(str, strlen(str), 1, fil);
}
void sw_fwrite_str_zero(FILE *fil, char *str) {
fwrite(str, strlen(str), 1, fil);
sw_fwrite_u8(fil, 0);
}
/* TODO: implement checks that string not too long? */
void sw_fwrite_str_prefix8(FILE *fil, char *str) {
u8 size = strlen(str);
sw_fwrite_u8(fil, size);
fwrite(str, size, 1, fil);
}
void sw_fwrite_str_prefix16(FILE *fil, char *str) {
u16 size = strlen(str);
sw_fwrite_u16(fil, size);
fwrite(str, size, 1, fil);
}
void sw_fwrite_str_prefix32(FILE *fil, char *str) {
u32 size = strlen(str);
sw_fwrite_u32(fil, size);
fwrite(str, size, 1, fil);
}