This commit is contained in:
Daniel Ledda
2025-01-11 17:02:49 +01:00
parent 1ae4a5fef9
commit ef930159e7
7 changed files with 172 additions and 158 deletions

View File

@@ -22,4 +22,63 @@ void os_free(void *ptr, size_t size) {
Assert(err != -1);
}
string os_readEntireFile(Arena *arena, string filename) {
FILE *input = fopen((char *)filename.str, "r");
string readBuffer;
if (input) {
struct stat st;
stat((char *)filename.str, &st);
size_t fsize = st.st_size;
readBuffer = PushString(arena, fsize);
fread(readBuffer.str, sizeof(byte), readBuffer.length, input);
fclose(input);
} else {
readBuffer = PushString(arena, 0);
}
return readBuffer;
}
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
bool result = false;
FILE *output = fopen((char *)filename.str, "w");
if (output) {
fwrite(contents, contentsLength, contentsLength, output);
fclose(output);
result = true;
}
return result;
}
bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
bool result = false;
FILE *output = fopen((char *)filename.str, "a");
if (output) {
fwrite(contents, sizeof(byte), contentsLength, output);
fclose(output);
result = true;
}
return result;
}
void os_log(LogTarget target, const char *fmt, va_list argList) {
Scratch scratch = scratchStart(0, 0);
string result = strPrintfv(scratch.arena, fmt, argList);
// TODO(djledda): finish implementation without cstdlib
switch (target) {
case LogTarget_stdin:
write(0, (const void *)result.str, result.length);
break;
case LogTarget_stderr:
fflush(stderr);
write(2, (const void *)result.str, result.length);
break;
case LogTarget_stdout:
default:
fflush(stdout);
write(1, (const void *)result.str, result.length);
break;
}
scratchEnd(scratch);
}
#endif