Repair putinikeystring() function
This commit is contained in:
parent
578ee44515
commit
a793745ce4
@ -9,5 +9,7 @@
|
|||||||
git clone https://github.com/niuyuling/libini
|
git clone https://github.com/niuyuling/libini
|
||||||
cd libini
|
cd libini
|
||||||
make
|
make
|
||||||
|
|
||||||
|
# test
|
||||||
gcc -Wall test.c -o test -L./ -lini -static
|
gcc -Wall test.c -o test -L./ -lini -static
|
||||||
./test
|
./test
|
@ -4,4 +4,8 @@ name=aixiao.me
|
|||||||
|
|
||||||
a=10
|
a=10
|
||||||
b=1234567890
|
b=1234567890
|
||||||
c=1.1234567
|
c=1.223445446
|
||||||
|
|
||||||
|
|
||||||
|
[TAC]
|
||||||
|
e=!@#$%^&*()_+
|
||||||
|
22
libini.c
22
libini.c
@ -3,19 +3,21 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "libini.h"
|
#include "libini.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
int getinikeystring(char *title, char *key, char *filename, char *buf)
|
int getinikeystring(char *title, char *key, char *filename, char *buf)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
int flag = 0;
|
int flag = 0;
|
||||||
char sTitle[64], *wTmp;
|
char sTitle[BUFFER_SIZE], *wTmp;
|
||||||
char sLine[1024];
|
char sLine[BUFFER_SIZE];
|
||||||
sprintf(sTitle, "[%s]", title);
|
sprintf(sTitle, "[%s]", title);
|
||||||
|
|
||||||
if (NULL == (fp = fopen(filename, "r"))) {
|
if (NULL == (fp = fopen(filename, "r"))) {
|
||||||
perror("fopen");
|
perror("fopen");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
while (NULL != fgets(sLine, 1024, fp)) {
|
while (NULL != fgets(sLine, BUFFER_SIZE, fp)) {
|
||||||
if (0 == strncmp("//", sLine, 2))
|
if (0 == strncmp("//", sLine, 2))
|
||||||
continue;
|
continue;
|
||||||
if ('#' == sLine[0])
|
if ('#' == sLine[0])
|
||||||
@ -49,18 +51,20 @@ int putinikeystring(char *title, char *key, char *val, char *filename)
|
|||||||
FILE *fpr;
|
FILE *fpr;
|
||||||
FILE *fpw;
|
FILE *fpw;
|
||||||
int flag = 0;
|
int flag = 0;
|
||||||
char sLine[1024], sTitle[32], *wTmp;
|
char sLine[BUFFER_SIZE], sTitle[BUFFER_SIZE], *wTmp;
|
||||||
|
char sLine_backup[BUFFER_SIZE];
|
||||||
sprintf(sTitle, "[%s]", title);
|
sprintf(sTitle, "[%s]", title);
|
||||||
if (NULL == (fpr = fopen(filename, "r")))
|
if (NULL == (fpr = fopen(filename, "r")))
|
||||||
return -1;
|
return -1;
|
||||||
sprintf(sLine, "%s.tmp", filename);
|
sprintf(sLine, "%s.tmp", filename);
|
||||||
if (NULL == (fpw = fopen(sLine, "w")))
|
if (NULL == (fpw = fopen(sLine, "w")))
|
||||||
return -1;
|
return -1;
|
||||||
while (NULL != fgets(sLine, 1024, fpr)) {
|
while (NULL != fgets(sLine, BUFFER_SIZE, fpr)) {
|
||||||
if (2 != flag) {
|
if (2 != flag) {
|
||||||
wTmp = strchr(sLine, '=');
|
wTmp = strchr(sLine, '=');
|
||||||
if ((NULL != wTmp) && (1 == flag)) {
|
if ((NULL != wTmp) && (1 == flag)) {
|
||||||
if (0 == strncmp(key, sLine, strlen(key))) {
|
strcpy(sLine_backup, sLine);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
@ -80,21 +84,21 @@ int putinikeystring(char *title, char *key, char *val, char *filename)
|
|||||||
|
|
||||||
int getinikeyint(char *title, char *key, char *filename)
|
int getinikeyint(char *title, char *key, char *filename)
|
||||||
{
|
{
|
||||||
char buf[256];
|
char buf[BUFFER_SIZE];
|
||||||
getinikeystring(title, key, filename, buf);
|
getinikeystring(title, key, filename, buf);
|
||||||
return atoi(buf);
|
return atoi(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
long int getinikeylong(char *title, char *key, char *filename)
|
long int getinikeylong(char *title, char *key, char *filename)
|
||||||
{
|
{
|
||||||
char buf[256];
|
char buf[BUFFER_SIZE];
|
||||||
getinikeystring(title, key, filename, buf);
|
getinikeystring(title, key, filename, buf);
|
||||||
return atol(buf);
|
return atol(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
float getinikeyfloat(char *title, char *key, char *filename)
|
float getinikeyfloat(char *title, char *key, char *filename)
|
||||||
{
|
{
|
||||||
char buf[256];
|
char buf[BUFFER_SIZE];
|
||||||
getinikeystring(title, key, filename, buf);
|
getinikeystring(title, key, filename, buf);
|
||||||
return atof(buf);
|
return atof(buf);
|
||||||
}
|
}
|
||||||
|
16
test.c
16
test.c
@ -4,14 +4,20 @@
|
|||||||
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);
|
||||||
|
getinikeystring("CAT", "name", "config.ini", buf); // 字符串
|
||||||
printf("%s\n", buf);
|
printf("%s\n", buf);
|
||||||
|
|
||||||
getinikeystring("CAT", "name", "config.ini", buf);
|
|
||||||
|
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"); // 写入
|
||||||
|
getinikeystring("TAC", "e", "config.ini", buf);
|
||||||
printf("%s\n", buf);
|
printf("%s\n", buf);
|
||||||
|
|
||||||
printf("%d\n", getinikeyint("CAT", "a", "config.ini"));
|
|
||||||
printf("%ld\n", getinikeylong("CAT", "b", "config.ini"));
|
|
||||||
printf("%f\n", getinikeyfloat("CAT", "c", "config.ini"));
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user