【工作总结】一组关于key=value类似配置文件的读写操作函数

原创 2012年04月05日 22:14:48

工作中经常会碰到解析配置文件的时候,经常会碰到,这里记录下来,用于常用。

配置文件格式如下:

#key1

key1=value1

#key2

key2=value2

...


对应函数如下,很简单。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>


//if 1, os_printf will print all message
//esle,will not.
#if 1
#define os_printf(...) \
{\
    printf("\n########file:%s, fun:%s(), line:%d\n",__FILE__,__FUNCTION__,__LINE__);\
    printf(__VA_ARGS__);\
    printf("\r\n");\
    fflush(stdout);\
}
#else
#define os_printf(...)\
{\
}
#endif
//end.

#define INCREASEMENT 50

//Added by XYang 2011-08-11 
// All configure file will set to this one file.
#define DEVICE_CFG_FILE "/mnt/device.conf"

typedef int (*pFunc)(char *, void *);

int myGetStringFunc(char *pos, void *buf){
    int len = strlen(pos);
    if(pos[--len] == '\n'){
        pos[len] = '\0';
    }

    if(strcpy((char *)buf,pos) == buf){
        return 0;
    }

    return 1;
}

int myGetIntFunc(char *pos, void *value){
    char *pPos = pos;
    int  *pDest = (int *)value;

    if(sscanf(pPos,"%d",pDest) == 1){
        return 0;
    }

    return 1;
}

int mySetStringFunc(char *buf, void *value){
    char *pDest = buf;
    char *pVal = (char *)value;
    int  lenChange = 0;
    int  lenOfVal  = 0;

    lenOfVal = strlen(pVal) + 1;
    lenChange = strlen(pDest) - lenOfVal;

    if(sprintf(pDest,"%s\n",pVal) != -1){
        if( lenChange > 0 ){
            memset(pDest+lenOfVal,0,lenChange);
        }
        return 0;
    }

    return 1;
}

int mySetIntFunc(char *buf, void *value){
    char *pDest = buf;
    int  data = (int)value;

    if(sprintf(pDest,"%d\n",data) != -1){
        return 0;
    }

    return 1;
}

int SysParamSetFunc(FILE *fd, char *key, void *value, pFunc myfunc){
    FILE  *fp = fd;
    char  *pBuf = NULL;
    char  *pStartOfBuf = NULL;
    char  *pPosOfKey = NULL;
    int   fileLength = 0;
    int   bufLength = 0;
    int   lenChange = 0;
    int   retVal = 0;

    do{
        if(fseek(fd,0,SEEK_END) != 0){
            retVal = 2;
            break;
        }

        fileLength = ftell(fd);
        if(fileLength == -1){
            retVal = 4;
            break;
        }

        if(fseek(fd,0,SEEK_SET) != 0){
            retVal = 2;
            break;
        }

        bufLength = fileLength + INCREASEMENT;
        pBuf = (char *)malloc(bufLength);
        if(pBuf == NULL){
            retVal = 8;
            break;
        }
        memset(pBuf,0,bufLength);
        pStartOfBuf = pBuf;

        while(!feof(fp)){
            pPosOfKey = fgets(pStartOfBuf,bufLength,fp);
            if(pPosOfKey == pStartOfBuf)
            {
                if( (retVal&0x01) == 0x00 ){
                    while(*pPosOfKey == ' ')
                    {
                        ++pPosOfKey;
                    }

                    if(*pPosOfKey != '#')
                    {
                        if(strstr(pPosOfKey,key) == pPosOfKey)
                        {
                            pPosOfKey += strlen(key);

                            if(*pPosOfKey==' ' || *pPosOfKey==':')
                            {
                                retVal = 1;
                                while(*pPosOfKey==' ' || *pPosOfKey==':')
                                {
                                    ++pPosOfKey;
                                }

                                lenChange = strlen(pPosOfKey);
                                if(myfunc(pPosOfKey,value) != 0){
                                    retVal = 17;
                                }

                                lenChange = lenChange - strlen(pPosOfKey);
                            }
                        }
                    }
                }
                pStartOfBuf += strlen(pStartOfBuf);
            }else if(!feof(fp)){
                retVal = retVal | 0x20;
                break;
            }
        }

        /*On condition that there is no key param,return 0x40*/

        if(retVal == 0){
            retVal = 64;
        }
    }while(0);

    if(retVal == 1){
        rewind(fp);

        if(lenChange > 0 ){
            while(lenChange > 0){
                pBuf[strlen(pBuf)] = ' ';
                --lenChange;
            }
        }else{
            while(lenChange < 0){
                if(pBuf[strlen(pBuf)-1] == ' '){
                    pBuf[strlen(pBuf)-1] = '\0';
                }
                ++lenChange;
            }
        }

        if( fputs(pBuf,fp) < 0){
            retVal = retVal | 0x80;
        }else{
            fflush(fp);
        }
    }

    free(pBuf);
    rewind(fp);

    if(retVal == 1)
    {
        return 0;
    }

    return retVal;
}
/*
** Set parameter with additional argument
** Modify "value" to the "key" if "key" exist, added if "key" not exist.
*/

int SysParamSetFuncWithAdditinalArg(FILE *fd, char *key, void *value, pFunc myfunc){
    FILE  *fp = fd;
    char  *pBuf = NULL;
    char  *pStartOfBuf = NULL;
    char  *pPosOfKey = NULL;
    int   fileLength = 0;
    int   bufLength = 0;
    int   lenChange = 0;
    int   retVal = 0;

    do{
        if(fseek(fd,0,SEEK_END) != 0){
            retVal = 2;
            break;
        }

        fileLength = ftell(fd);
        if(fileLength == -1){
            retVal = 4;
            break;
        }

        if(fseek(fd,0,SEEK_SET) != 0){
            retVal = 2;
            break;
        }

        bufLength = fileLength + INCREASEMENT;
        pBuf = (char *)malloc(bufLength);
        if(pBuf == NULL){
            retVal = 8;
            break;
        }
        memset(pBuf,0,bufLength);
        pStartOfBuf = pBuf;

        while(!feof(fp)){
            pPosOfKey = fgets(pStartOfBuf,bufLength,fp);
            if(pPosOfKey == pStartOfBuf)
            {
                if( (retVal&0x01) == 0x00 ){
                    while(*pPosOfKey == ' ')
                    {
                        ++pPosOfKey;
                    }

                    if(*pPosOfKey != '#')
                    {
                        if(strstr(pPosOfKey,key) == pPosOfKey)
                        {
                            pPosOfKey += strlen(key);

                            if(*pPosOfKey==' ' || *pPosOfKey==':')
                            {
                                retVal = 1;
                                while(*pPosOfKey==' ' || *pPosOfKey==':')
                                {
                                    ++pPosOfKey;
                                }

                                lenChange = strlen(pPosOfKey);
                                if(myfunc(pPosOfKey,value) != 0){
                                    retVal = 17;
                                }

                                lenChange = lenChange - strlen(pPosOfKey);
                            }
                        }
                    }
                }
                pStartOfBuf += strlen(pStartOfBuf);
            }else if(!feof(fp)){
                retVal = retVal | 0x20;
                break;
            }
        }

        /*On condition that there is no key param,return 0x40*/
        /*
        if(retVal == 0){
            retVal = 64;
        }
        */
    }while(0);

    rewind(fp);

    if(retVal == 1){
        if(lenChange > 0 ){
            while(lenChange > 0){
                pBuf[strlen(pBuf)] = ' ';
                --lenChange;
            }
        }else{
            while(lenChange < 0){
                if(pBuf[strlen(pBuf)-1] == ' '){
                    pBuf[strlen(pBuf)-1] = '\0';
                }
                ++lenChange;
            }
        }
    }else if(retVal == 0){
        sprintf(pStartOfBuf,"\n%s:",key);
        pStartOfBuf += strlen(pStartOfBuf);
        if(myfunc(pStartOfBuf,value) != 0){
            retVal = -1;
        }
        pStartOfBuf += strlen(pStartOfBuf);
        --pStartOfBuf;
        while(*pStartOfBuf == '\n' || *pStartOfBuf == ' '){
            *pStartOfBuf = '\0';
            --pStartOfBuf;
        }
    }

    if( fputs(pBuf,fp) < 0){
        retVal = retVal | 0x80;
    }else{
        fflush(fp);
    }

    free(pBuf);
    rewind(fp);

    if(retVal == 1)
    {
        return 0;
    }

    return retVal;
}

int SysParamGetFunc(FILE *fd, char *key, void *value, pFunc myfunc){
    FILE  *fp = fd;
    char  *pBuf = NULL;
    char  *pStartOfBuf = NULL;
    char  *pPosOfKey = NULL;
    int   bufLength = 0;
    int   retVal = 0;

    do{
        if(fseek(fd,0,SEEK_END) != 0)
        {
            retVal = 2;
            break;
        }

        bufLength = ftell(fd);
        if(bufLength == -1)
        {
            retVal = 4;
            break;
        }

        if(fseek(fd,0,SEEK_SET) != 0)
        {
            retVal = 2;
            break;
        }

        pBuf = (char *)malloc(bufLength);
        if(pBuf == NULL)
        {
            retVal = 8;
            break;
        }

        memset(pBuf,0,bufLength);
        pStartOfBuf = pBuf;

        while(!feof(fp))
        {
            pPosOfKey = fgets(pStartOfBuf,bufLength,fp);
            if(pPosOfKey == pStartOfBuf)
            {
                while(*pPosOfKey == ' ')
                {
                    ++pPosOfKey;
                }

                if(*pPosOfKey != '#')
                {
                    if(strstr(pPosOfKey,key) == pPosOfKey)
                    {
                        pPosOfKey += strlen(key);
                        if(*pPosOfKey==' ' || *pPosOfKey==':')
                        {
                            retVal = 1;
                            while(*pPosOfKey==' ' || *pPosOfKey==':')
                            {
                                ++pPosOfKey;
                            }

                            if(myfunc(pPosOfKey,value) != 0){
                                retVal = 17;
                            }
                            break;
                        }
                    }
                }
                pStartOfBuf += strlen(pStartOfBuf);
            }else if(!feof(fp)){
                retVal = 32;
                break;
            }
        }
        /*On condition that there is no key param,return 0x40*/
        if(retVal == 0)
        {
            retVal = 64;
        }
    }while(0);

    free(pBuf);
    rewind(fp);

    if(retVal == 1)
    {
        return 0;
    }

    return retVal;
}

int SysParamGetString(FILE * fd, char * key, char * value){
    return SysParamGetFunc(fd, key, value, myGetStringFunc);
}

int SysParamGetInt(FILE * fd, char * key, int * value){
    return SysParamGetFunc(fd, key, value, myGetIntFunc);
}

int SysParamSetString(FILE * fd, char * key, char * value){
#if 1
    char srcValue[400] = {"\0"};
    SysParamGetString(fd, key, srcValue);
    if(strcmp(value, srcValue)) {
	return SysParamSetFunc(fd,key,value,mySetStringFunc);
    }
    else
	return 0;
#else
    return SysParamSetFunc(fd,key,value,mySetStringFunc);
#endif
}

int SysParamSetInt(FILE * fd, char * key, int value){
#if 1
    int srcValue;
    SysParamGetInt(fd, key, &srcValue);
    if(value != srcValue) {
	return SysParamSetFunc(fd,key,(void*)value,mySetIntFunc);
    } else {
	return 0;
    }
#else
    return SysParamSetFunc(fd,key,(void*)value,mySetIntFunc);
#endif
}

int saveYuduanCfg(char *arg,char *applyItem)
{
	FILE* file = fopen(DEVICE_CFG_FILE,"r+");
	int ret = SysParamSetString(file,applyItem,arg);
	fclose(file);
	return ret;
}
int getYuduanCfg(char *arg,char *applyItem)
{
    FILE* file = fopen(DEVICE_CFG_FILE,"r+");
    int ret = SysParamGetString(file,applyItem,arg);
    fclose(file);
    return ret;
}


//
int main()
{
	char *tempStr[64]={0};
	getYuduanCfg(tempStr,"IPADDDR")
	saveYuduanCfg("0","TESTITEM");

	return 0;

}


其中,函数SysParamSetFuncWithAdditinalArg,可以对于不存在的key进行动态添加,是个亮点。

相关文章推荐

易语言配置文件读写操作

  • 2013年10月24日 12:50
  • 4KB
  • 下载

java配置文件的读写操作

java中有一中配置文件properties,可以放置一些数据。 写入配置文件:           因为此文件中数据都是以键值对的形式存在,所以写入需要key和value。        ...

bash 脚本实现的配置文件(ini/inc.sh)的读写操作

这两个bash 函数是以前写的,目的是对一个已经存在的ini配置文件进行修改和读取,当然实在Linux Bash Shell下进行的: 例如一个配置文件 config.ini # config.in...
  • wxqee
  • wxqee
  • 2012年06月08日 10:00
  • 6436

配置文件读写操作(.ini)

使用C#实现对配置文件(config.ini)的读写操作,以下为基本操作函数源码,包括不同数据类型参数的读写:string、int、double...
  • jyxyscf
  • jyxyscf
  • 2017年07月12日 13:40
  • 99

Java文件读写操作函数实现

  • 2015年04月27日 19:25
  • 10KB
  • 下载

文件读写操作+动态内存分配+结构体指针+函数指针数组实现通讯录系统

实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2. 删除指定联系人信息 3. 查找指定联系人信息 4....

C++ 的各种文件读写操作总结

  • 2012年09月15日 11:17
  • 76KB
  • 下载

C、C++文件的读写操作总结

  • 2011年08月27日 16:43
  • 72KB
  • 下载

C语言文件读写操作中缓冲区问题和setbuf函数详解

清除和设置文件缓冲区 (1).清除文件缓冲区函数: int fflush(FILE *stream); int flushall(); fflush()函数将清除由stream指向的文件缓冲区里的内...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【工作总结】一组关于key=value类似配置文件的读写操作函数
举报原因:
原因补充:

(最多只允许输入30个字)