diff --git a/README.md b/README.md index 6bb01be..0866839 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/libconf.a b/libconf.a new file mode 100644 index 0000000..006ce53 Binary files /dev/null and b/libconf.a differ diff --git a/libconf.c b/libconf.c index c765e66..3242949 100644 --- a/libconf.c +++ b/libconf.c @@ -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) { diff --git a/libconf.h b/libconf.h index 0d48e5b..88fed2a 100644 --- a/libconf.h +++ b/libconf.h @@ -7,6 +7,6 @@ #include #include -char *read_conf(char *filename, char *module, char *key); +extern char *read_conf(char *filename, char *module, char *key); #endif diff --git a/libconf.o b/libconf.o new file mode 100644 index 0000000..b3af925 Binary files /dev/null and b/libconf.o differ diff --git a/libconf.so b/libconf.so new file mode 100644 index 0000000..dc2f2ca Binary files /dev/null and b/libconf.so differ diff --git a/test b/test new file mode 100644 index 0000000..f5cb07e Binary files /dev/null and b/test differ