i64 -> u64 + osFileXXX() works on both linux and mac I think
This commit is contained in:
@@ -643,7 +643,7 @@ fn void osDebugPrint(bool debug_mode, const char* format, ...);
|
|||||||
TermIOs osStartTUI(bool blocking);
|
TermIOs osStartTUI(bool blocking);
|
||||||
fn void osEndTUI(TermIOs old_terminal_attributes);
|
fn void osEndTUI(TermIOs old_terminal_attributes);
|
||||||
fn Dim2 osGetTerminalDimensions();
|
fn Dim2 osGetTerminalDimensions();
|
||||||
void osBlitToTerminal(ptr writeable_output_ansi_string, i64 count);
|
void osBlitToTerminal(ptr writeable_output_ansi_string, u64 count);
|
||||||
void osReadConsoleInput(u8* buffer, u32 len);
|
void osReadConsoleInput(u8* buffer, u32 len);
|
||||||
|
|
||||||
// network stuff
|
// network stuff
|
||||||
|
|||||||
@@ -37,121 +37,6 @@ fn void osSleepMicroseconds(u32 t) {
|
|||||||
usleep(t);
|
usleep(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Files
|
|
||||||
fn bool osFileExists(String filename) {
|
|
||||||
bool result = access((str)filename.bytes, F_OK) == 0;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn String osFileRead(Arena* arena, ptr filepath) {
|
|
||||||
struct stat st;
|
|
||||||
stat(filepath, &st);
|
|
||||||
String result = { st.st_size, st.st_size, 0 };
|
|
||||||
result.bytes = arenaAlloc(arena, st.st_size);
|
|
||||||
|
|
||||||
size_t handle = open(filepath, O_RDWR, S_IRUSR | S_IRGRP | S_IROTH);
|
|
||||||
read(handle, result.bytes, st.st_size);
|
|
||||||
close(handle);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bool osFileCreate(String filename) {
|
|
||||||
/*
|
|
||||||
M_Scratch scratch = scratch_get();
|
|
||||||
string nt = str_copy(&scratch.arena, filename);
|
|
||||||
bool result = true;
|
|
||||||
size_t handle = open((const char*) nt.str, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
|
|
||||||
if (handle == -1) {
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
scratch_return(&scratch);
|
|
||||||
close(handle);
|
|
||||||
return true;
|
|
||||||
*/
|
|
||||||
bool result = true;
|
|
||||||
size_t handle = open((str)filename.bytes, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
|
|
||||||
if (handle == -1) {
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
if (close(handle) == -1) {
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bool osFileCreateWrite(String filename, String data) {
|
|
||||||
/*
|
|
||||||
M_Scratch scratch = scratch_get();
|
|
||||||
string nt = str_copy(&scratch.arena, filename);
|
|
||||||
b32 result = true;
|
|
||||||
size_t handle =
|
|
||||||
open((const char*) nt.str, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
|
|
||||||
if (handle == -1) result = false;
|
|
||||||
write(handle, data.str, data.size);
|
|
||||||
close(handle);
|
|
||||||
scratch_return(&scratch);
|
|
||||||
return result;
|
|
||||||
*/
|
|
||||||
bool result = true;
|
|
||||||
size_t handle = open(
|
|
||||||
(str)filename.bytes,
|
|
||||||
O_RDWR | O_CREAT | O_TRUNC,
|
|
||||||
S_IRUSR | S_IRGRP | S_IROTH
|
|
||||||
);
|
|
||||||
if (handle == -1) result = false;
|
|
||||||
write(handle, data.bytes, data.length);
|
|
||||||
close(handle);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bool osFileWrite(String filename, String data) {
|
|
||||||
/*
|
|
||||||
M_Scratch scratch = scratch_get();
|
|
||||||
string nt = str_copy(&scratch.arena, filename);
|
|
||||||
b32 result = true;
|
|
||||||
size_t handle =
|
|
||||||
open((const char*) nt.str, O_RDWR | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
|
|
||||||
if (handle == -1) result = false;
|
|
||||||
write(handle, data.str, data.size);
|
|
||||||
close(handle);
|
|
||||||
*/
|
|
||||||
bool result = true;
|
|
||||||
size_t handle = open((str) filename.bytes, O_RDWR | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
|
|
||||||
if (handle == -1) result = false;
|
|
||||||
write(handle, data.bytes, data.length);
|
|
||||||
close(handle);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn Resulti64 osFileOpenForWriting(String filename) {
|
|
||||||
size_t handle = open((str)filename.bytes, O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IROTH);
|
|
||||||
Resulti64 result = {
|
|
||||||
.success = handle != -1,
|
|
||||||
.value = (i64)handle,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn Resulti64 osFileClose(Resulti64 handle) {
|
|
||||||
i32 close_result = close((size_t)handle.value);
|
|
||||||
Resulti64 result = {
|
|
||||||
.success = close_result != -1,
|
|
||||||
.value = (i64)close_result,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bool osFileWriteOpenFile(Resulti64 handle, String data) {
|
|
||||||
assert(handle.success == true);
|
|
||||||
i32 wrote_this_round = 0;
|
|
||||||
for (i32 bytes_written = 0; bytes_written < data.length; bytes_written += wrote_this_round) {
|
|
||||||
wrote_this_round = write((size_t)handle.value, data.bytes + bytes_written, data.length - bytes_written);
|
|
||||||
if (wrote_this_round == -1) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Misc
|
// Misc
|
||||||
fn void osDebugPrint(bool debug_mode, const char * format, ... ) {
|
fn void osDebugPrint(bool debug_mode, const char * format, ... ) {
|
||||||
if (debug_mode) {
|
if (debug_mode) {
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ fn Dim2 osGetTerminalDimensions() {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void osBlitToTerminal(ptr writeable_output_ansi_string, i64 count) {
|
void osBlitToTerminal(ptr writeable_output_ansi_string, u64 count) {
|
||||||
int flags = fcntl(STDOUT_FILENO, F_GETFL);
|
int flags = fcntl(STDOUT_FILENO, F_GETFL);
|
||||||
fcntl(STDOUT_FILENO, F_SETFL, flags & ~O_NONBLOCK);
|
fcntl(STDOUT_FILENO, F_SETFL, flags & ~O_NONBLOCK);
|
||||||
|
|
||||||
@@ -157,3 +157,119 @@ i32 osLanIPAddress() { // returns as HOST byte-order
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Files
|
||||||
|
fn bool osFileExists(String filename) {
|
||||||
|
bool result = access((str)filename.bytes, F_OK) == 0;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn String osFileRead(Arena* arena, ptr filepath) {
|
||||||
|
struct stat st;
|
||||||
|
stat(filepath, &st);
|
||||||
|
String result = { st.st_size, st.st_size, 0 };
|
||||||
|
result.bytes = arenaAlloc(arena, st.st_size);
|
||||||
|
|
||||||
|
size_t handle = open(filepath, O_RDWR, S_IRUSR | S_IRGRP | S_IROTH);
|
||||||
|
read(handle, result.bytes, st.st_size);
|
||||||
|
close(handle);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bool osFileCreate(String filename) {
|
||||||
|
/*
|
||||||
|
M_Scratch scratch = scratch_get();
|
||||||
|
string nt = str_copy(&scratch.arena, filename);
|
||||||
|
bool result = true;
|
||||||
|
size_t handle = open((const char*) nt.str, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
|
||||||
|
if (handle == -1) {
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
scratch_return(&scratch);
|
||||||
|
close(handle);
|
||||||
|
return true;
|
||||||
|
*/
|
||||||
|
bool result = true;
|
||||||
|
size_t handle = open((str)filename.bytes, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||||
|
if (handle == -1) {
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
if (close(handle) == -1) {
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bool osFileCreateWrite(String filename, String data) {
|
||||||
|
/*
|
||||||
|
M_Scratch scratch = scratch_get();
|
||||||
|
string nt = str_copy(&scratch.arena, filename);
|
||||||
|
b32 result = true;
|
||||||
|
size_t handle =
|
||||||
|
open((const char*) nt.str, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
|
||||||
|
if (handle == -1) result = false;
|
||||||
|
write(handle, data.str, data.size);
|
||||||
|
close(handle);
|
||||||
|
scratch_return(&scratch);
|
||||||
|
return result;
|
||||||
|
*/
|
||||||
|
bool result = true;
|
||||||
|
size_t handle = open(
|
||||||
|
(str)filename.bytes,
|
||||||
|
O_RDWR | O_CREAT | O_TRUNC,
|
||||||
|
S_IRUSR | S_IRGRP | S_IROTH
|
||||||
|
);
|
||||||
|
if (handle == -1) result = false;
|
||||||
|
write(handle, data.bytes, data.length);
|
||||||
|
close(handle);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bool osFileWrite(String filename, String data) {
|
||||||
|
/*
|
||||||
|
M_Scratch scratch = scratch_get();
|
||||||
|
string nt = str_copy(&scratch.arena, filename);
|
||||||
|
b32 result = true;
|
||||||
|
size_t handle =
|
||||||
|
open((const char*) nt.str, O_RDWR | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
|
||||||
|
if (handle == -1) result = false;
|
||||||
|
write(handle, data.str, data.size);
|
||||||
|
close(handle);
|
||||||
|
*/
|
||||||
|
bool result = true;
|
||||||
|
size_t handle = open((str) filename.bytes, O_RDWR | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
|
||||||
|
if (handle == -1) result = false;
|
||||||
|
write(handle, data.bytes, data.length);
|
||||||
|
close(handle);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn Resulti64 osFileOpenForWriting(String filename) {
|
||||||
|
size_t handle = open((str)filename.bytes, O_WRONLY | O_APPEND, S_IRUSR | S_IRGRP | S_IROTH);
|
||||||
|
Resulti64 result = {
|
||||||
|
.success = handle != -1,
|
||||||
|
.value = (i64)handle,
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn Resulti64 osFileClose(Resulti64 handle) {
|
||||||
|
i32 close_result = close((size_t)handle.value);
|
||||||
|
Resulti64 result = {
|
||||||
|
.success = close_result != -1,
|
||||||
|
.value = (i64)close_result,
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bool osFileWriteOpenFile(Resulti64 handle, String data) {
|
||||||
|
assert(handle.success == true);
|
||||||
|
i32 wrote_this_round = 0;
|
||||||
|
for (i32 bytes_written = 0; bytes_written < data.length; bytes_written += wrote_this_round) {
|
||||||
|
wrote_this_round = write((size_t)handle.value, data.bytes + bytes_written, data.length - bytes_written);
|
||||||
|
if (wrote_this_round == -1) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -198,7 +198,7 @@ fn Dim2 osGetTerminalDimensions() {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void osBlitToTerminal(ptr writeable_output_ansi_string, i64 count) {
|
void osBlitToTerminal(ptr writeable_output_ansi_string, u64 count) {
|
||||||
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
DWORD written;
|
DWORD written;
|
||||||
WriteConsole(hStdout, writeable_output_ansi_string, count, &written, NULL);
|
WriteConsole(hStdout, writeable_output_ansi_string, count, &written, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user