refactor(sstream): 使用scc_snprintf替换snprintf_
日志格式化函数从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中的抽象接口定义,提供标准库的具体实现。
This commit is contained in:
@@ -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__ */
|
||||
|
||||
50
runtime/scc_core/include/scc_core_pal.h
Normal file
50
runtime/scc_core/include/scc_core_pal.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef __SCC_CORE_PAL_H__
|
||||
#define __SCC_CORE_PAL_H__
|
||||
// PAL, platform abstraction layer
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* 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__ */
|
||||
@@ -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 字符串兼容性
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user