- Replace `scc_probe_stream_consume` with `scc_probe_stream_next` for consistent stream advancement - Remove redundant `scc_probe_stream_reset` calls before peeking, as `next` and `peek` handle state - Update `scc_cstring_new` to `scc_cstring_create` and `scc_pos_init` to `scc_pos_create` for naming consistency - Change `scc_pp_macro_get` parameter to `const scc_cstring_t*` for better const-correctness - Improves code clarity and maintains proper stream position tracking
131 lines
3.9 KiB
C
131 lines
3.9 KiB
C
#ifndef __SMCC_CORE_PROBE_STREAM_H__
|
||
#define __SMCC_CORE_PROBE_STREAM_H__
|
||
|
||
#include "core_impl.h"
|
||
#include "core_macro.h"
|
||
#include "core_mem.h"
|
||
#include "core_str.h"
|
||
|
||
struct scc_probe_stream;
|
||
typedef struct scc_probe_stream scc_probe_stream_t;
|
||
|
||
#define scc_stream_eof (-1)
|
||
|
||
/**
|
||
* @brief 带探针的流接口
|
||
*
|
||
* 这个流提供了双指针机制:当前读取位置(头指针)和探针位置(尾指针)。
|
||
* 尾指针只能向前移动,用于查看而不消费。
|
||
* 头指针可以前进或单次后退,但不能一直后退到尾指针后面。
|
||
*/
|
||
struct scc_probe_stream {
|
||
scc_cstring_t name;
|
||
|
||
/// @brief 消费头指针处的字符(移动头指针)
|
||
int (*consume)(scc_probe_stream_t *stream);
|
||
|
||
/// @brief 查看当前探针位置的字符,不移动任何指针
|
||
int (*peek)(scc_probe_stream_t *stream);
|
||
|
||
/// @brief 移动探针位置并返回字符
|
||
int (*next)(scc_probe_stream_t *stream);
|
||
|
||
/// @brief 移动头指针到探针位置
|
||
void (*sync)(scc_probe_stream_t *stream);
|
||
|
||
/// @brief 重置探针位置到头指针位置
|
||
void (*reset)(scc_probe_stream_t *stream);
|
||
|
||
/// @brief 回退一个字符(单次后退,头指针后退一步)
|
||
cbool (*back)(scc_probe_stream_t *stream);
|
||
|
||
/// @brief 读取指定数量的字符到缓冲区
|
||
usize (*read_buf)(scc_probe_stream_t *stream, char *buffer, usize count);
|
||
|
||
/// @brief 检查是否到达流末尾
|
||
cbool (*is_at_end)(scc_probe_stream_t *stream);
|
||
|
||
/// @brief 销毁流并释放资源
|
||
void (*drop)(scc_probe_stream_t *stream);
|
||
};
|
||
|
||
static inline int scc_probe_stream_consume(scc_probe_stream_t *self) {
|
||
return self->consume(self);
|
||
}
|
||
|
||
static inline int scc_probe_stream_peek(scc_probe_stream_t *self) {
|
||
return self->peek(self);
|
||
}
|
||
|
||
static inline int scc_probe_stream_next(scc_probe_stream_t *self) {
|
||
return self->next(self);
|
||
}
|
||
|
||
static inline void scc_probe_stream_sync(scc_probe_stream_t *self) {
|
||
self->sync(self);
|
||
}
|
||
|
||
static inline cbool scc_probe_stream_back(scc_probe_stream_t *self) {
|
||
return self->back(self);
|
||
}
|
||
|
||
static inline void scc_probe_stream_reset(scc_probe_stream_t *self) {
|
||
self->reset(self);
|
||
}
|
||
|
||
static inline usize scc_probe_stream_read_buf(scc_probe_stream_t *self,
|
||
char *buffer, usize count) {
|
||
return self->read_buf(self, buffer, count);
|
||
}
|
||
|
||
static inline cbool scc_probe_stream_is_at_end(scc_probe_stream_t *self) {
|
||
return self->is_at_end(self);
|
||
}
|
||
|
||
static inline cbool scc_probe_stream_has_more(scc_probe_stream_t *self) {
|
||
return !self->is_at_end(self);
|
||
}
|
||
|
||
static inline void scc_probe_stream_drop(scc_probe_stream_t *self) {
|
||
self->drop(self);
|
||
}
|
||
|
||
#ifndef __SCC_NO_MEM_PROBE_STREAM__
|
||
/**
|
||
* @brief 内存探针流结构
|
||
*/
|
||
typedef struct scc_mem_probe_stream {
|
||
scc_probe_stream_t stream;
|
||
const char *data;
|
||
usize data_length;
|
||
usize curr_pos; // 当前读取位置
|
||
usize probe_pos; // 探针位置(用于peek)
|
||
cbool owned; // 是否拥有数据(如果拥有将会自动释放)
|
||
} scc_mem_probe_stream_t;
|
||
|
||
/**
|
||
* @brief 初始化内存探针流(由你负责scc_mem_probe_stream_t的释放)
|
||
*
|
||
* @param stream 流结构指针
|
||
* @param data 数据指针
|
||
* @param length 数据长度
|
||
* @param owned 是否拥有数据(如果拥有将会自动释放)
|
||
* @return core_probe_stream_t* 成功返回流指针,失败返回NULL
|
||
*/
|
||
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
|
||
const char *data, usize length,
|
||
cbool owned);
|
||
/**
|
||
* @brief 构造内存探针流(其中drop会自动释放内存)
|
||
*
|
||
* @param data
|
||
* @param length
|
||
* @param owned 是否拥有数据(如果拥有将会自动释放)
|
||
* @return scc_probe_stream_t*
|
||
*/
|
||
scc_probe_stream_t *scc_mem_probe_stream_alloc(const char *data, usize length,
|
||
cbool owned);
|
||
#endif
|
||
|
||
#endif /* __SMCC_CORE_PROBE_STREAM_H__ */
|