Fix the space problem on the right side of the equal sign.

This commit is contained in:
aixiao 2019-08-06 18:01:24 +08:00
parent c91f6c093f
commit 3bf9e8941f
5 changed files with 47 additions and 17 deletions

View File

@ -3,13 +3,13 @@ CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip STRIP := $(CROSS_COMPILE)strip
AR := $(CROSS_COMPILE)ar AR := $(CROSS_COMPILE)ar
CFLAGS += -g -Wall CFLAGS += -g -Wall
LIBS = libini.so DLIB = libini.so
SLIB = libini.a SLIB = libini.a
all: libini.o all: libini.o
$(CC) $(CFLAGS) -FPIC -shared $^ -o $(LIBS) $(CC) $(CFLAGS) -FPIC -shared $^ -o $(DLIB)
$(AR) -rc $(SLIB) $^ $(AR) -rc $(SLIB) $^
clean: clean:
rm -rf *.o rm -rf *.o
rm $(LIBS) $(SLIB) rm $(DLIB) $(SLIB)

View File

@ -2,7 +2,7 @@
非常小的读取、写入ini配置文件c库. 非常小的读取、写入ini配置文件c库.
支持字符串、整形、长整型、浮点型. 支持字符串、整形、长整型、浮点型.
注释是"//"、"#"、";". 注释是"//"、"#"、";".
支持等号右有若干空格. 支持等号右有若干空格.
不再支持单一右边所有字符,支持两种格式一种是带引号和不带引号的key 不再支持单一右边所有字符,支持两种格式一种是带引号和不带引号的key
# build # build

View File

@ -8,5 +8,5 @@ c=1.223445446
[TAC] [TAC]
e="!@#$%^&*()_+"; e = abcdef!@#$%^&*()_+
f=abc f=123

View File

@ -5,6 +5,7 @@
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
// 去除str前的"和后的";
char *delchar(char *str, char *temp) char *delchar(char *str, char *temp)
{ {
int len = strlen(str); int len = strlen(str);
@ -19,6 +20,28 @@ char *delchar(char *str, char *temp)
return str; 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) int getinikeystring(char *title, char *key, char *filename, char *buf)
{ {
FILE *fp; FILE *fp;
@ -26,6 +49,7 @@ int getinikeystring(char *title, char *key, char *filename, char *buf)
char sTitle[BUFFER_SIZE], *wTmp; char sTitle[BUFFER_SIZE], *wTmp;
char sLine[BUFFER_SIZE]; char sLine[BUFFER_SIZE];
sprintf(sTitle, "[%s]", title); sprintf(sTitle, "[%s]", title);
char buf1[BUFFER_SIZE];
if (NULL == (fp = fopen(filename, "r"))) { if (NULL == (fp = fopen(filename, "r"))) {
perror("fopen"); perror("fopen");
@ -41,10 +65,12 @@ int getinikeystring(char *title, char *key, char *filename, char *buf)
wTmp = strchr(sLine, '='); wTmp = strchr(sLine, '=');
if ((NULL != wTmp) && (1 == flag)) { if ((NULL != wTmp) && (1 == flag)) {
sLine[strlen(sLine) - 1] = '\0'; 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 !(strncasecmp
(strtok(sLine, "="), key, strlen(strtok(sLine, "="))))) { (del_space(strtok(sLine, "="), buf1), key,
strlen(del_space(strtok(sLine, "="), buf1))))) { // 判断key长度的等号左边字符是否相等, 判断key长度的等号左边字符(去除空格)是否相等
fclose(fp); fclose(fp);
while (*(wTmp + 1) == ' ') { while (*(wTmp + 1) == ' ') {
wTmp++; 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) int putinikeystring(char *title, char *key, char *val, char *filename)
{ {
char buf[BUFFER_SIZE];
FILE *fpr; FILE *fpr;
FILE *fpw; FILE *fpw;
int flag = 0; int flag = 0;
@ -83,8 +110,8 @@ int putinikeystring(char *title, char *key, char *val, char *filename)
if (0 == strncmp(key, sLine, strlen(key)) if (0 == strncmp(key, sLine, strlen(key))
&& &&
!(strncasecmp !(strncasecmp
(strtok(sLine_backup, "="), key, (del_space(strtok(sLine_backup, "="), buf), key,
strlen(strtok(sLine_backup, "="))))) { strlen(del_space(strtok(sLine_backup, "="), buf))))) {
flag = 2; flag = 2;
sprintf(wTmp + 1, "%s\n", val); sprintf(wTmp + 1, "%s\n", val);
} }

7
test.c
View File

@ -4,10 +4,11 @@
int main(void) int main(void)
{ {
char buf[50]; char buf[50];
memset(buf, 0, 50);
getinikeystring("CAT", "age", "config.ini", buf); // 字符串 getinikeystring("CAT", "age", "config.ini", buf); // 字符串
printf("%s\n", buf); printf("%s\n", buf);
memset(buf, 0, 50); memset(buf, 0, 50);
getinikeystring("CAT", "name", "config.ini", buf); // 字符串 getinikeystring("CAT", "name", "config.ini", buf); // 字符串
printf("%s\n", buf); printf("%s\n", buf);
@ -20,7 +21,7 @@ int main(void)
printf("%f\n", getinikeyfloat("CAT", "c", "config.ini")); // 浮点型默认小数点后6位 printf("%f\n", getinikeyfloat("CAT", "c", "config.ini")); // 浮点型默认小数点后6位
memset(buf, 0, 50); memset(buf, 0, 50);
putinikeystring("TAC", "e", "\"!@#$%^&*()_+\";", "config.ini"); // 写入 putinikeystring("TAC", "e", " abcdef!@#$%^&*()_+", "config.ini"); // 写入
getinikeystring("TAC", "e", "config.ini", buf); getinikeystring("TAC", "e", "config.ini", buf);
printf("%s\n", buf); printf("%s\n", buf);
memset(buf, 0, 50); memset(buf, 0, 50);
@ -29,5 +30,7 @@ int main(void)
printf("%s\n", buf); printf("%s\n", buf);
memset(buf, 0, 50); memset(buf, 0, 50);
printf("%d\n", getinikeystring("CAT", "age", "config.ini", buf));
return 1; return 1;
} }