/* -*- Mode: C; tab-width: 4; -*- */ /* * Copyright (C) 2009, HustMoon Studio * * 文件名称:myini.c * 摘 要:读取ini文件+写入ini文件 * 作 者:HustMoon@BYHH * 修 改:2009.10.8 */ #include "myini.h" #include #include #define NOT_COMMENT(c) (c!=';' && c!='#') /* 不是注释行 */ #ifndef strnicmp #define strnicmp strncasecmp #endif static void getLine(const char *buf, int inStart, int *lineStart, int *lineEnd); static int findKey(const char *buf, const char *section, const char *key, int *sectionStart, int *valueStart, unsigned long *valueSize); static int getSection(const char *buf, int inStart); char *loadFile(const char *fileName) { FILE *fp = NULL; long size = 0; char *buf = NULL; if ((fp=fopen(fileName, "rb")) == NULL) return NULL; fseek(fp, 0, SEEK_END); size = ftell(fp); rewind(fp); buf = (char *)malloc(size+1); buf[size] = '\0'; if (fread(buf, size, 1, fp) < 1) { free(buf); buf = NULL; } fclose(fp); return buf; } static void getLine(const char *buf, int inStart, int *lineStart, int *lineEnd) { int start, end; for (start=inStart; buf[start]==' ' || buf[start]=='\t' || buf[start]=='\r' || buf[start]=='\n'; start++); for (end=start; buf[end]!='\r' && buf[end]!='\n' && buf[end]!='\0'; end++); *lineStart = start; *lineEnd = end; } static int findKey(const char *buf, const char *section, const char *key, int *sectionStart, int *valueStart, unsigned long *valueSize) { int lineStart, lineEnd, i; for (*sectionStart=-1, lineEnd=0; buf[lineEnd]!='\0'; ) { getLine(buf, lineEnd, &lineStart, &lineEnd); if (buf[lineStart] == '[') { for (i=++lineStart; i