From b8c7dd8a4d28111d0dc53dc64f6b7d4b68b82436 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Wed, 7 Jun 2023 00:05:44 +0200 Subject: [PATCH] feature (libnx): implemented basic file operations --- libnx/inc/public/nox/stdio.h | 12 +++++++++++ libnx/src/stdio.c | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) 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