diff --git a/README.md b/README.md index 8ba717c..1b3d10a 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ 非常小的读取、写入ini配置文件c库. 支持字符串、整形、长整型、浮点型. 注释是"//"、"#"、";". - 会读取等号右边所有字符. 支持等号左右有若干空格. + 不再支持单一右边所有字符,支持两种格式一种是带引号和不带引号的key # build git clone https://github.com/niuyuling/libini diff --git a/config.ini b/config.ini index 8850372..55d4658 100644 --- a/config.ini +++ b/config.ini @@ -1,11 +1,12 @@ [CAT] -age=1343567 -name=aixiao.me +age="1343567"; +name="aixiao.me"; -a=10 +a="10"; b=1234567890 c=1.223445446 [TAC] -e=!@#$%^&*()_+ +e="!@#$%^&*()_+"; +f=abc diff --git a/libini.c b/libini.c index 1b082e1..a83f835 100644 --- a/libini.c +++ b/libini.c @@ -5,6 +5,20 @@ #define BUFFER_SIZE 1024 +char *delchar(char *str, char *temp) +{ + int len = strlen(str); + char *p1 = str; + if ((0 == strncmp("\"", str, 1)) && (str[len - 1] == ';') + && (str[len - 2] == '"')) { + p1[len - 2] = '\0'; + return strcpy(temp, p1 + 1); + } else { + return str; + } + return str; +} + int getinikeystring(char *title, char *key, char *filename, char *buf) { FILE *fp; @@ -20,19 +34,22 @@ int getinikeystring(char *title, char *key, char *filename, char *buf) while (NULL != fgets(sLine, BUFFER_SIZE, fp)) { if (0 == strncmp("//", sLine, 2)) continue; - if (0 == strncmp("#", sLine, 1)) + if (sLine[0] == '#' || 0 == strncmp("#", sLine, 1)) continue; - if (0 == strncmp(";", sLine, 1)) + if (sLine[0] == ';' || 0 == strncmp(";", sLine, 1)) continue; wTmp = strchr(sLine, '='); if ((NULL != wTmp) && (1 == flag)) { sLine[strlen(sLine) - 1] = '\0'; - 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, "="))))) { fclose(fp); while (*(wTmp + 1) == ' ') { wTmp++; } - strcpy(buf, wTmp + 1); + delchar(strcpy(buf, wTmp + 1), buf); return 0; } } else { @@ -63,9 +80,13 @@ int putinikeystring(char *title, char *key, char *val, char *filename) wTmp = strchr(sLine, '='); if ((NULL != wTmp) && (1 == flag)) { strcpy(sLine_backup, sLine); - if (0 == strncmp(key, sLine, strlen(key)) && !(strncasecmp(strtok(sLine_backup, "="), key, strlen(strtok(sLine_backup, "="))))) { + if (0 == strncmp(key, sLine, strlen(key)) + && + !(strncasecmp + (strtok(sLine_backup, "="), key, + strlen(strtok(sLine_backup, "="))))) { flag = 2; - sprintf(wTmp+1, "%s\n", val); + sprintf(wTmp + 1, "%s\n", val); } } else { if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { @@ -101,4 +122,3 @@ float getinikeyfloat(char *title, char *key, char *filename) getinikeystring(title, key, filename, buf); return atof(buf); } - diff --git a/libini.h b/libini.h index 10fd6c3..3b434da 100644 --- a/libini.h +++ b/libini.h @@ -6,4 +6,4 @@ int getinikeyint(char *title, char *key, char *filename); long getinikeylong(char *title, char *key, char *filename); float getinikeyfloat(char *title, char *key, char *filename); -#endif \ No newline at end of file +#endif diff --git a/test.c b/test.c index 6c0f039..48912be 100644 --- a/test.c +++ b/test.c @@ -1,23 +1,33 @@ #include "libini.h" #include +#include -int main(void) { +int main(void) +{ char buf[50]; - getinikeystring("CAT", "age", "config.ini", buf); // 字符串 + getinikeystring("CAT", "age", "config.ini", buf); // 字符串 printf("%s\n", buf); - getinikeystring("CAT", "name", "config.ini", buf); // 字符串 + memset(buf, 0, 50); + getinikeystring("CAT", "name", "config.ini", buf); // 字符串 printf("%s\n", buf); + memset(buf, 0, 50); + printf("%d\n", getinikeyint("CAT", "a", "config.ini")); // 整型 + memset(buf, 0, 50); + printf("%ld\n", getinikeylong("CAT", "b", "config.ini")); // 长整型 + memset(buf, 0, 50); + printf("%f\n", getinikeyfloat("CAT", "c", "config.ini")); // 浮点型(默认小数点后6位) + memset(buf, 0, 50); - printf("%d\n", getinikeyint("CAT", "a", "config.ini")); // 整型 - printf("%ld\n", getinikeylong("CAT", "b", "config.ini")); // 长整型 - printf("%f\n", getinikeyfloat("CAT", "c", "config.ini")); // 浮点型(默认小数点后6位) - - - putinikeystring("TAC", "e", "!@#$%^&*()_+", "config.ini"); // 写入 + putinikeystring("TAC", "e", "\"!@#$%^&*()_+\";", "config.ini"); // 写入 getinikeystring("TAC", "e", "config.ini", buf); printf("%s\n", buf); - + memset(buf, 0, 50); + + getinikeystring("TAC", "f", "config.ini", buf); + printf("%s\n", buf); + memset(buf, 0, 50); + return 1; -} \ No newline at end of file +}