From cf96cc141bd5ee47675f432590a93dce331cbeee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=82?= Date: Fri, 21 Oct 2022 23:48:19 +0800 Subject: [PATCH] support save file by repl --- src/PikaObj.c | 24 ++++++++++++++++++++++-- src/pika_config_valid.h | 7 +++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/PikaObj.c b/src/PikaObj.c index 01d4e0b55..2987a1efb 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -999,7 +999,7 @@ void obj_shellLineProcess(PikaObj* self, ShellConfig* cfg) { buff[buff_i++] = input[0]; } /* end */ - __platform_printf("\r\n=============== [code] ===============\r\n"); + __platform_printf("\r\n=============== [Code] ===============\r\n"); size_t len = strGetSize(buff); for (size_t i = 0; i < len; i++) { if (buff[i] == '\r') { @@ -1012,7 +1012,27 @@ void obj_shellLineProcess(PikaObj* self, ShellConfig* cfg) { __platform_printf("%c", buff[i]); } __platform_printf("\r\n"); - __platform_printf("=============== [code] ===============\r\n"); + __platform_printf("=============== [File] ===============\r\n"); + __platform_printf("[ Info] File buff used: %d/%d (%0.2f%%)\r\n", + (int)len, (int)PIKA_READ_FILE_BUFF_SIZE, + ((float)len / (float)PIKA_READ_FILE_BUFF_SIZE)); +#if PIKA_SHELL_SAVE_FILE_ENABLE + char* file_name = PIKA_SHELL_SAVE_FILE_NAME; + __platform_printf("[ Info] Saving file to '%s'...\r\n", + file_name); + FILE* fp = __platform_fopen(file_name, "wb"); + if (NULL == fp) { + __platform_printf("[ Error] Open file '%s' error!\r\n", + file_name); + __platform_fclose(fp); + } else { + __platform_fwrite(buff, 1, len + 1, fp); + __platform_printf("[ Info] Writing %d bytes to '%s'\r\n", + (int)(len + 1), file_name); + __platform_fclose(fp); + } +#endif + __platform_printf("=============== [ Run] ===============\r\n"); obj_run(self, (char*)buff); if (NULL != strstr(buff, "exit()")) { is_exit = PIKA_TRUE; diff --git a/src/pika_config_valid.h b/src/pika_config_valid.h index 9d59faf94..09f121085 100644 --- a/src/pika_config_valid.h +++ b/src/pika_config_valid.h @@ -324,6 +324,13 @@ #define PIKA_ARG_CACHE_SIZE sizeof(Arg) + 16 #endif + #ifndef PIKA_SHELL_SAVE_FILE_ENABLE + #define PIKA_SHELL_SAVE_FILE_ENABLE 0 + #endif + + #ifndef PIKA_SHELL_SAVE_FILE_NAME + #define PIKA_SHELL_SAVE_FILE_NAME "pika_shell_save.py" + #endif /* configuration validation */ #endif