在我的开源纯 C 代码中,我使用这个简单的结构从字符串缓冲区读取和解析数据:
typedef struct lts_LoadState
{
const unsigned char * pos;
size_t unread;
} lts_LoadState;
使用这个简单的 API访问缓冲区:
/* Initialize buffer */
void ltsLS_init(lts_LoadState * ls,const unsigned char * data, size_t len);
/* Do we have something to read? (Actually a macro.) */
BOOL ltsLS_good(ls);
/* How much do we have to read? (Actually a macro.) */
size_t ltsLS_unread(ls);
/* Eat given number of characters, return pointer to beginning of eaten data */
const unsigned char * ltsLS_eat(lts_LoadState * ls, size_t len);
注意:可以在不破坏当前实现的情况下ltsLS_unread
替换为。return (ltsLS_good(ls)) ? SIZE_MAX : 0
此代码用于从字符串缓冲区以自定义格式加载一些数据。(这可能是一个更好的说明。)
现在我需要不是从字符串缓冲区加载数据,而是从FILE
指针加载数据。
我不想复制粘贴实现,而是想重用现有代码。(当然,我可以重构/调整它。)
这是 C++ 中的教科书内容,但是如何在纯 C 中做到这一点而不会产生运行时开销?
这是一个使用lts_LoadState
API 且不可复制粘贴的示例函数(当然,可以更改以同时支持字符串缓冲区和FILE *
):
static int ltsLS_readline(
lts_LoadState * ls,
const unsigned char ** dest,
size_t * len
)
{
const unsigned char * origin = ls->pos;
unsigned char last = 0;
size_t read = 0;
while (ltsLS_good(ls))
{
if (ltsLS_unread(ls) > 0)
{
unsigned char b = *ls->pos; /* OK, this should be ltsLS_eat_char macro. */
++ls->pos;
--ls->unread;
if (b == '\n')
{
*dest = origin;
*len = (last == '\r') ? read - 1 : read;
return LUATEXTS_ESUCCESS;
}
last = b;
++read;
}
else
{
ls->unread = 0;
ls->pos = NULL;
}
}
return LUATEXTS_ECLIPPED;
}