From 71266f6d68e4132faaf4bf4f76bc7ed80e7a2cbc Mon Sep 17 00:00:00 2001 From: aixiao Date: Thu, 27 Jun 2019 20:45:31 +0800 Subject: [PATCH] Initial submission of source code, no bugs found --- Makefile | 15 ++++++++ README.md | 14 +++++++- config.ini | 7 ++++ libini.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ libini.h | 9 +++++ test.c | 17 +++++++++ 6 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 config.ini create mode 100644 libini.c create mode 100644 libini.h create mode 100644 test.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d5c1790 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CROSS_COMPILE ?= +CC := $(CROSS_COMPILE)gcc +STRIP := $(CROSS_COMPILE)strip +AR := $(CROSS_COMPILE)ar +CFLAGS += -g -Wall +LIBS = libini.so +SLIB = libini.a + +all: libini.o + $(CC) $(CFLAGS) -FPIC -shared $^ -o $(LIBS) + $(AR) -rc $(SLIB) $^ + +clean: + rm -rf *.o + rm $(LIBS) $(SLIB) diff --git a/README.md b/README.md index 2290617..420ff76 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ -# libini \ No newline at end of file +# libini + 非常小的c语言读取、写入ini配置文件c库. + 支持字符串、整形、长整型、浮点型. + 注释是"//"、"#"、";". + 会读取等号右边所有字符. + 支持等号左右有若干空格. + +# build + git clone https://github.com/niuyuling/libini + cd libini + make + gcc -Wall test.c -o test -L./ -lini -static + ./test \ No newline at end of file diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..20f43f8 --- /dev/null +++ b/config.ini @@ -0,0 +1,7 @@ +[CAT] +age=1343567 +name=aixiao.me + +a=10 +b=1234567890 +c=1.1234567 diff --git a/libini.c b/libini.c new file mode 100644 index 0000000..42560bb --- /dev/null +++ b/libini.c @@ -0,0 +1,101 @@ +#include +#include +#include +#include "libini.h" + +int getinikeystring(char *title, char *key, char *filename, char *buf) +{ + FILE *fp; + int flag = 0; + char sTitle[64], *wTmp; + char sLine[1024]; + sprintf(sTitle, "[%s]", title); + + if (NULL == (fp = fopen(filename, "r"))) { + perror("fopen"); + return -1; + } + while (NULL != fgets(sLine, 1024, fp)) { + if (0 == strncmp("//", sLine, 2)) + continue; + if ('#' == sLine[0]) + continue; + if (';' == sLine[0]) + 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, "="))))) { + //printf("%s\n", sLine); + fclose(fp); + while (*(wTmp + 1) == ' ') { + wTmp++; + } + strcpy(buf, wTmp + 1); + return 0; + } + } else { + if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { + flag = 1; + } + } + } + fclose(fp); + return -1; +} + +int putinikeystring(char *title, char *key, char *val, char *filename) +{ + FILE *fpr; + FILE *fpw; + int flag = 0; + char sLine[1024], sTitle[32], *wTmp; + sprintf(sTitle, "[%s]", title); + if (NULL == (fpr = fopen(filename, "r"))) + return -1; + sprintf(sLine, "%s.tmp", filename); + if (NULL == (fpw = fopen(sLine, "w"))) + return -1; + while (NULL != fgets(sLine, 1024, fpr)) { + if (2 != flag) { + wTmp = strchr(sLine, '='); + if ((NULL != wTmp) && (1 == flag)) { + if (0 == strncmp(key, sLine, strlen(key))) { + flag = 2; + sprintf(wTmp + 1, " %s\n", val); + } + } else { + if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { + flag = 1; + } + } + } + fputs(sLine, fpw); + } + fclose(fpr); + fclose(fpw); + sprintf(sLine, "%s.tmp", filename); + return rename(sLine, filename); +} + +int getinikeyint(char *title, char *key, char *filename) +{ + char buf[256]; + getinikeystring(title, key, filename, buf); + return atoi(buf); +} + +long int getinikeylong(char *title, char *key, char *filename) +{ + char buf[256]; + getinikeystring(title, key, filename, buf); + return atol(buf); +} + +float getinikeyfloat(char *title, char *key, char *filename) +{ + char buf[256]; + getinikeystring(title, key, filename, buf); + return atof(buf); +} + diff --git a/libini.h b/libini.h new file mode 100644 index 0000000..10fd6c3 --- /dev/null +++ b/libini.h @@ -0,0 +1,9 @@ +#ifndef 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); + +#endif \ No newline at end of file diff --git a/test.c b/test.c new file mode 100644 index 0000000..582bceb --- /dev/null +++ b/test.c @@ -0,0 +1,17 @@ +#include "libini.h" +#include + +int main(void) { + + char buf[50]; + getinikeystring("CAT", "age", "config.ini", buf); + printf("%s\n", buf); + + getinikeystring("CAT", "name", "config.ini", 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; +} \ No newline at end of file