From 3bf9e8941f1d83be3d0a5df01eff76c0fdbf443b Mon Sep 17 00:00:00 2001 From: aixiao Date: Tue, 6 Aug 2019 18:01:24 +0800 Subject: [PATCH] Fix the space problem on the right side of the equal sign. --- Makefile | 6 +++--- README.md | 2 +- config.ini | 12 ++++++------ libini.c | 37 ++++++++++++++++++++++++++++++++----- test.c | 7 +++++-- 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index d5c1790..6b9dd4f 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,13 @@ CC := $(CROSS_COMPILE)gcc STRIP := $(CROSS_COMPILE)strip AR := $(CROSS_COMPILE)ar CFLAGS += -g -Wall -LIBS = libini.so +DLIB = libini.so SLIB = libini.a all: libini.o - $(CC) $(CFLAGS) -FPIC -shared $^ -o $(LIBS) + $(CC) $(CFLAGS) -FPIC -shared $^ -o $(DLIB) $(AR) -rc $(SLIB) $^ clean: rm -rf *.o - rm $(LIBS) $(SLIB) + rm $(DLIB) $(SLIB) diff --git a/README.md b/README.md index 1b3d10a..d85f49a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 非常小的读取、写入ini配置文件c库. 支持字符串、整形、长整型、浮点型. 注释是"//"、"#"、";". - 支持等号左右有若干空格. + 支持等号右有若干空格. 不再支持单一右边所有字符,支持两种格式一种是带引号和不带引号的key # build diff --git a/config.ini b/config.ini index 55d4658..af12dc0 100644 --- a/config.ini +++ b/config.ini @@ -1,12 +1,12 @@ [CAT] -age="1343567"; +age = "1343567"; name="aixiao.me"; -a="10"; -b=1234567890 -c=1.223445446 +a ="10"; +b= 1234567890 +c = 1.223445446 [TAC] -e="!@#$%^&*()_+"; -f=abc +e = abcdef!@#$%^&*()_+ +f=123 \ No newline at end of file diff --git a/libini.c b/libini.c index a83f835..1836521 100644 --- a/libini.c +++ b/libini.c @@ -5,6 +5,7 @@ #define BUFFER_SIZE 1024 +// 去除str前的"和后的"; char *delchar(char *str, char *temp) { int len = strlen(str); @@ -19,6 +20,28 @@ char *delchar(char *str, char *temp) return str; } +// 去除字符空格 +char *del_space(char *str, char *buf) +{ + unsigned int uLen = strlen(str); + + if (0 == uLen) { + return '\0'; + } + + memset(buf, 0, uLen + 1); + + unsigned int i = 0, j = 0; + for (i = 0; i < uLen + 1; i++) { + if (str[i] != ' ') { + buf[j++] = str[i]; + } + } + buf[j] = '\0'; + + return buf; +} + int getinikeystring(char *title, char *key, char *filename, char *buf) { FILE *fp; @@ -26,6 +49,7 @@ int getinikeystring(char *title, char *key, char *filename, char *buf) char sTitle[BUFFER_SIZE], *wTmp; char sLine[BUFFER_SIZE]; sprintf(sTitle, "[%s]", title); + char buf1[BUFFER_SIZE]; if (NULL == (fp = fopen(filename, "r"))) { perror("fopen"); @@ -41,10 +65,12 @@ int getinikeystring(char *title, char *key, char *filename, char *buf) wTmp = strchr(sLine, '='); if ((NULL != wTmp) && (1 == flag)) { sLine[strlen(sLine) - 1] = '\0'; - if (0 == strncmp(key, sLine, strlen(key)) - && + + //if (0 == strncmp(key, sLine, strlen(key)) && !(strncasecmp(strtok(sLine, "="), key, strlen(strtok(sLine, "="))))) { + if (0 == strncmp(key, sLine, strlen(key)) && !(strncasecmp - (strtok(sLine, "="), key, strlen(strtok(sLine, "="))))) { + (del_space(strtok(sLine, "="), buf1), key, + strlen(del_space(strtok(sLine, "="), buf1))))) { // 判断key长度的等号左边字符是否相等, 判断key长度的等号左边字符(去除空格)是否相等 fclose(fp); while (*(wTmp + 1) == ' ') { wTmp++; @@ -64,6 +90,7 @@ int getinikeystring(char *title, char *key, char *filename, char *buf) int putinikeystring(char *title, char *key, char *val, char *filename) { + char buf[BUFFER_SIZE]; FILE *fpr; FILE *fpw; int flag = 0; @@ -83,8 +110,8 @@ int putinikeystring(char *title, char *key, char *val, char *filename) if (0 == strncmp(key, sLine, strlen(key)) && !(strncasecmp - (strtok(sLine_backup, "="), key, - strlen(strtok(sLine_backup, "="))))) { + (del_space(strtok(sLine_backup, "="), buf), key, + strlen(del_space(strtok(sLine_backup, "="), buf))))) { flag = 2; sprintf(wTmp + 1, "%s\n", val); } diff --git a/test.c b/test.c index 48912be..0c42524 100644 --- a/test.c +++ b/test.c @@ -4,10 +4,11 @@ int main(void) { - char buf[50]; + memset(buf, 0, 50); getinikeystring("CAT", "age", "config.ini", buf); // 字符串 printf("%s\n", buf); + memset(buf, 0, 50); getinikeystring("CAT", "name", "config.ini", buf); // 字符串 printf("%s\n", buf); @@ -20,7 +21,7 @@ int main(void) printf("%f\n", getinikeyfloat("CAT", "c", "config.ini")); // 浮点型(默认小数点后6位) memset(buf, 0, 50); - putinikeystring("TAC", "e", "\"!@#$%^&*()_+\";", "config.ini"); // 写入 + putinikeystring("TAC", "e", " abcdef!@#$%^&*()_+", "config.ini"); // 写入 getinikeystring("TAC", "e", "config.ini", buf); printf("%s\n", buf); memset(buf, 0, 50); @@ -28,6 +29,8 @@ int main(void) getinikeystring("TAC", "f", "config.ini", buf); printf("%s\n", buf); memset(buf, 0, 50); + + printf("%d\n", getinikeystring("CAT", "age", "config.ini", buf)); return 1; }