From 2c4b803058df159df7133b1cdf9d60a7b5fba599 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Mon, 2 Mar 2026 21:21:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(sstream):=20=E4=BD=BF=E7=94=A8scc=5Fsn?= =?UTF-8?q?printf=E6=9B=BF=E6=8D=A2snprintf=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 日志格式化函数从snprintf_更改为scc_snprintf以保持一致性, 并调整代码格式以适应新的函数调用方式。 refactor(runtime): 实现平台抽象层(PAL) 引入scc_core_pal.h作为平台抽象层,将内存管理、IO操作和 文件系统功能抽象化。原有的scc_malloc等函数现在通过宏定义 映射到对应的scc_pal_*函数。 feat(runtime): 添加字符串断言检查 在scc_cstring_append_cstr函数中添加Assert(str->data != null) 断言,确保字符串数据指针不为空。 refactor(runtime): 重新组织核心实现文件结构 将原有的cfg.std_impl.c拆分为core_impl.c中的具体实现和 scc_core_pal.h中的抽象接口定义,提供标准库的具体实现。 --- libs/sstream/include/scc_pos_log.h | 8 +- libs/sstream/src/scc_pos_log.c | 8 +- runtime/scc_core/include/scc_core_impl.h | 41 +++----- runtime/scc_core/include/scc_core_pal.h | 50 ++++++++++ runtime/scc_core/include/scc_core_str.h | 1 + runtime/scc_core/src/cfg.std_impl.c | 118 +++++++++-------------- runtime/scc_core/src/core_impl.c | 99 +++++++++++++++++++ 7 files changed, 222 insertions(+), 103 deletions(-) create mode 100644 runtime/scc_core/include/scc_core_pal.h create mode 100644 runtime/scc_core/src/core_impl.c diff --git a/libs/sstream/include/scc_pos_log.h b/libs/sstream/include/scc_pos_log.h index fd9ae2c..5328740 100644 --- a/libs/sstream/include/scc_pos_log.h +++ b/libs/sstream/include/scc_pos_log.h @@ -9,9 +9,11 @@ extern logger_t __scc_usr_log; #define SCC_POS_LOG(level, pos, fmt, ...) \ do { \ char _full_msg[LOGGER_MAX_BUF_SIZE]; \ - int _n = snprintf_(_full_msg, sizeof(_full_msg), \ - "%s:%lu:%lu: ", (pos).name, (pos).line, (pos).col); \ - snprintf_(_full_msg + _n, sizeof(_full_msg) - _n, fmt, ##__VA_ARGS__); \ + int _n = \ + scc_snprintf(_full_msg, sizeof(_full_msg), \ + "%s:%zu:%zu: ", (pos).name, (pos).line, (pos).col); \ + scc_snprintf(_full_msg + _n, sizeof(_full_msg) - _n, fmt, \ + ##__VA_ARGS__); \ __scc_usr_log.handler(&__scc_usr_log, level, null, 0, null, "%s", \ _full_msg); \ } while (0) diff --git a/libs/sstream/src/scc_pos_log.c b/libs/sstream/src/scc_pos_log.c index 044f395..0524a7f 100644 --- a/libs/sstream/src/scc_pos_log.c +++ b/libs/sstream/src/scc_pos_log.c @@ -32,13 +32,13 @@ static int user_log_handler(logger_t *module, log_level_t level, #endif /* clang-format on */ char buf[LOGGER_MAX_BUF_SIZE]; - int off = - snprintf_(buf, sizeof(buf), "%s[%s]%s ", color_code ? color_code : "", - level_str, color_code ? ANSI_NONE : ""); + int off = scc_snprintf(buf, sizeof(buf), "%s[%s]%s ", + color_code ? color_code : "", level_str, + color_code ? ANSI_NONE : ""); va_list args; va_start(args, fmt); - vsnprintf_(buf + off, sizeof(buf) - off, fmt, args); + scc_vsnprintf(buf + off, sizeof(buf) - off, fmt, args); va_end(args); log_puts(buf); diff --git a/runtime/scc_core/include/scc_core_impl.h b/runtime/scc_core/include/scc_core_impl.h index f4400c8..9078a19 100644 --- a/runtime/scc_core/include/scc_core_impl.h +++ b/runtime/scc_core/include/scc_core_impl.h @@ -1,21 +1,21 @@ #ifndef __SCC_CORE_IMPL_H__ #define __SCC_CORE_IMPL_H__ +#include "scc_core_pal.h" #include "scc_core_type.h" -void *scc_malloc(usize size); -void *scc_calloc(usize count, usize size); -void *scc_realloc(void *ptr, usize new_size); -void scc_free(void *ptr); - -typedef struct scc_file *scc_file_t; +#define scc_malloc scc_pal_malloc +#define scc_calloc scc_pal_calloc +#define scc_realloc scc_pal_realloc +#define scc_free scc_pal_free +#define scc_exit scc_pal_exit +typedef void *scc_file_t; typedef enum { - SCC_FILE_READ, /* 读取源文件、头文件 */ - SCC_FILE_WRITE, /* 写入目标文件、汇编文件 */ - SCC_FILE_APPEND /* 日志、调试输出 */ + SCC_FILE_READ, + SCC_FILE_WRITE, + SCC_FILE_APPEND, } scc_fmode_t; - scc_file_t scc_fopen(const char *path, scc_fmode_t mode); void scc_fclose(scc_file_t file); usize scc_fsize(scc_file_t file); @@ -23,20 +23,11 @@ usize scc_fread(scc_file_t file, void *buffer, usize size); usize scc_fwrite(scc_file_t file, const void *buffer, usize size); cbool scc_fexists(const char *path); -#include "printf/printf.h" -#define scc_snprintf snprintf_ -#define scc_vsnprintf vsnprintf_ -#ifdef __SCC_CORE_FMT_IMPL__ -#include "printf/printf.c" -#endif - -/* 标准输出 - 用于编译进度、结果 */ -void scc_printf(const char *format, ...); - -/* 错误输出 - 用于错误信息、警告 */ -void scc_eprintf(const char *format, ...); - -/* 程序控制 */ -void scc_exit(int code); +int scc_printf(const char *format, ...); +int scc_eprintf(const char *format, ...); +int scc_fprintf(scc_file_t file, const char *format, ...); +int scc_vfprintf(scc_file_t file, const char *format, va_list args); +int scc_snprintf(char *buff, usize buff_size, const char *fmt, ...); +int scc_vsnprintf(char *buff, usize buff_size, const char *fmt, va_list args); #endif /* __SCC_CORE_IMPL_H__ */ diff --git a/runtime/scc_core/include/scc_core_pal.h b/runtime/scc_core/include/scc_core_pal.h new file mode 100644 index 0000000..6919a08 --- /dev/null +++ b/runtime/scc_core/include/scc_core_pal.h @@ -0,0 +1,50 @@ +#ifndef __SCC_CORE_PAL_H__ +#define __SCC_CORE_PAL_H__ +// PAL, platform abstraction layer + +#include + +/* Memory */ + +// Required +void *scc_pal_malloc(size_t size); +void *scc_pal_calloc(size_t count, size_t size); +void *scc_pal_realloc(void *ptr, size_t new_size); +void scc_pal_free(void *ptr); + +/* Exit */ + +// Required +void scc_pal_exit(int code); + +/* IO */ + +// Required +void scc_pal_putchar(char c); + +// Option +void scc_pal_eputchar(char c); + +// Option +void scc_pal_write(const char *data, size_t len); + +// Option +void scc_pal_ewrite(const char *data, size_t len); + +/* File */ + +typedef void *scc_pal_file_t; +scc_pal_file_t scc_pal_fopen(const char *path); +void scc_pal_fclose(scc_pal_file_t f); +size_t scc_pal_fread(scc_pal_file_t f, void *buf, size_t size); +size_t scc_pal_fwrite(scc_pal_file_t f, const void *buf, size_t size); +size_t scc_pal_ftell(scc_pal_file_t f); +enum scc_pal_seek_type { + SCC_SEEK_PAL_CUR, + SCC_SEEK_PAL_END, + SCC_SEEK_PAL_SET, +}; +size_t scc_pal_fseek(scc_pal_file_t f, size_t offset, + enum scc_pal_seek_type whence); + +#endif /* __SCC_CORE_PAL_H__ */ diff --git a/runtime/scc_core/include/scc_core_str.h b/runtime/scc_core/include/scc_core_str.h index 7f57b64..1f32c08 100644 --- a/runtime/scc_core/include/scc_core_str.h +++ b/runtime/scc_core/include/scc_core_str.h @@ -117,6 +117,7 @@ static inline void scc_cstring_append_cstr(scc_cstring_t *str, const char *data, scc_memcpy(str->data + str->size - 1, data, len); str->size += len; + Assert(str->data != null); str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性 } diff --git a/runtime/scc_core/src/cfg.std_impl.c b/runtime/scc_core/src/cfg.std_impl.c index 44d37fb..d971046 100644 --- a/runtime/scc_core/src/cfg.std_impl.c +++ b/runtime/scc_core/src/cfg.std_impl.c @@ -2,13 +2,7 @@ #define _CRT_SECURE_NO_WARNINGS #endif -#define __SCC_CORE_FMT_IMPL__ -#include -#define __SCC_LOG_IMPL_IMPORT_SRC__ -#define log_vsnprintf vsnprintf_ -#define log_puts(str) scc_printf("%s", str) -#define log_exit scc_exit -#include +#include #include #include @@ -16,91 +10,73 @@ #include #include -void putchar_(char c) { putchar(c); } -/* ====== 内存管理核心接口实现 ====== */ +void *scc_pal_malloc(size_t size) { return malloc(size); } -void *scc_malloc(usize size) { return malloc(size); } +void *scc_pal_calloc(size_t count, size_t size) { return calloc(count, size); } -void *scc_calloc(usize count, usize size) { return calloc(count, size); } - -void *scc_realloc(void *ptr, usize new_size) { return realloc(ptr, new_size); } - -void scc_free(void *ptr) { free(ptr); } - -/* ====== 文件系统核心接口实现 ====== */ - -static const char *get_file_mode_string(scc_fmode_t mode) { - switch (mode) { - case SCC_FILE_READ: - return "rb"; - case SCC_FILE_WRITE: - return "wb"; - case SCC_FILE_APPEND: - return "ab"; - default: - return "rb"; - } +void *scc_pal_realloc(void *ptr, size_t new_size) { + return realloc(ptr, new_size); } -scc_file_t scc_fopen(const char *path, scc_fmode_t mode) { - const char *mode_str = get_file_mode_string(mode); - return (scc_file_t)fopen(path, mode_str); +void scc_pal_free(void *ptr) { free(ptr); } + +scc_pal_file_t scc_pal_fopen(const char *path) { + return (scc_pal_file_t)fopen(path, "r+b"); } -void scc_fclose(scc_file_t file) { +void scc_pal_fclose(scc_pal_file_t file) { if (file) { fclose((FILE *)file); } } -usize scc_fsize(scc_file_t file) { - FILE *fp = (FILE *)file; - if (fseek(fp, 0, SEEK_END) != 0) { - perror("fseek failed"); - return 0; - } - usize fsize = ftell(fp); - if (fseek(fp, 0, SEEK_SET)) { - perror("fseek failed"); - return 0; - } - return fsize; -} - -usize scc_fread(scc_file_t file, void *buffer, usize size) { +size_t scc_pal_fread(scc_pal_file_t file, void *buffer, size_t size) { if (!file || !buffer) return 0; return fread(buffer, 1, size, (FILE *)file); } -usize scc_fwrite(scc_file_t file, const void *buffer, usize size) { - if (!file || !buffer) - return 0; +size_t scc_pal_fwrite(scc_pal_file_t file, const void *buffer, size_t size) { return fwrite(buffer, 1, size, (FILE *)file); } -cbool scc_fexists(const char *path) { - scc_file_t fp = scc_fopen(path, SCC_FILE_READ); - if (!fp) - return false; - scc_fclose(fp); - return true; +size_t scc_pal_ftell(scc_pal_file_t f) { return ftell((FILE *)f); } + +size_t scc_pal_fseek(scc_pal_file_t f, size_t offset, + enum scc_pal_seek_type whence) { + int whence_val = 0; + switch (whence) { + case SCC_SEEK_PAL_CUR: + whence_val = SEEK_CUR; + break; + case SCC_SEEK_PAL_END: + whence_val = SEEK_END; + break; + case SCC_SEEK_PAL_SET: + whence_val = SEEK_SET; + break; + } + return fseek(f, offset, whence_val); } -void scc_printf(const char *format, ...) { - va_list args; - va_start(args, format); - vfprintf(stdout, format, args); - va_end(args); +void scc_pal_exit(int code) { exit(code); } + +void scc_pal_putchar(char c) { putchar(c); } + +void scc_pal_eputchar(char c) { fputc(c, stderr); } + +void scc_pal_write(const char *data, size_t len) { + int res = fputs(data, stdout); + if (res != 0) { + fprintf(stderr, "\nError writing to stdout [%d]\n", res); + scc_pal_exit(1); + } } -void scc_eprintf(const char *format, ...) { - va_list args; - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); +void scc_pal_ewrite(const char *data, size_t len) { + int res = fputs(data, stderr); + if (res != 0) { + fprintf(stderr, "\nError writing to stderr [%d]\n", res); + scc_pal_exit(1); + } } - -/* ====== 系统核心接口实现 ====== */ - -void scc_exit(int code) { exit(code); } diff --git a/runtime/scc_core/src/core_impl.c b/runtime/scc_core/src/core_impl.c new file mode 100644 index 0000000..a294cb5 --- /dev/null +++ b/runtime/scc_core/src/core_impl.c @@ -0,0 +1,99 @@ +// IMPLEMENT +#include + +#include +#define __SCC_LOG_IMPL_IMPORT_SRC__ +#include + +#define scc_stdout 1 +#define scc_stderr 2 + +void putchar_(char ch) { LOG_FATAL("you can't use printf.c directly"); } + +scc_file_t scc_fopen(const char *path, scc_fmode_t mode) { + return scc_pal_fopen(path); +} + +void scc_fclose(scc_file_t file) { scc_pal_fclose(file); } + +usize scc_fread(scc_file_t file, void *buffer, usize size) { + return scc_pal_fread(file, buffer, size); +} + +usize scc_fwrite(scc_file_t file, const void *buffer, usize size) { + return scc_pal_fwrite(file, buffer, size); +} + +usize scc_fsize(scc_pal_file_t file) { + if (scc_pal_fseek(file, 0, SCC_SEEK_PAL_END) != 0) { + LOG_ERROR("fseek failed"); + return 0; + } + size_t fsize = scc_pal_ftell(file); + if (scc_pal_fseek(file, 0, SCC_SEEK_PAL_SET)) { + LOG_ERROR("fseek failed"); + return 0; + } + return fsize; +} + +cbool scc_fexists(const char *path) { + scc_file_t fp = scc_fopen(path, SCC_FILE_READ); + if (!fp) + return false; + scc_fclose(fp); + return true; +} + +int scc_printf(const char *format, ...) { + int ret = 0; + va_list args; + va_start(args, format); + ret = scc_vfprintf((scc_file_t)scc_stdout, format, args); + va_end(args); + return ret; +} + +int scc_eprintf(const char *format, ...) { + int ret = 0; + va_list args; + va_start(args, format); + ret = scc_vfprintf((scc_file_t)scc_stderr, format, args); + va_end(args); + return ret; +} + +int scc_fprintf(scc_file_t file, const char *format, ...) { + int ret = 0; + va_list args; + va_start(args, format); + ret = scc_vfprintf(file, format, args); + va_end(args); + return ret; +} + +int scc_vfprintf(scc_file_t file, const char *format, va_list args) { + char buf[4096] = {0}; + int size = vsnprintf_(buf, sizeof(buf), format, args); + if (file == (scc_file_t)scc_stdout) { + scc_pal_write(buf, size); + } else if (file == (scc_file_t)scc_stderr) { + scc_pal_ewrite(buf, size); + } else { + scc_pal_fwrite(file, buf, size); + } + return size; +} + +int scc_snprintf(char *buff, usize buff_size, const char *fmt, ...) { + int ret; + va_list args; + va_start(args, fmt); + ret = vsnprintf_(buff, buff_size, fmt, args); + va_end(args); + return ret; +} + +int scc_vsnprintf(char *buff, usize buff_size, const char *fmt, va_list args) { + return vsnprintf_(buff, buff_size, fmt, args); +}