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
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)

View File

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

View File

@ -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

View File

@ -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);
}

7
test.c
View File

@ -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);
@ -29,5 +30,7 @@ int main(void)
printf("%s\n", buf);
memset(buf, 0, 50);
printf("%d\n", getinikeystring("CAT", "age", "config.ini", buf));
return 1;
}