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

将属性文件以key-value值形式输出的工具类

PropertyUtil .javapackage com.lmb.util; import java.io.IOException; import java.util.ArrayList; impo...
  • lmb55
  • lmb55
  • 2016年05月03日 21:47
  • 1438

一句python,一句R︱列表、元组、字典、数据类型、自定义模块导入(格式、去重)

pandas中有类似R中的read.table的功能,而且很像 1、python的read_csv #数据导入 df = pd.read_csv('./cpu.csv',header=0) #中文...
  • sinat_26917383
  • sinat_26917383
  • 2016年08月23日 11:46
  • 2771

sscanf正则表达式与string快速提取key=value

sscanf正则表达式的一种用法, 用来获取配置文件(key=value)中的值比较方便。 #include int main() { char buf[]="abcdefg=456"; ...
  • liukang325
  • liukang325
  • 2013年10月29日 11:46
  • 1774

js通过key-value取值

js通过key-value取值
  • DUDUfine
  • DUDUfine
  • 2016年08月18日 20:01
  • 5488

Redis快速入门:Key-Value存储系统简介

Redis是一个Key-Value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。这些数据类...
  • oh_Mourinho
  • oh_Mourinho
  • 2016年05月18日 10:24
  • 2545

常见Key-Value存储系统的内存管理策略解析

Key-Value存储作为NoSQL存储的一种常见方式,提供了比SQL数据库更好的可扩展性和读写性能。比如当前开源最热门的Memcached和Redis;淘宝的Tair、腾讯的Cmem、Amazon的...
  • a339811061
  • a339811061
  • 2014年05月10日 15:39
  • 3446

关于HashMap根据Value获取Key

Map中是一个key有且只有一个value. 但是一个value可以对应多个key值. 一般都是通过key,然后map.get(key)获得到value. 如果想要反向想通过value获得key...
  • jinwufeiyang
  • jinwufeiyang
  • 2016年08月30日 18:34
  • 10598

redis在spring集成下key与value的使用方法

假设一切都已经配置妥当。Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用的使用方法参照: http://blog.csdn.net/san...
  • zr527397749
  • zr527397749
  • 2016年11月01日 17:22
  • 2855

配置文件key/value的读写以及Excel的定时导出

近期工作主要涉及一些excel的处理,写一些东西方便学习
  • cccfire
  • cccfire
  • 2017年07月20日 10:27
  • 271

(key,value)对value进行排序

降序排序package test;import java.util.ArrayList; import java.util.Collections; import java.util.Comparat...
  • sundujing
  • sundujing
  • 2015年10月12日 22:35
  • 396
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【工作总结】一组关于key=value类似配置文件的读写操作函数
举报原因:
原因补充:

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