From 9db9888a499a6cb998f16270fbb7c0be3eacefe7 Mon Sep 17 00:00:00 2001 From: lyon Date: Wed, 4 May 2022 16:53:07 +0800 Subject: [PATCH] load libfile to array asset file is ok --- port/linux/test/compile-test.cpp | 9 +++-- src/PikaCompiler.c | 58 +++++++++++++++++++++++++++++--- src/PikaCompiler.h | 1 + 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/port/linux/test/compile-test.cpp b/port/linux/test/compile-test.cpp index 9e7cfd618..d34198e1a 100644 --- a/port/linux/test/compile-test.cpp +++ b/port/linux/test/compile-test.cpp @@ -430,7 +430,6 @@ TEST(lib, lib_to_file) { EXPECT_EQ(pikaMemNow(), 0); } - TEST(lib, save2) { LibObj* lib = New_LibObj(); @@ -466,8 +465,7 @@ TEST(lib, load_file) { "import test_module3\n" "test_module1.mytest()\n" "test_module2.mytest()\n" - "test_module3.mytest()\n" - ); + "test_module3.mytest()\n"); /* asset */ EXPECT_STREQ(log_buff[2], "test_module_1_hello\r\n"); EXPECT_STREQ(log_buff[1], "test_module_2_hello\r\n"); @@ -497,3 +495,8 @@ TEST(lib, load_err_file_type) { LibObj_deinit(lib); EXPECT_EQ(pikaMemNow(), 0); } + +TEST(lib, lib_file_to_array) { + Lib_loadLibraryFileToArray("test/python/lib_to_file.py.a", + "test/python"); +} diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index 8f4926af8..838fda384 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -287,19 +287,69 @@ int LibObj_loadLibrary(LibObj* self, uint8_t* library) { return 0; } -int LibObj_loadLibraryFile(LibObj* self, char* input_file_name) { - Arg* file_arg = arg_loadFile(NULL, input_file_name); +int LibObj_loadLibraryFile(LibObj* self, char* lib_file_name) { + Arg* file_arg = arg_loadFile(NULL, lib_file_name); if (NULL == file_arg) { __platform_printf("Error: Could not load library file '%s'\n", - input_file_name); + lib_file_name); return 1; } /* save file_arg as __lib_buf to libObj */ obj_setArg_noCopy(self, "__lib_buf", file_arg); if (0 != LibObj_loadLibrary(self, arg_getBytes(file_arg))) { __platform_printf("Error: Could not load library from '%s'\n", - input_file_name); + lib_file_name); return 2; } return 0; } + +size_t pika_fputs(char* str, FILE* fp) { + size_t size = strGetSize(str); + return __platform_fwrite(str, 1, size, fp); +} + +int Lib_loadLibraryFileToArray(char* origin_file_name, char* out_folder) { + Args buffs = {0}; + Arg* file_arg = arg_loadFile(NULL, origin_file_name); + int res = 0; + if (NULL == file_arg) { + __platform_printf("Error: Could not load file '%s'\n", + origin_file_name); + return 1; + } + char* output_file_name = NULL; + output_file_name = strsGetLastToken(&buffs, origin_file_name, '/'); + output_file_name = strsAppend(&buffs, "__asset_", output_file_name); + output_file_name = strsReplace(&buffs, output_file_name, ".", "_"); + output_file_name = strsAppend(&buffs, output_file_name, ".c"); + + char* output_file_path = strsAppend(&buffs, out_folder, "/"); + output_file_path = strsAppend(&buffs, output_file_path, output_file_name); + + FILE* fp = __platform_fopen(output_file_path, "w+"); + char* array_name = strsGetLastToken(&buffs, origin_file_name, '/'); + array_name = strsReplace(&buffs, array_name, ".", "_"); + pika_fputs("const unsigned char", fp); + pika_fputs(array_name, fp); + pika_fputs("[] = {", fp); + char byte_buff[32] = {0}; + uint8_t* array = arg_getBytes(file_arg); + for (size_t i = 0; i < arg_getBytesSize(file_arg); i++) { + if (i % 12 == 0) { + pika_fputs("\n ", fp); + } + __platform_sprintf(byte_buff, "0x%02x, ", array[i]); + pika_fputs(byte_buff, fp); + } + + pika_fputs("\n};\n", fp); + res = 0; + goto exit; + +exit: + __platform_fclose(fp); + strsDeinit(&buffs); + arg_deinit(file_arg); + return res; +} diff --git a/src/PikaCompiler.h b/src/PikaCompiler.h index 349424c52..9bb4dc799 100644 --- a/src/PikaCompiler.h +++ b/src/PikaCompiler.h @@ -19,6 +19,7 @@ int LibObj_staticLinkFile(LibObj* self, char* input_file_name); void LibObj_listModules(LibObj* self); int LibObj_saveLibraryFile(LibObj* self, char* output_file_name); int LibObj_loadLibraryFile(LibObj* self, char* input_file_name); +int Lib_loadLibraryFileToArray(char* origin_file_name, char* pikascript_root); #define LIB_VERSION_NUMBER 1 #define LIB_INFO_BLOCK_SIZE 32