【工作总结】一组关于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进行动态添加,是个亮点。

java获取配置文件及根据key获取配置文件中的value的工具类

一、方式一 1、工具类如下 package com.gayaapi.common.util; import java.io.InputStream; import java.util.Propert...
  • IRhythm
  • IRhythm
  • 2017-06-19 14:37:17
  • 1162

sed替换配置文件例子

文本格式为 key=value 用sed替换 例如: mode=LOW(mode不一定为等于LOW),要替换成mode=HIGH 可以用 sed -i "s/\(mode=\)\S*/...
  • chaehom
  • chaehom
  • 2012-07-30 01:08:35
  • 2260

Linux C 读写修改删除配置文件函数

相关博文: http://www.doc88.com/p-632738089005.html  http://www.cnblogs.com/hoys/archive/2011/04/25/202...
  • tanqiuwei
  • tanqiuwei
  • 2012-03-08 13:16:56
  • 1640

来段代码提提神-Java读取config.properties文件中的key=value

又到了每天需要来段代码提提神的时间。结束一天辛劳的工作,可以思考人生了。                  //绑定config.properties文件 private static Resourc...
  • u010235716
  • u010235716
  • 2016-03-24 17:49:46
  • 1224

根据key从Properties文件中加载指定的value

//单例模式实现读取***.properties文件的内容 public class OVLoadProperties { // 声明一个自己的实例 private static OV...
  • myonelotus
  • myonelotus
  • 2017-01-09 15:36:01
  • 782

Spring 读取properties文件key+value方式

1、PropertyPlaceholderConfigurer和 1)spring将properties文件读取后在配置文件中直接将对象的配置信息填充到bean中的变量里。 原本使用PropertyP...
  • liuxiao723846
  • liuxiao723846
  • 2017-05-06 16:40:44
  • 1117

Key-Value双向快速高效查找类C,C++高手实现

  • 2011年01月28日 14:56
  • 2KB
  • 下载

C#中config文件的key标签读写

/// /// 读写系统配置xml文件。 /// public class ConfigurationFile { /// ///...
  • zy332719794
  • zy332719794
  • 2015-04-07 09:59:46
  • 1626

linux下配置文件key-value程序分析

1,在linux下配置文件大多以key=value的键值对形式存在; 2,看了网上有些自己写的键值对解析,感觉有些繁琐; 3,我的理解是先通过gets...
  • cp25807720
  • cp25807720
  • 2013-07-12 19:10:58
  • 1149

js 重写Array 类似map key,value结构

  • 2015年12月04日 14:02
  • 4KB
  • 下载
收藏助手
不良信息举报
您举报文章:【工作总结】一组关于key=value类似配置文件的读写操作函数
举报原因:
原因补充:

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