diff options
Diffstat (limited to 'source/base/base_os.h')
| -rw-r--r-- | source/base/base_os.h | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/source/base/base_os.h b/source/base/base_os.h new file mode 100644 index 0000000..b87c946 --- /dev/null +++ b/source/base/base_os.h | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | #ifndef BASE_OS_H | ||
| 2 | #define BASE_OS_H | ||
| 3 | |||
| 4 | #ifdef OS_LINUX | ||
| 5 | |||
| 6 | |||
| 7 | #define STDIN_FD 0 | ||
| 8 | #define STDOUT_FD 1 | ||
| 9 | #define STDERR_FD 2 | ||
| 10 | |||
| 11 | #endif /* BASE_OS_H */ | ||
| 12 | |||
| 13 | #ifdef BASE_IMPLEMENTATION | ||
| 14 | |||
| 15 | internal string8 | ||
| 16 | load_file(mem_arena *arena, const char *path) | ||
| 17 | { | ||
| 18 | string8 result = {0}; | ||
| 19 | struct stat sbuf = {0}; | ||
| 20 | |||
| 21 | // TODO(nasr): abstract this to a platform layer | ||
| 22 | s32 file = open(path, O_RDONLY); | ||
| 23 | if(file == -1) | ||
| 24 | { | ||
| 25 | warn("fialed to open file. path could be invalid"); return (string8){0}; | ||
| 26 | } | ||
| 27 | |||
| 28 | if(fstat(file, &sbuf) == -1) | ||
| 29 | { | ||
| 30 | warn("error: fstat failed"); | ||
| 31 | close(file); | ||
| 32 | return (string8){0}; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | result = PushString8(arena, sbuf.st_size); | ||
| 37 | |||
| 38 | result.size = (u64)sbuf.st_size; | ||
| 39 | if(result.size != 0) | ||
| 40 | { | ||
| 41 | result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0); | ||
| 42 | } | ||
| 43 | |||
| 44 | close(file); | ||
| 45 | return result; | ||
| 46 | } | ||
| 47 | |||
| 48 | internal string8 | ||
| 49 | write_file(const char *path, string8 data) | ||
| 50 | { | ||
| 51 | string8 result = {0}; | ||
| 52 | s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
| 53 | if(file == -1) | ||
| 54 | { | ||
| 55 | warn("failed to open file for writing. path could be invalid"); | ||
| 56 | return (string8){0}; | ||
| 57 | } | ||
| 58 | |||
| 59 | u64 written = 0; | ||
| 60 | while(written < data.size) | ||
| 61 | { | ||
| 62 | s64 err = write(file, data.data + written, data.size - written); | ||
| 63 | if(err == -1) | ||
| 64 | { | ||
| 65 | warn("write syscall failed"); | ||
| 66 | close(file); | ||
| 67 | return (string8){0}; | ||
| 68 | } | ||
| 69 | written += err; | ||
| 70 | } | ||
| 71 | |||
| 72 | close(file); | ||
| 73 | result = data; | ||
| 74 | return result; | ||
| 75 | } | ||
| 76 | |||
| 77 | #define os_write(void const *buf, u64 count) _os_write(STDIN, buf, count) | ||
| 78 | #define os_read(void const *buf, u64 count) _os_read(STDIN, buf, count) | ||
| 79 | |||
| 80 | internal s64 | ||
| 81 | _os_write(s32 fd, void const *buf, u64 count) | ||
| 82 | { | ||
| 83 | return syscall(SYS_write, fd, buf, count); | ||
| 84 | } | ||
| 85 | |||
| 86 | internal s64 | ||
| 87 | _os_read(s32 fd, void *buf, u64 count) | ||
| 88 | { | ||
| 89 | return syscall(SYS_read, fd, buf, count); | ||
| 90 | } | ||
| 91 | |||
| 92 | internal void | ||
| 93 | print_s8(string8 s) | ||
| 94 | { | ||
| 95 | os_write(s.data, s.size); | ||
| 96 | } | ||
| 97 | |||
| 98 | internal void | ||
| 99 | print(const char *str) | ||
| 100 | { | ||
| 101 | s32 len = 0; | ||
| 102 | while (str[len]) len++; | ||
| 103 | os_write(STDOUT_FILENO, str, len); | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | internal void | ||
| 108 | write_string(s32 fd, const char *str) | ||
| 109 | { | ||
| 110 | s32 len = 0; | ||
| 111 | while (str[len]) len++; | ||
| 112 | os_write(fd, str, len); | ||
| 113 | } | ||
| 114 | |||
| 115 | internal void | ||
| 116 | write_int(s32 num) | ||
| 117 | { | ||
| 118 | |||
| 119 | if (num < 0) | ||
| 120 | { | ||
| 121 | write(STDERR_FILENO, "-", 1); | ||
| 122 | num = -num; | ||
| 123 | } | ||
| 124 | if (num >= 10) write_int(num / 10); | ||
| 125 | |||
| 126 | char digit = '0' + (num % 10); | ||
| 127 | |||
| 128 | write(STDERR_FILENO, &digit, 1); | ||
| 129 | } | ||
| 130 | |||
| 131 | #endif | ||
| 132 | #endif | ||
