diff options
Diffstat (limited to 'source/base/base_io.h')
| -rw-r--r-- | source/base/base_io.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/source/base/base_io.h b/source/base/base_io.h new file mode 100644 index 0000000..ac55737 --- /dev/null +++ b/source/base/base_io.h | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | #ifndef BASE_IO_H | ||
| 2 | #define BASE_IO_H | ||
| 3 | |||
| 4 | #define STDIN_FD 0 | ||
| 5 | #define STDOUT_FD 1 | ||
| 6 | #define STDERR_FD 2 | ||
| 7 | |||
| 8 | internal s64 | ||
| 9 | os_write(s32 fd, void const *buf, u64 count) | ||
| 10 | { | ||
| 11 | return syscall(SYS_write, fd, buf, count); | ||
| 12 | } | ||
| 13 | |||
| 14 | internal s64 | ||
| 15 | os_read(s32 fd, void *buf, u64 count) | ||
| 16 | { | ||
| 17 | return syscall(SYS_read, fd, buf, count); | ||
| 18 | } | ||
| 19 | |||
| 20 | internal void | ||
| 21 | print_s8(string8 s) | ||
| 22 | { | ||
| 23 | os_write(STDOUT_FILENO, s.data, s.size); | ||
| 24 | } | ||
| 25 | |||
| 26 | internal void | ||
| 27 | print(const char *str) | ||
| 28 | { | ||
| 29 | s32 len = 0; | ||
| 30 | while (str[len]) len++; | ||
| 31 | os_write(STDOUT_FILENO, str, len); | ||
| 32 | |||
| 33 | } | ||
| 34 | |||
| 35 | internal void | ||
| 36 | write_int(s32 num) | ||
| 37 | { | ||
| 38 | |||
| 39 | if (num < 0) | ||
| 40 | { | ||
| 41 | write(STDERR_FILENO, "-", 1); | ||
| 42 | num = -num; | ||
| 43 | } | ||
| 44 | if (num >= 10) | ||
| 45 | write_int(num / 10); | ||
| 46 | char digit = '0' + (num % 10); | ||
| 47 | |||
| 48 | write(STDERR_FILENO, &digit, 1); | ||
| 49 | } | ||
| 50 | |||
| 51 | internal void | ||
| 52 | write_string(s32 fd, const char *str) | ||
| 53 | { | ||
| 54 | s32 len = 0; | ||
| 55 | while (str[len]) len++; | ||
| 56 | os_write(fd, str, len); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | #endif /* BASE_IO_H */ | ||
