use pikafs_fxxx API

This commit is contained in:
lyon 2023-02-12 11:12:28 +08:00
parent 9c8f60c839
commit 531dced423
5 changed files with 59 additions and 69 deletions

View File

@ -3,30 +3,13 @@
#include "PikaCompiler.h"
#include "PikaStdData_List.h"
typedef struct {
uint8_t* addr;
size_t size;
size_t pos;
} PIKAFS_FILE;
int PikaStdData_FILEIO_init(PikaObj* self, char* path, char* mode) {
if (obj_isArgExist(self, "_f")) {
/* already initialized */
return 0;
}
if (strIsStartWith(path, "pikafs/")) {
PIKAFS_FILE* f = (PIKAFS_FILE*)pikaMalloc(sizeof(PIKAFS_FILE));
memset(f, 0, sizeof(PIKAFS_FILE));
extern volatile PikaObj* __pikaMain;
uint8_t* library_bytes = obj_getPtr((PikaObj*)__pikaMain, "@libraw");
if (NULL == library_bytes) {
return 1;
}
char* file_name = path + 7;
if (PIKA_RES_OK != _loadModuleDataWithName(library_bytes, file_name,
&f->addr, &f->size)) {
return 1;
}
pikafs_FILE* f = pikafs_fopen(path + 7, "rb");
obj_setInt(self, "pikafs", PIKA_TRUE);
obj_setPtr(self, "_f", f);
obj_setStr(self, "_mode", mode);
@ -43,11 +26,11 @@ int PikaStdData_FILEIO_init(PikaObj* self, char* path, char* mode) {
void PikaStdData_FILEIO_close(PikaObj* self) {
if (PIKA_TRUE == obj_getInt(self, "pikafs")) {
PIKAFS_FILE* f = obj_getPtr(self, "_f");
pikafs_FILE* f = obj_getPtr(self, "_f");
if (NULL == f) {
return;
}
pikaFree(f, sizeof(PIKAFS_FILE));
pikafs_fclose(f);
obj_setPtr(self, "_f", NULL);
return;
}
@ -59,18 +42,6 @@ void PikaStdData_FILEIO_close(PikaObj* self) {
obj_setPtr(self, "_f", NULL);
}
size_t _pikafs_fread(void* buf, size_t size, size_t count, PIKAFS_FILE* f) {
if (f->pos >= f->size) {
return 0;
}
if (f->pos + size * count > f->size) {
count = (f->size - f->pos) / size;
}
__platform_memcpy(buf, f->addr + f->pos, size * count);
f->pos += size * count;
return count;
}
Arg* PikaStdData_FILEIO_read(PikaObj* self, PikaTuple* size_) {
int size = 0;
if (pikaTuple_getSize(size_) == 0) {
@ -87,11 +58,11 @@ Arg* PikaStdData_FILEIO_read(PikaObj* self, PikaTuple* size_) {
int n = 0;
/* read */
if (PIKA_TRUE == obj_getInt(self, "pikafs")) {
PIKAFS_FILE* f = obj_getPtr(self, "_f");
pikafs_FILE* f = obj_getPtr(self, "_f");
if (NULL == f) {
return NULL;
}
n = _pikafs_fread(buf, 1, size, f);
n = pikafs_fread(buf, 1, size, f);
} else {
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
"--gtest_filter=except.except_break"
// "--gtest_filter=except.except_break"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -3,30 +3,13 @@
#include "PikaCompiler.h"
#include "PikaStdData_List.h"
typedef struct {
uint8_t* addr;
size_t size;
size_t pos;
} PIKAFS_FILE;
int PikaStdData_FILEIO_init(PikaObj* self, char* path, char* mode) {
if (obj_isArgExist(self, "_f")) {
/* already initialized */
return 0;
}
if (strIsStartWith(path, "pikafs/")) {
PIKAFS_FILE* f = (PIKAFS_FILE*)pikaMalloc(sizeof(PIKAFS_FILE));
memset(f, 0, sizeof(PIKAFS_FILE));
extern volatile PikaObj* __pikaMain;
uint8_t* library_bytes = obj_getPtr((PikaObj*)__pikaMain, "@libraw");
if (NULL == library_bytes) {
return 1;
}
char* file_name = path + 7;
if (PIKA_RES_OK != _loadModuleDataWithName(library_bytes, file_name,
&f->addr, &f->size)) {
return 1;
}
pikafs_FILE* f = pikafs_fopen(path + 7, "rb");
obj_setInt(self, "pikafs", PIKA_TRUE);
obj_setPtr(self, "_f", f);
obj_setStr(self, "_mode", mode);
@ -43,11 +26,11 @@ int PikaStdData_FILEIO_init(PikaObj* self, char* path, char* mode) {
void PikaStdData_FILEIO_close(PikaObj* self) {
if (PIKA_TRUE == obj_getInt(self, "pikafs")) {
PIKAFS_FILE* f = obj_getPtr(self, "_f");
pikafs_FILE* f = obj_getPtr(self, "_f");
if (NULL == f) {
return;
}
pikaFree(f, sizeof(PIKAFS_FILE));
pikafs_fclose(f);
obj_setPtr(self, "_f", NULL);
return;
}
@ -59,18 +42,6 @@ void PikaStdData_FILEIO_close(PikaObj* self) {
obj_setPtr(self, "_f", NULL);
}
size_t _pikafs_fread(void* buf, size_t size, size_t count, PIKAFS_FILE* f) {
if (f->pos >= f->size) {
return 0;
}
if (f->pos + size * count > f->size) {
count = (f->size - f->pos) / size;
}
__platform_memcpy(buf, f->addr + f->pos, size * count);
f->pos += size * count;
return count;
}
Arg* PikaStdData_FILEIO_read(PikaObj* self, PikaTuple* size_) {
int size = 0;
if (pikaTuple_getSize(size_) == 0) {
@ -87,11 +58,11 @@ Arg* PikaStdData_FILEIO_read(PikaObj* self, PikaTuple* size_) {
int n = 0;
/* read */
if (PIKA_TRUE == obj_getInt(self, "pikafs")) {
PIKAFS_FILE* f = obj_getPtr(self, "_f");
pikafs_FILE* f = obj_getPtr(self, "_f");
if (NULL == f) {
return NULL;
}
n = _pikafs_fread(buf, 1, size, f);
n = pikafs_fread(buf, 1, size, f);
} else {
FILE* f = obj_getPtr(self, "_f");
if (f == NULL) {

View File

@ -865,3 +865,39 @@ PIKA_RES pikaMaker_linkRaw(PikaMaker* self, char* file_path) {
LibObj_staticLinkFile(lib, file_path);
return PIKA_RES_OK;
}
pikafs_FILE* pikafs_fopen(char* file_name, char* mode) {
pikafs_FILE* f = (pikafs_FILE*)pikaMalloc(sizeof(pikafs_FILE));
memset(f, 0, sizeof(pikafs_FILE));
extern volatile PikaObj* __pikaMain;
uint8_t* library_bytes = obj_getPtr((PikaObj*)__pikaMain, "@libraw");
if (NULL == library_bytes) {
return NULL;
}
if (PIKA_RES_OK !=
_loadModuleDataWithName(library_bytes, file_name, &f->addr, &f->size)) {
return NULL;
}
return f;
}
int pikafs_fread(void* buf, size_t size, size_t count, pikafs_FILE* f) {
if (f->pos >= f->size) {
return 0;
}
if (f->pos + size * count > f->size) {
count = (f->size - f->pos) / size;
}
__platform_memcpy(buf, f->addr + f->pos, size * count);
f->pos += size * count;
return count;
}
int pikafs_fwrite(void* buf, size_t size, size_t count, pikafs_FILE* file) {
return -1;
}
int pikafs_fclose(pikafs_FILE* file) {
pikaFree(file,sizeof(pikafs_FILE));
return 0;
}

View File

@ -45,4 +45,16 @@ PIKA_RES _loadModuleDataWithName(uint8_t* library_bytes,
#define PIKA_APP_VERSION_OFFSET 2
#define PIKA_APP_MODULE_NUM_OFFSET 3
typedef struct {
uint8_t* addr;
size_t size;
size_t pos;
} pikafs_FILE;
pikafs_FILE* pikafs_fopen(char* file_name, char* mode);
int pikafs_fread(void* buf, size_t size, size_t count, pikafs_FILE* file);
int pikafs_fwrite(void* buf, size_t size, size_t count, pikafs_FILE* file);
int pikafs_fclose(pikafs_FILE* file);
#endif