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:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 字符串兼容性
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,7 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#define __SCC_CORE_FMT_IMPL__
|
||||
#include <scc_core_impl.h>
|
||||
#define __SCC_LOG_IMPL_IMPORT_SRC__
|
||||
#define log_vsnprintf vsnprintf_
|
||||
#define log_puts(str) scc_printf("%s", str)
|
||||
#define log_exit scc_exit
|
||||
#include <log.h>
|
||||
#include <scc_core_pal.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
@@ -16,91 +10,73 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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); }
|
||||
|
||||
99
runtime/scc_core/src/core_impl.c
Normal file
99
runtime/scc_core/src/core_impl.c
Normal file
@@ -0,0 +1,99 @@
|
||||
// IMPLEMENT
|
||||
#include <printf/printf.c>
|
||||
|
||||
#include <scc_core_impl.h>
|
||||
#define __SCC_LOG_IMPL_IMPORT_SRC__
|
||||
#include <scc_core_log.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user