--- /dev/null
+/*
+ * modified port of Cini project
+ * original by:
+ * liukui,liukee2009@foxmail.com
+*/
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include "ini.h"
+
+#define CHECK_FILE(f) if((f)==NULL) return FILE_NO_FILE
+
+typedef struct FIELD {
+ struct FIELD *fNext;
+ char FieldName[FIELDNAME_LEN];
+ char FieldValue[FIELDVALUE_LEN];
+} FIELD;
+typedef struct tagSECTION {
+ struct tagSECTION *sNext;
+ char SectionName[SECTION_LEN];
+ FIELD *fNext;
+} SECTION;
+
+static SECTION *g_INIlist = NULL;
+
+static void removeSpace(char *str) {
+ int i;
+ int start=0;
+ int end=strlen(str);
+ while(0x20 == str[start])start++;
+ for(i=start;i<end;i++) {
+ str[i-start]=str[i];
+ }
+ for(i=end-1;i>=0;i--) {
+ if (str[i] == 0x20) {
+ str[i]='\0';
+ } else
+ break;
+ }
+}
+
+void WriteString(char *SectionName, char *FieldName, char *FieldValue) {
+ SECTION *sect = NULL, *preSect = NULL;
+ FIELD *Field = NULL, *preField = NULL;
+
+ if (!g_INIlist)
+ {
+ sect = malloc(sizeof(*sect));
+ memset(sect,0,sizeof(*sect));
+ strcpy(sect->SectionName, SectionName);
+ g_INIlist = sect;
+ } else {
+ sect = g_INIlist;
+ while(sect) {
+ if(!strcmp(sect->SectionName, SectionName))
+ break;
+ preSect = sect;
+ sect = sect->sNext;
+ }
+ if (!sect) {
+ sect = malloc(sizeof(*sect));
+ memset(sect,0,sizeof(*sect));
+ strcpy(sect->SectionName, SectionName);
+ preSect->sNext = sect;
+ }
+ }
+ if (!sect->fNext) {
+ Field = malloc(sizeof(*Field));
+ memset(Field,0,sizeof(*Field));
+ strcpy(Field->FieldName, FieldName);
+ sect->fNext = Field;
+ } else {
+ Field = sect->fNext;
+ while(Field) {
+ if(!strcmp(Field->FieldName, FieldName))
+ break;
+ preField=Field;
+ Field=Field->fNext;
+ }
+
+ if (!Field) {
+ Field = malloc(sizeof(*Field));
+ memset(Field,0,sizeof(*Field));
+ strcpy(Field->FieldName, FieldName);
+ preField->fNext = Field;
+ }
+ }
+ strcpy(Field->FieldValue, FieldValue);
+ return;
+}
+
+/* 0:ok;1:no section;2:no filename; */
+void ReadString(char *SectionName,char *FieldName,char *FieldValue) {
+ SECTION *sect = NULL;
+ FIELD *Field = NULL;
+ if (NULL == g_INIlist)
+ return;
+ sect=g_INIlist;
+ while(sect) {
+ if(!strcmp(sect->SectionName, SectionName))
+ break;
+ sect = sect->sNext;
+ }
+ if (!sect)
+ return;
+ if (!sect->fNext)
+ return;
+ Field = sect->fNext;
+ while(Field) {
+ if(!strcmp(Field->FieldName, FieldName))
+ break;
+ Field = Field->fNext;
+ }
+ if (!Field)
+ return;
+ strcpy(FieldValue, Field->FieldValue);
+ return;
+}
+
+int loadINI(char *filename) {
+ FILE *f;
+ int i, fpointer;
+ char SectionName[SECTION_LEN], FieldName[FIELDNAME_LEN], FieldValue[FIELDVALUE_LEN], ch;
+
+ f=fopen(filename,"rb");
+ CHECK_FILE(f);
+
+ while(!feof(f)) {
+ while(!feof(f)) {
+ fread(&ch,sizeof(char),1,f);
+ if (ch == '[')
+ break;
+ }
+ i=0;
+ while(!feof(f))
+ {
+ fread(&ch,sizeof(char),1,f);
+ if (ch != ']')
+ SectionName[i++] = ch;
+ else {
+ SectionName[i]='\0';
+ break;
+ }
+ }
+ removeSpace(SectionName);
+ while(!feof(f)) {
+ fread(&ch,sizeof(char),1,f);
+ if (ch == 0x0d)
+ break;
+ }
+ fread(&ch,sizeof(char),1,f);
+ if (SectionName[0] == '\0')
+ continue;
+ while(!feof(f)) {
+ while(!feof(f)) {
+ fpointer = ftell(f);
+ fread(&ch,sizeof(char),1,f);
+ if (isalnum(ch) || ch=='[')
+ break;
+ }
+ if(feof(f))
+ break;
+ if (ch=='[') {
+ fseek(f,fpointer,SEEK_SET);
+ break;
+ }
+ i=0;
+ FieldName[i++] = ch;
+ while(!feof(f))
+ {
+ fread(&ch,sizeof(char),1,f);
+ if (ch != '=')
+ FieldName[i++] = ch;
+ else {
+ FieldName[i] = '\0';
+ break;
+ }
+ }
+ removeSpace(FieldName);
+ if (FieldName[0]=='\0')
+ continue;
+ i=0;
+ while(!feof(f)) {
+ fread(&ch,sizeof(char),1,f);
+ if (ch != '\r' && ch != '\n')
+ FieldValue[i++] = ch;
+ else {
+ FieldValue[i] = '\0';
+ break;
+ }
+ }
+ removeSpace(FieldValue);
+ fread(&ch,sizeof(char),1,f);
+ WriteString(SectionName,FieldName,FieldValue);
+ }
+ }
+ fclose(f);
+ return FILE_SUCCESS;
+}
+
+int saveINI(char *filename) {
+ SECTION *sect = NULL, *preSect;
+ FIELD *Field = NULL, *preField;
+ char *pSBracket = "[", *pEBracket="]", *peq="=", *pend="\r\n";
+
+ if (NULL == g_INIlist)
+ return FILE_SUCCESS;
+ FILE *f = fopen(filename,"wb");
+ CHECK_FILE(f);
+ sect = g_INIlist;
+ while(sect) {
+ preSect = sect;
+ fwrite(pSBracket,1,strlen(pSBracket),f);
+ fwrite(&(preSect->SectionName),1,strlen((char *)&(preSect->SectionName)),f);
+ fwrite(pEBracket,1,strlen(pEBracket),f);
+ fwrite(pend,1,strlen(pend),f);
+ if (NULL != sect->fNext) {
+ Field = sect->fNext;
+ while(Field) {
+ preField = Field;
+ fwrite(&(preField->FieldName),1,strlen(preField->FieldName),f);
+ fwrite(peq,1,strlen(peq),f);
+ fwrite(&(preField->FieldValue),1,strlen(preField->FieldValue),f);
+ fwrite(pend,1,strlen(pend),f);
+ Field = Field->fNext;
+ free(preField);
+ }
+ }
+ sect=sect->sNext;
+ free(preSect);
+ }
+ fflush(f);
+ fclose(f);
+ g_INIlist = NULL;
+ return FILE_SUCCESS;
+}
+
+#ifdef __cplusplus
+}
+#endif
\ No newline at end of file