分享
 
 
 

配置文件的读取,纯C代码

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

C没有这些常用的数据结构,只好自己写一个简单的存储结构了:

<p>

/*****************************strlist.h***************************/

#ifndef DS_STRING_LIST_H_

#define DS_STRING_LIST_H_

struct strlist_node {

char key_[64]; //Store the data as key

char value_[64]; //the value

struct strlist_node* next;

//struct strlist* prev;

};

struct strlist {

int size_;

struct strlist_node* head_;

};

struct strlist* strlist_create ();

void strlist_destroy (struct strlist* list);

int strlist_add (struct strlist* list, char const* d);

int strlist_add2 (struct strlist* list, char const* key, char const* value);

int strlist_remove (struct strlist* list, const char* key);

int strlist_size (struct strlist* list);

void strlist_clear (struct strlist* list);

///Get value of special item, if it is not existed return NULL

char const* strlist_value (struct strlist* list, char const* key);

#ifdef _DEBUG

void strlist_print ();

#endif

#endif

</p>

<p>

/****************************strlist.c******************************/

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include "strlist.h"

#include "common.h" ///define TRACE macro

struct strlist* strlist_create ()

{

struct strlist* list = (struct strlist*) malloc (sizeof (struct strlist));

list->size_ = 0;

list->head_ = NULL;

return list;

}

void strlist_destroy (struct strlist* list)

{

strlist_clear (list);

free (list);

}

int strlist_add (struct strlist* list, char const* d)

{

char* key = NULL;

char* ret = NULL;

if (NULL == list)

return 0;

ret = strchr (d, ':');

if (ret == d || ret == NULL || *(++ret) == 0) ///Invalid datum format ignore it

return 0;

key = (char*) malloc (ret - d);

strncpy (key, d, ret - d - 1);

strlist_remove (list, key);

struct strlist_node* node = (struct strlist_node*) malloc (sizeof (struct strlist_node));

memset (node->key_, 0, 64);

memset (node->value_, 0, 64);

strncpy (node->key_, key, ret - d - 1);

strcpy (node->value_, ret);

TRACEN ("\nAdd key : %s\nand value: %s\n", node->key_, node->value_);

node->next = NULL;

free (key);

if (NULL == list->head_)

{

list->head_ = node;

list->size_ = 1;

return list->size_;

}

node->next = list->head_;

list->head_ = node;

++(list->size_);

return list->size_;

}

int strlist_add2 (struct strlist* list, char const* key, char const* value)

{

if (NULL == list)

return 0;

strlist_remove (list, key);

struct strlist_node* node = (struct strlist_node*) malloc (sizeof (struct strlist_node));

memset (node->key_, 0, 64);

memset (node->value_, 0, 64);

strcpy (node->key_, key);

strcpy (node->value_, value);

node->next = NULL;

if (NULL == list->head_)

{

list->head_ = node;

list->size_ = 1;

return list->size_;

}

node->next = list->head_;

list->head_ = node;

++(list->size_);

return list->size_;

}

int strlist_remove (struct strlist* list, char const* key)

{

if (NULL == list || NULL == list->head_)

return -1;

struct strlist_node* work = list->head_;

struct strlist_node* prev = work;

while (work != NULL)

{

if (strcmp (work->key_, key) == 0)

{

break;

}

prev = work;

work = work->next;

}

if (NULL == work)

return -1;

prev->next = work->next;

if (work == list->head_)

list->head_ = work->next;

free (work);

return --(list->size_);

}

int strlist_size (struct strlist* list)

{

return list->size_;

}

void strlist_clear (struct strlist* list)

{

struct strlist_node* work = list->head_;

while (work != NULL)

{

TRACE;

struct strlist_node* p = work;

work = work->next;

free (p);

}

list->head_ = NULL;

list->size_ = 0;

}

char const* strlist_value (struct strlist* list, char const* key)

{

if (NULL == list || NULL == list->head_)

return NULL;

struct strlist_node* work = list->head_;

while (work != NULL)

{

if (strcmp (work->key_, key) == 0)

{

return work->value_;

}

work = work->next;

}

return NULL;

}

#ifdef _DEBUG

void strlist_print (struct strlist* list)

{

TRACE;

if (NULL == list || NULL == list->head_)

return;

fprintf (stderr, "list's size is %d\n", list->size_);

struct strlist_node* work = list->head_;

while (work != NULL)

{

fprintf (stderr, "%s = %s\n", work->key_, work->value_);

work = work->next;

}

}

#endif

</p>

下面是读取,解析文件部分

<p>

/*******************read_config.h********************/

#ifndef DS_CONFIG_HANDLE_H_

#define DS_CONFIG_HANDLE_H_

#include <stdlib.h>

int config_load (char const* filename);

void config_clean ();

char const* config_string (char const* section, char const* key);

int config_integer (char const* section, char const* key);

float config_float (char const* section, char const* key);

//int config_save (char const* filename);

#endif //DS_CONFIG_HANDLE_H_

</p>

<p>

/***********************read_config.c************************/

#include "config_reader.h"

#include "strlist.h"

#include <stdio.h>

#include <string.h>

#include "common.h"

struct strlist* config_list = NULL;

char section[32];

int process_line (char key[], char value[], char const* line);

int config_load (char const* filename)

{

char* buf_ = NULL;

int read_ = 0;

int size_ = 256;

char* config_pos;

char* config_offset;

char line[100];

char key[32];

char value[64];

int count = 0;

if (NULL == filename)

return -1;

FILE* file = fopen (filename, "r");

if (NULL == file)

return -1;

///Read file into memory

do {

if (NULL != buf_)

free (buf_);

size_ *= 2;

buf_ = (char*) malloc (size_);

rewind (file);

read_ = fread (buf_, 1, size_, file);

} while (read_ >= size_);

TRACE1 ("Read the file finished");

fclose (file);

config_pos = buf_;

config_offset = buf_;

///parse the config file now

config_list = strlist_create ();

while (*config_offset != 0)

{

if (*config_pos == '\n' || *config_pos == 0)

{

memset (line, 0, sizeof (line));

strncpy (line, config_offset, config_pos - config_offset);

TRACE1 (line);

if (process_line (key, value, line))

{

TRACE;

++count;

char tk[64];

sprintf (tk, "%s-%s", section, key);

strlist_add2 (config_list, tk, value);

}

config_offset = config_pos + 1;

}

++config_pos;

}

free (buf_);

#ifdef _DEBUG

strlist_print (config_list);

#endif

return count;

}

int process_line (char key[], char value[], char const* line)

{

char buf[100];

char* r;

char* w;

memset (buf, 0, sizeof (buf));

strcpy (buf, line);

memset (key, 0, 32);

memset (value, 0, 64);

r = buf;

while (*r != 0)

{

if (*r == ';')

break;

++r;

}

while (*r != 0)

*r++ = 0;

r = buf;

w = key;

while (*r != 0)

{

if (*r == '=')

break;

//if (*r = ';' && w == key) ///If the first non space character is ';', the line is comment

//return 0;

if (*r != ' ')

*w++ = *r;

++r;

}

TRACE1 (key);

--w;

if (*key == '[' && *(key + strlen (key) - 1) == ']')

{

memset (section, 0, 64);

w = key;

strncpy (section, key + 1, strlen (key) - 2);

return 0;

}

++r;

while (*r != 0)

{

if (*r == ' ')

++r;

else

break;

}

strcpy (value, r);

if (strlen (key) > 0 && strlen (value) > 0)

return 1;

return 0;

}

void config_clean ()

{

strlist_destroy (config_list);

}

char const* config_string (char const* section, char const* key)

{

char tk[64];

sprintf (tk, "%s-%s", section, key);

return strlist_value (config_list, tk);

}

int config_integer (char const* section, char const* key)

{

char tk[64];

sprintf (tk, "%s-%s", section, key);

return atoi (strlist_value (config_list, tk));

}

float config_float (char const* section, char const* key)

{

char tk[64];

sprintf (tk, "%s-%s", section, key);

return atof (strlist_value (config_list, tk));

}

//int config_save (char const* filename)

//{

///懒,不写了,^_^

//}

</p>

在Linux和Windows下编译测试通过

如果让偶不小心拷贝了份不能正常工作的版本上了,表丢砖头,告诉偶,偶改还不行嘛

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有