feature (libnx): implemented basic file operations

This commit is contained in:
antifallobst 2023-06-07 00:05:44 +02:00
parent 2c4cb4dd01
commit b8c7dd8a4d
2 changed files with 52 additions and 0 deletions

View File

@ -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

View File

@ -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;
}