diff --git a/libnx/inc/public/nox/stdio.h b/libnx/inc/public/nox/stdio.h index 22f691d..4076617 100644 --- a/libnx/inc/public/nox/stdio.h +++ b/libnx/inc/public/nox/stdio.h @@ -3,4 +3,16 @@ #ifndef LIBNX_STDIO_H #define LIBNX_STDIO_H +#include "public/nox/stdtypes.h" +#include "public/nox/string.h" + +typedef struct file_T file_T; + +file_T file_open (string_t path); +void file_close (file_T* file); +uint64_t file_read (file_T* file, void* buffer, uint64_t n); +uint64_t file_write (file_T* file, void* buffer, uint64_t n); +uint64_t file_cursor_get (file_T* file); +void file_cursor_set (file_T* file, uint64_t cursor); + #endif //LIBNX_STDIO_H diff --git a/libnx/src/stdio.c b/libnx/src/stdio.c index 263ea62..6846c2a 100644 --- a/libnx/src/stdio.c +++ b/libnx/src/stdio.c @@ -1,3 +1,43 @@ // This file is part of noxos and licensed under the MIT open source license #include "public/nox/stdio.h" +#include "public/nox/syscall.h" +#include "public/nox/math.h" + +struct file_T{ + uint64_t fd; + uint64_t cursor; +}; + +file_T file_open(string_t path) { + file_T file; + + nx_fs_open(path, strlen(path), &file.fd); + file.cursor = 0; + + return file; +} + +void file_close(file_T* file) { + nx_fs_close(file->fd); +} + +uint64_t file_read(file_T* file, void* buffer, uint64_t n) { + uint64_t rbytes = nx_fs_read(file->fd, file->cursor, buffer, n); + file->cursor += min(rbytes, n); + return rbytes; +} + +uint64_t file_write(file_T* file, void* buffer, uint64_t n) { + uint64_t wbytes = nx_fs_write(file->fd, file->cursor, buffer, n); + file->cursor += min(wbytes, n); + return wbytes; +} + +uint64_t file_cursor_get(file_T* file) { + return file->cursor; +} + +void file_cursor_set(file_T* file, uint64_t cursor) { + file->cursor = cursor; +} \ No newline at end of file