`
steven-zhou
  • 浏览: 207886 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

配置文件操作函数--C语言实现

阅读更多
//config.h
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN  256

struct PNode {
    char    *key;
    char    *value;
    struct PNode *next;
};

typedef struct {
    char    *file;
    struct PNode *first;
} Config;

Config *config_create(char *file);
void config_destroy(Config *config);
char *config_get_value(Config *config, char *key);
void *config_update_value(Config *config, char *key, char *value);
void config_save(Config *config);



//config.c
#include "config.h"

static char set_key_value(struct PNode *p, char *line);
static char *str_trim(char *pStr);
static char *str_trim_quote(char *pStr);

static char buf[MAXLEN];

Config *config_create(char *file)
{
	Config *config = malloc(sizeof(Config));
	config->file = malloc(strlen(file) + 1);
	strcpy(config->file, file);

	struct PNode *pre;
	struct PNode *p;

	FILE *fp;
	if ((fp = fopen(file, "r")) == NULL) {
		if ((fp = fopen(file, "w")) == NULL)
			goto ERR;
	}
	
	if (fgets(buf, MAXLEN, fp) == NULL) {
		fclose(fp);
		return config;
	} else {
		p = malloc(sizeof(struct PNode));
		set_key_value(p, buf);
		p->next = NULL;
		config->first = p;
		pre = p;
	}

	while (fgets(buf, MAXLEN, fp) != NULL) {
		p = malloc(sizeof(struct PNode));
		set_key_value(p, buf);
		p->next = NULL;
		pre->next = p;
		pre = p;
	}
	
	fclose(fp);
	return config;
ERR:
	free(config->file);
	free(config);
	return NULL;
}

void config_destroy(Config *config)
{
	struct PNode *pre = (config == NULL) ? NULL : config->first;
	struct PNode *p = pre;

	while (p != NULL) {
		p = p->next;
		free(pre->key);
		free(pre->value);
		free(pre);
		pre = p;
	}
	free(config->file);
	free(config);
}

void config_list(Config *config)
{
	if (config == NULL)
		return;

	fprintf(stdout, "file: %s\n", config->file);
	struct PNode *p	= config->first;
	int i = 1;
	while (p != NULL) {
		fprintf(stdout, "%d ", i++);
		fprintf(stdout, "key: %s", p->key);
		fprintf(stdout, "\t value: %s\n", p->value);
		p = p->next;
	}
}


char *config_get_value(Config *config, char *key)
{
	struct PNode *p;
	if (config == NULL)
		return NULL;
	
	p = config->first;
	while (p != NULL) {
		if (strcmp(p->key, key) == 0)
			return p->value;
		p = p->next;
	}		
}

void *config_update_value(Config *config, char *key, char *value)
{
	if (config == NULL)
		return;
	
	struct PNode *p = config->first;
	struct PNode *pre = NULL;

	while (p != NULL) {
		if (strcmp(p->key, key) == 0) {
			free(p->value);
			p->value = malloc(strlen(value) + 1);
			strcpy(p->value, value);
			return;
		}
		pre = p;
		p = p->next;
	}

	struct PNode *new = malloc(sizeof(struct PNode));
	new->key = malloc(strlen(key) + 1);
	strcpy(new->key, key);
	new->value = malloc(strlen(value) + 1);
	strcpy(new->value, value);
	new->next = NULL;

	if (pre== NULL)	config->first = new;
	else pre->next = new;
}

void config_save(Config *config)
{
	if (config == NULL)
		return;
	
	char tmpfile[1024];
	sprintf(tmpfile, "%s-bak", config->file);
	
	FILE *fp;
	if ((fp = fopen(tmpfile, "w")) == NULL)
		return;
	
	char buf[1024];
	struct PNode *p;
	for (p = config->first; p != NULL; p = p->next) {
		sprintf(buf, "%s=\"%s\"\n", p->key, p->value);
		fputs(buf, fp);		
	}
	fclose(fp);
	rename(tmpfile, config->file);
}

static char set_key_value(struct PNode *p, char *line)
{
	char *p_equal = strchr(line, '=');
	*p_equal = '\0';

	char *s = str_trim(line);
	p->key = malloc(strlen(s) + 1);
	strcpy(p->key, s);

	s = str_trim(p_equal + 1);
	s = str_trim_quote(s);
	p->value = malloc(strlen(s) + 1);
	strcpy(p->value, s);
}

static char *str_trim(char *pStr)
{
    char *pStart = pStr;
    char *pEnd = pStart + strlen(pStart) - 1;

    while(isspace(*pStart)) pStart++;
    while(isspace(*pEnd))   pEnd--;

    *(pEnd + 1) = '\0';
	return pStart;
}

static char *str_trim_quote(char *pStr)
{
	char *pStart = pStr;
	char *pEnd = pStart + strlen(pStart) - 1;
	if (*pStart == '"')	pStart++;
	if (*pEnd == '"')	pEnd--;
	*(pEnd + 1) = '\0';
	return pStart;
}

int main(int argc, char **argv)
{
	Config *config = config_create("test.conf");
	config_update_value(config, "A", "111");
	config_update_value(config, "B", "222");
	config_update_value(config, "C", "333");
	config_list(config);
	config_save(config);
	config_destroy(config);
	exit(EXIT_SUCCESS);
}



分享到:
评论

相关推荐

    C语言配置文件函数库(附源码)

    C语言配置文件函数库方便广大C语言爱好者!whsvc

    conf-c.zip

    conf-c基于C语言的轻量级读取/创建配置文件的函数库基于C语言的轻量级读取/创建配置文件的函数库基于C语言的轻量级读取/创建配置文件的函数库基于C语言的轻量级读取/创建配置文件的函数库基于C语言的轻量级读取/创建...

    C语言实现的读写配置文件函数,与Windows API函数功能一样

    利用纯C语言实现的配置文件.ini读写函数,可跨平台使用。功能与Windows API函数 WritePrivateProfileString 和GetPrivateProfileString 一样

    C语言实现读写配置文件

    用纯C语言实现配置文件的读写函数,可代替WritePrivateProfileString和GetPrivateProfileString函数. 

    用c语言实现配置文件读写

    用c语言实现配置文件读写,完整的函数,适当的注释,值得的参考!

    c语言实现ini文件读写操作

    C语言实现的INI文件读写操作是软件开发中常用的一种配置管理技术。INI文件以其简单的结构和易于编辑的特性,成为了存储程序设置和配置信息的首选格式。通过使用C语言编写的读写操作函数,开发者可以轻松地在程序中...

    基于C语言的轻量级读取创建配置文件的函数库.rar

    不过一般实现的函数需要传递的参数都有配置文件的路径. 个人认为在某些情况下参数传入 流 重用性更大一点. 本想基于流的参数将 读取, 添加, 删除, 修改 配置文件的函数全部实现. 但发现 删除 , 修改 需要重新打开...

    基于C语言的轻量级读取、创建配置文件的函数库.zip

    基于C语言的轻量级读取、创建配置文件的函数库

    C语言编程实战:文件操作基础,掌握fopen、fprintf、fread、fwrite等核心函数,实现高效数据存储与检索

    例如,在开发数据处理软件、日志系统、配置文件读写等场景中,本资源的内容将发挥重要作用。学习本资源后,你将能够针对具体需求,编写出高效、可靠的文件读写代码。 本资源的特点是讲解清晰、示例丰富,有助于读者...

    方便易用纯c语言日志函数包

     纯c语言日志记录函数  调用简单,支持可变参数  支持配置文件动态调整日志级别

    用C语言读取.ini文件

    在Windows下可以用GetPrivateProfileString或GetPrivateProfileInt方便读取.ini配置文件内容,自己尝试写了一下这个函数。还有一些bug,后面慢慢改。

    日志系统实现及参考demo

    其中 self-syslog 文件夹中包含系统日志源文件,读取配置文件函数,以及用户自定义写日志函数; syslogPrintf 文件夹中主要是打印和 log 日志系统(调用syslog实现)函数的封装; user-demo 文件夹中主要是提供的一个 ...

    用C实现添加和读取配置文件函数

    本篇文章是对用C语言实现添加和读取配置文件函数的方法进行了详细的分析介绍,需要的朋友参考下

    文件读写大挑战:C语言专家教你一招搞定!.zip

    掌握C语言的文件IO操作对于进行数据存储、配置加载、日志记录等任务至关重要。 在C语言编程领域,文件IO(输入/输出)操作是与外部存储介质如硬盘上文件交互的重要环节。通过使用stdio.h头文件中提供的函数,开发者...

    sourceInsight经典配置文件

    sourceInsight经典配置文件,白底,类名绿色,函数名蓝色,赏心悦目

    c语言实战105例源码

    关于C语言一些简单的实例,里面有些思想值得借鉴 1 一个价值“三天”的BUG  2 灵活使用递增(递减)操作符  3 算术运算符计算器  4 逻辑运算符计算器 5 IP地址解析  6 用if…else语句解决奖金发放问题...

    谭浩强C语言全书word版

    10 1.13.5 Run菜单 11 1.13.6 Compile菜单 12 1.13.7 Project菜单 13 1.13.8 Options菜单 14 1.13.9 Debug菜单 18 1.13.10 Break/watch菜单 19 1.13.11 Turbo C 2.0的配置文件 20 C语言教程 C语言概述C语言的发展...

Global site tag (gtag.js) - Google Analytics