配置文件操作函数--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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值