The delchar() function removes string quotes and colons
This commit is contained in:
parent
70f4c55c8a
commit
c91f6c093f
@ -2,8 +2,8 @@
|
||||
非常小的读取、写入ini配置文件c库.
|
||||
支持字符串、整形、长整型、浮点型.
|
||||
注释是"//"、"#"、";".
|
||||
会读取等号右边所有字符.
|
||||
支持等号左右有若干空格.
|
||||
不再支持单一右边所有字符,支持两种格式一种是带引号和不带引号的key
|
||||
|
||||
# build
|
||||
git clone https://github.com/niuyuling/libini
|
||||
|
@ -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
|
||||
|
34
libini.c
34
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);
|
||||
}
|
||||
|
||||
|
18
test.c
18
test.c
@ -1,23 +1,33 @@
|
||||
#include "libini.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void) {
|
||||
int main(void)
|
||||
{
|
||||
|
||||
char buf[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);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user