optimization

This commit is contained in:
aixiao 2019-11-22 18:53:51 +08:00
parent 3bf9e8941f
commit 3846c36587
5 changed files with 40 additions and 21 deletions

View File

@ -3,7 +3,7 @@
支持字符串、整形、长整型、浮点型.
注释是"//"、"#"、";".
支持等号右有若干空格.
不再支持单一右边所有字符,支持两种格式一种是带引号和不带引号的key
支持单一右边所有字符,支持两种格式一种是带引号和不带引号的key
# build
git clone https://github.com/niuyuling/libini

View File

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

View File

@ -42,8 +42,30 @@ char *del_space(char *str, char *buf)
return buf;
}
// 创建临时内存
int libini_memory(char **pp)
{
if (pp != NULL) {
char *tmp = (char *)malloc(BUFFER_SIZE);
if (tmp != NULL) {
*pp = tmp; // pp的地址指向tmp
return 1;
}
}
return -1;
}
// 释放临时内存
void libini_free(char *p)
{
if (p != NULL) {
free(p);
}
}
int getinikeystring(char *title, char *key, char *filename, char *buf)
{
bzero(buf, 0);
FILE *fp;
int flag = 0;
char sTitle[BUFFER_SIZE], *wTmp;

View File

@ -1,9 +1,12 @@
#ifndef LIBINI_H
#define LIBINI_H
int getinikeystring(char *title, char *key, char *filename, char *buf);
int putinikeystring(char *title, char *key, char *val, char *filename);
int getinikeyint(char *title, char *key, char *filename);
long getinikeylong(char *title, char *key, char *filename);
float getinikeyfloat(char *title, char *key, char *filename);
int libini_memory(char **pp);
void libini_free(char *p);
#endif

31
test.c
View File

@ -4,33 +4,26 @@
int main(void)
{
char buf[50];
memset(buf, 0, 50);
getinikeystring("CAT", "age", "config.ini", buf); // 字符串
printf("%s\n", buf);
char *tmp = NULL;
libini_memory(&tmp); // 创建内存
memset(buf, 0, 50);
getinikeystring("CAT", "name", "config.ini", buf); // 字符串
printf("%s\n", buf);
memset(buf, 0, 50);
getinikeystring("CAT", "age", "config.ini", tmp); // 字符串
printf("%s\n", tmp);
getinikeystring("CAT", "name", "config.ini", tmp); // 字符串
printf("%s\n", tmp);
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", " abcdef!@#$%^&*()_+", "config.ini"); // 写入
getinikeystring("TAC", "e", "config.ini", buf);
printf("%s\n", buf);
memset(buf, 0, 50);
getinikeystring("TAC", "e", "config.ini", tmp);
printf("%s\n", tmp);
getinikeystring("TAC", "f", "config.ini", buf);
printf("%s\n", buf);
memset(buf, 0, 50);
getinikeystring("TAC", "f", "config.ini", tmp);
printf("%s\n", tmp);
printf("%d\n", getinikeystring("CAT", "age", "config.ini", buf));
libini_free(tmp);
return 1;
}