#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "ini.h"
#include "queue.h"
#include "_string_.h"
typedef struct ini_item_t {
struct ini_item_t *prev;
struct ini_item_t *next;
char *name;
char *value;
} INIITEM, *LPINIITEM;
typedef struct ini_section_t {
struct ini_section_t *prev;
struct ini_section_t *next;
char *name;
int itemCount;
struct ini_item_t *first_item;
struct ini_item_t *last_item;
} INISECTION, *LPINISECTION;
typedef struct ini_file_t {
char *name;
int sectionCount;
struct ini_section_t *first_section;
struct ini_section_t *last_section;
int flag;
char mode[8];
} INIFILE, *LPINIFILE;
static LPINIITEM ini_addItem(LPINISECTION iniSection, const char *name, const char *value)
{
LPINIITEM item = (LPINIITEM)malloc(sizeof(INIITEM));
item->name = (char *)malloc(strlen(name) + 1);
strcpy(item->name, name);
item->value = (char *)malloc(strlen(value) + 1);
strcpy(item->value, value);
add_node((void**)&(iniSection->first_item), (void**)&(iniSection->last_item), item);
iniSection->itemCount++;
return item;
}
static void ini_delItem(LPINISECTION iniSection, LPINIITEM item)
{
free(item->name);
free(item->value);
del_node((void**)&(iniSection->first_item), (void**)&(iniSection->last_item), item);
free(item);
iniSection->itemCount--;
}
static LPINISECTION ini_addSection(LPINIFILE iniFile, const char *name)
{
LPINISECTION sec = (LPINISECTION)malloc(sizeof(INISECTION));
sec->name = (char *)malloc(strlen(name) + 1);
strcpy(sec->name, name);
sec->first_item = NULL;
sec->last_item = NULL;
sec->itemCount = 0;
add_node((void**)&(iniFile->first_section), (void**)&(iniFile->last_section), sec);
return sec;
}
static void ini_delSection(LPINIFILE iniFile, LPINISECTION sec)
{
LPINIITEM item = sec->first_item;
free(sec->name);
while(item)
{
LPINIITEM tmp = item->next;
ini_delItem(sec, item);
item = tmp;
}
del_node((void**)&(iniFile->first_section), (void**)&(iniFile->last_section), sec);
free(sec);
}
static LPINIFILE ini_createIniStruct(const char *name)
{
LPINIFILE iniFile = (LPINIFILE)malloc(sizeof(INIFILE));
iniFile->name = (char *)malloc(strlen(name) + 1);
strcpy(iniFile->name, name);
iniFile->first_section = NULL;
iniFile->last_section = NULL;
iniFile->sectionCount = 0;
iniFile->flag = 0;
return iniFile;
}
static void ini_destryIniStruct(LPINIFILE iniFile)
{
LPINISECTION sec = iniFile->first_section;
free(iniFile->name);
while(sec)
{
LPINISECTION tmp = sec->next;
ini_delSection(iniFile, sec);
sec = tmp;
}
free(iniFile);
}
static void ini_writeItem(FILE *fp, LPINIITEM item)
{
fprintf(fp, "%s=%s\n", item->name, item->value);
}
static void ini_writeSection(FILE *fp, LPINISECTION sec)
{
LPINIITEM item = sec->first_item;
fprintf(fp, "[%s]\n", sec->name);
while(item)
{
ini_writeItem(fp, item);
item = item->next;
}
fprintf(fp, "\n");
}
static LPINIITEM ini_searchItem(LPINISECTION sec, const char *name)
{
LPINIITEM item;
if(sec == NULL)
return NULL;
item = sec->first_item;
while(item)
{
if(strcmp(item->name, name) == 0)
return item;
item = item->next;
}
return NULL;
}
static LPINISECTION ini_searchSection(LPINIFILE iniFile, const char *name)
{
LPINISECTION sec;
if(iniFile == NULL)
return NULL;
sec = iniFile->first_section;
while(sec)
{
if(strcmp(sec->name, name) == 0)
return sec;
sec = sec->next;
}
return NULL;
}
INIFILEHANDLE ini_open(const char *iniFile, const char *mode)
{
char lineBuf[1024];
LPINIFILE file;
LPINISECTION curSection = NULL;
FILE *fp;
fp = fopen(iniFile, mode);
if(fp == NULL)
return NULL;
file = ini_createIniStruct(iniFile);
strcpy(file->mode, mode);
while(fgets(lineBuf, 1023, fp))
{
char *p = strtrim(lineBuf);
int len = strlen(p);
if(strlen(p) <= 0)
continue;
else if(p[0] == '#')
continue;
else if((p[0] == '[') && (p[len - 1] == ']'))
{
p[len - 1] = '\0';
p = strtrim(&p[1]);
curSection = ini_addSection(file, p);
}
else
{
char *p2 = strchr(lineBuf, '=');
if(p2 == NULL)
{
ini_destryIniStruct(file);
file = NULL;
break;
}
*p2++ = '\0';
p = strtrim(p);
p2 = strtrim(p2);
ini_addItem(curSection, p, p2);
}
}
fclose(fp);
return (INIFILEHANDLE)file;
}
int ini_SaveTo(INIFILEHANDLE ini, FILE *fp)
{
LPINIFILE iniFile = (LPINIFILE)ini;
LPINISECTION sec = iniFile->first_section;
if(fp == NULL)
return -1;
fprintf(fp, "# %s\n", iniFile->name);
while(sec)
{
ini_writeSection(fp, sec);
sec = sec->next;
}
return 0;
}
int ini_saveAs(INIFILEHANDLE ini, const char *saveFile)
{
LPINIFILE iniFile = (LPINIFILE)ini;
const char *filename = saveFile;
FILE *fp;
int ret;
if(saveFile == NULL)
{
if(strchr(iniFile->mode, 'w') == 0)
return -1;
filename = iniFile->name;
}
fp = fopen(filename, "w+");
if(fp == NULL)
return -1;
ret = ini_SaveTo(ini, fp);
fclose(fp);
return ret;
}
void ini_close(INIFILEHANDLE ini)
{
LPINIFILE iniFile = (LPINIFILE)ini;
if((iniFile->flag) && (strchr(iniFile->mode, 'w')))
ini_saveAs(ini, NULL);
ini_destryIniStruct(iniFile);
}
char *ini_ReadString(INIFILEHANDLE ini, const char *secName, const char *itemName, char *outBuf, const char *Default)
{
LPINIITEM item = ini_searchItem(ini_searchSection((LPINIFILE)ini, secName), itemName);
if(item == NULL)
{
if(outBuf == NULL)
return (char *)Default;
return strcpy(outBuf, Default);
}
if(outBuf == NULL)
return item->value;
return strcpy(outBuf, item->value);
}
int ini_ReadInt(INIFILEHANDLE ini, const char *secName, const char *itemName, int Default)
{
LPINIITEM item = ini_searchItem(ini_searchSection((LPINIFILE)ini, secName), itemName);
if(item == NULL)
return Default;
return atoi(item->value);
}
int ini_WriteString(INIFILEHANDLE ini, const char *secName, const char *itemName, const char *value)
{
LPINIFILE iniFile = (LPINIFILE)ini;
LPINISECTION sec;
LPINIITEM item;
iniFile->flag = 1;
sec = ini_searchSection(iniFile, secName);
if(sec == NULL)
{
sec = ini_addSection(iniFile, secName);
item = ini_addItem(sec, itemName, value);
}
else
{
item = ini_searchItem(sec, itemName);
if(item == NULL)
{
item = ini_addItem(sec, itemName, value);
}
else
{
free(item->value);
item->value = (char *)malloc(strlen(value) + 1);
strcpy(item->value, value);
}
}
return 0;
}
int ini_WriteInt(INIFILEHANDLE ini, const char *secName, const char *itemName, int Value)
{
char tmpString[16] = "";
sprintf(tmpString, "%d", Value);
return ini_WriteString(ini, secName, itemName, tmpString);
}
一个用标准C写的ini文件的操作程序
4星 · 超过85%的资源 需积分: 15 55 浏览量
2009-03-19
10:46:11
上传
评论 3
收藏 2KB RAR 举报
leajian
- 粉丝: 11
- 资源: 79
最新资源
- 【创新创业材料】某服装进入XX市市场可行性报告 .zip
- 【创新创业材料】某市图书馆建设项目可行性研究报告.zip
- 【创新创业材料】某医疗器械股份有限公司核心产品国内外营销网络建设项目可行性研究报告-优秀甲级资质可研报告.zip
- 【创新创业材料】某化工公司氨基酸原料及磷化工产品生产项目可行性研究报告-极品推荐140页优秀甲级资质可研报告.zip
- 质量运营与模式i建构分享 - 转PDF.pdf
- JAVA中Spring框架入门与实践心得.zip
- 【创新创业材料】泸州老窖中长期发展战略.zip
- 【创新创业材料】旅游地产:山东胶南小珠山旅游&文化创意地产项目可行性研究报告2008-138页.zip
- main.c
- Unity 创建快捷方式自动启动
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页