添加字符串预处理,设置转义字符

This commit is contained in:
aixiao 2022-01-20 12:07:47 +08:00
parent 48a6ea7d14
commit 6deababa06
7 changed files with 25 additions and 3 deletions

View File

@ -7,5 +7,5 @@
make
# test
gcc -Wall test.c -o test -L./ -lconf -static
gcc -Wall test.c -o test -I./ -L./ -lconf -static
./test

BIN
libconf.a Normal file

Binary file not shown.

View File

@ -1,5 +1,27 @@
#include "libconf.h"
/* 字符串预处理,设置转义字符 */
static void string_pretreatment(char *str, int *len)
{
char *lf, *p, *ori_strs[] = { "\\r", "\\n", "\\b", "\\v", "\\f", "\\t", "\\a", "\\b", "\\0" }, to_chrs[] = { '\r', '\n', '\b', '\v', '\f', '\t', '\a', '\b', '\0' };
int i;
while ((lf = strchr(str, '\n')) != NULL) {
for (p = lf + 1; *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'; p++)
*len -= 1;
strcpy(lf, p);
*len -= 1;
}
for (i = 0; i < sizeof(to_chrs); i++) {
for (p = strstr(str, ori_strs[i]); p; p = strstr(p, ori_strs[i])) {
//支持\\r
*(p - 1) == '\\' ? (*p--) : (*p = to_chrs[i]);
memmove(p + 1, p + 2, strlen(p + 2));
(*len)--;
}
}
}
/* 在content中设置变量(var)的首地址,值(val)的位置首地址和末地址,返回下一行指针 */
static char *set_var_val_lineEnd(char *content, char **var, char **val_begin, char **val_end)
{
@ -67,6 +89,7 @@ static char *set_var_val_lineEnd(char *content, char **var, char **val_begin, ch
val_len = strlen(*val_begin);
*val_end = lineEnd = *val_begin + val_len;
}
string_pretreatment(*val_begin, &val_len);
*val_end = *val_begin + val_len;
//printf("var[%s]\nbegin[%s]\n\n", *var, *val_begin);
return lineEnd;
@ -75,7 +98,6 @@ static char *set_var_val_lineEnd(char *content, char **var, char **val_begin, ch
static char *parse_global_module(char *content, char *key)
{
char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL)
{

View File

@ -7,6 +7,6 @@
#include <error.h>
#include <unistd.h>
char *read_conf(char *filename, char *module, char *key);
extern char *read_conf(char *filename, char *module, char *key);
#endif

BIN
libconf.o Normal file

Binary file not shown.

BIN
libconf.so Normal file

Binary file not shown.

BIN
test Normal file

Binary file not shown.