The delchar() function removes string quotes and colons

This commit is contained in:
aixiao 2019-07-02 11:15:11 +08:00
parent 70f4c55c8a
commit c91f6c093f
5 changed files with 55 additions and 24 deletions

View File

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

View File

@ -1,11 +1,12 @@
[CAT] [CAT]
age=1343567 age="1343567";
name=aixiao.me name="aixiao.me";
a=10 a="10";
b=1234567890 b=1234567890
c=1.223445446 c=1.223445446
[TAC] [TAC]
e=!@#$%^&*()_+ e="!@#$%^&*()_+";
f=abc

View File

@ -5,6 +5,20 @@
#define BUFFER_SIZE 1024 #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) int getinikeystring(char *title, char *key, char *filename, char *buf)
{ {
FILE *fp; FILE *fp;
@ -20,19 +34,22 @@ int getinikeystring(char *title, char *key, char *filename, char *buf)
while (NULL != fgets(sLine, BUFFER_SIZE, fp)) { while (NULL != fgets(sLine, BUFFER_SIZE, fp)) {
if (0 == strncmp("//", sLine, 2)) if (0 == strncmp("//", sLine, 2))
continue; continue;
if (0 == strncmp("#", sLine, 1)) if (sLine[0] == '#' || 0 == strncmp("#", sLine, 1))
continue; continue;
if (0 == strncmp(";", sLine, 1)) if (sLine[0] == ';' || 0 == strncmp(";", sLine, 1))
continue; continue;
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)) && !(strncasecmp(strtok(sLine, "="), key, strlen(strtok(sLine, "="))))) { if (0 == strncmp(key, sLine, strlen(key))
&&
!(strncasecmp
(strtok(sLine, "="), key, strlen(strtok(sLine, "="))))) {
fclose(fp); fclose(fp);
while (*(wTmp + 1) == ' ') { while (*(wTmp + 1) == ' ') {
wTmp++; wTmp++;
} }
strcpy(buf, wTmp + 1); delchar(strcpy(buf, wTmp + 1), buf);
return 0; return 0;
} }
} else { } else {
@ -63,9 +80,13 @@ int putinikeystring(char *title, char *key, char *val, char *filename)
wTmp = strchr(sLine, '='); wTmp = strchr(sLine, '=');
if ((NULL != wTmp) && (1 == flag)) { if ((NULL != wTmp) && (1 == flag)) {
strcpy(sLine_backup, sLine); 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; flag = 2;
sprintf(wTmp+1, "%s\n", val); sprintf(wTmp + 1, "%s\n", val);
} }
} else { } else {
if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { if (0 == strncmp(sTitle, sLine, strlen(sTitle))) {
@ -101,4 +122,3 @@ float getinikeyfloat(char *title, char *key, char *filename)
getinikeystring(title, key, filename, buf); getinikeystring(title, key, filename, buf);
return atof(buf); return atof(buf);
} }

28
test.c
View File

@ -1,23 +1,33 @@
#include "libini.h" #include "libini.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
int main(void) { int main(void)
{
char buf[50]; char buf[50];
getinikeystring("CAT", "age", "config.ini", buf); // 字符串 getinikeystring("CAT", "age", "config.ini", buf); // 字符串
printf("%s\n", 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); 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")); // 整型 putinikeystring("TAC", "e", "\"!@#$%^&*()_+\";", "config.ini"); // 写入
printf("%ld\n", getinikeylong("CAT", "b", "config.ini")); // 长整型
printf("%f\n", getinikeyfloat("CAT", "c", "config.ini")); // 浮点型默认小数点后6位
putinikeystring("TAC", "e", "!@#$%^&*()_+", "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);
getinikeystring("TAC", "f", "config.ini", buf);
printf("%s\n", buf);
memset(buf, 0, 50);
return 1; return 1;
} }