配置文件键值对读写API函数实现

项目中开发了一种读写配置文件键值的函数(name = value格式),非常实用,特将源码记录如下:

调用接口函数为:

int setcfgx(const char *filpath, const char *nam, char *val)  写入

int getcfgx(const char *filpath, const char *nam, char *val)  读取

Conftool.h文件源码:

/*
**  Copyright (c) 2012 OCS, Inc.
**
**  Project: GPON-ONT-V2.0
**  File:    conf_toolkit.h
**  Author:  ychxie/xuzhe
**  Date:    02/08/2012
**
**  Purpose:
**    provide funcitions for reading and writing configuration files
*/

#ifndef _CONF_TOOLKIT_H
#define _CONF_TOOLKIT_H

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

/* Macro constant definitions. */
#define SVC_PRINTF_BUF_LEN        4096
#define SVC_PRINTF_BUF_MARGIN_LEN 32
#define BOOL                   eOsBool

/* Type definitions. */
typedef enum
{
    eSVC_LL_MIN,
    eSVC_LL_MINOR = eSVC_LL_MIN,
    eSVC_LL_MAJOR,
    eSVC_LL_CRITICAL,
    eSVC_LL_NO_LOG,
    eSVC_LL_MAX,
    OG_ENUM_MAX
} eSvcLogLevel;

typedef enum
{
    FALSE,
    TRUE
} eOsBool;


/* Include files. */

/* Macro constant definitions. */
#define MAX_LINES 4096
#define LINE_LEN  128
#define NAME_LEN  128



/* Type definitions. */
typedef enum
{
    eOP_ERR_SUCCESS = 0,
    eOP_ERR_ERROR = 1    
}eOpCfgError;


/* External function declarations. */

/* Macro API definitions. */

/* Global variable declarations. */


/**************************************************************************
**  Function Name: setcfgx
**  Purpose:
**    set single key and value pairs into conf.
**  Parameters:
**    filpath - full path of configuration file
**    nam   - key
**    val     - value
**  Return:
**    0       - success
**    other - failure  (refer to eOpCfgError for details)
**  Notes:
**    None.
**************************************************************************/
int setcfgx(const char *filpath, const char *nam, char *val);

/**************************************************************************
**  Function Name: getcfgx
**  Purpose:
**    get single key from conf.
**  Parameters:
**    filpath - full path of configuration file
**    nam   - key
**    val     - value
**  Return:
**    0       - success
**    other - failure  (refer to eOpCfgError for details)
**  Notes:
**    None.
**************************************************************************/
int getcfgx(const char *filpath, const char *nam, char *val);

/**************************************************************************
**  Function Name: item_write
**  Purpose:
**    set single key and value pairs into conf.
**  Parameters:
**    filpath - full path of configuration file
**    nam   - key
**    val     - value
**  Return:
**    0       - success
**    other - failure  (refer to eOpCfgError for details)
**  Notes:
**    None.
**************************************************************************/
int item_write(const char *path, const char *key, const char *value);

/**************************************************************************
**  Function Name: item_read
**  Purpose:
**    get single key from conf.
**  Parameters:
**    filpath - full path of configuration file
**    nam   - key
**    val     - value
**  Return:
**    0       - success
**    other - failure  (refer to eOpCfgError for details)
**  Notes:
**    None.
**************************************************************************/
int item_read(const char *path, const char *key, char *value);

/**************************************************************************
**  Function Name: batch_start
**  Purpose:
**    Identification of batch operation start.
**  Parameters:
**    None
**  Return:
**    0       - success
**    other - failure  (refer to eOpCfgError for details)
**  Notes:
**    None.
**************************************************************************/
int batch_start(void);

/**************************************************************************
**  Function Name: batch_stop
**  Purpose:
**    Identification of batch operation stop.
**  Parameters:
**    None
**  Return:
**    0       - success
**    other - failure  (refer to eOpCfgError for details)
**  Notes:
**    None.
**************************************************************************/
int batch_stop(void);


#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

#endif

Conftool.c文件源码:

/*
**  Copyright (c) 2012 OCS, Inc.
**
**  Project: GPON-ONT-V2.0
**  File:    conf_toolkig.c
**  Author:  ychxie/xuzhe
**  Date:    02/08/2012
**
**  Purpose:
**    implement of conf_toolkit.h.
*/

/* Include files. */
#include <stdio.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <errno.h>
#include <stdarg.h>
#include <ctype.h>
#include <libgen.h>

#include "conftool.h"
#include "debug_log.h"

/* Macro constant definitions. */
#define S_ISSYMLNK(m) (((m) & S_IFMT) == S_IFLNK)  /**analyze if a file is a symbol link*/
#define OPEN_FLAG O_CREAT
#define OPEN_MODE 00644
#define INIT_V    1
#define MAX_READ_WRITE_TIMES 3

#define ERR_CACHE_MAX       (2 * 1024 + 4)          // 2k for Error Buffer.
#define LINE_CACHE_MAX      (5 * 1024 + 4)          // 5k for A Line.
#define FILE_PATH_MAX       (1 * 1024 + 4)          // 1k for File/Dir path.



/* Type definitions. */
struct one_line 
{
    char *line;
    char *extern_line;
    char line_cache[LINE_LEN];
    int line_size;
};

struct file_content 
{
    struct one_line lines[MAX_LINES];
    int line_cnt;
};

typedef enum
{
    eOP_TYPE_GET = 0,
    eOP_TYPE_SET,
    eOP_TYPE_BATCH_OP
}eOpType;



/* Local function declarations. */
static void op_log_head(eOpType op_type, const char* filename, const char *name);
static void op_log_tail(eOpType op_type, const char* filename, const char *name, const char *val, int op_ret);

static void rleadingblank(char *string);
static void rrearblank(char *string);
static void riprt(char *str);
static int readfromfile(char *filename, struct file_content *file);
static int writetofile(char *filename, char *strtxt, int strsize);
static int setlinecontent(struct file_content *file, int index,  char *fmt, const char *name, const char *val);
static void initstaticvar(void);
static void seterrstr(const char *formatP, ...);

/* Macro API definitions. */

/* Global variable definitions. */
static struct file_content batch_op_file;

//symbol of batching operation
//if symbol is true,then setcfgx operations not to read file again after first reading the file to RAM ,and not write file
//until executint the batch_stop(),then to set already_read false ,and begin to write file
static BOOL batch_flag = FALSE;

//indicate the file has been read into RAM, in batch_read stitution not need to read again
//in single_read and call DLL stitution,must set this parameter to false before function return in setcfgx(),otherwise not to read the file in readfromfile() 
static BOOL already_read = FALSE;

static BOOL modify_flag = FALSE;
static char batch_tmp_file_name[LINE_LEN] = {0};
static char batch_real_file_path[LINE_LEN] = {0};
static char error_str[ERR_CACHE_MAX] = {0};
static sem_t *sem = NULL;
static pthread_mutex_t *ptool_lock = NULL;

extern int errno;

//the methods below not to release
// int item_write(const char *path, const char *key, const char *value);
// int item_read(const char *path, const char *key, char *value);
int batch_start(void);
int batch_stop(void);

/**************************************************************************
**  Function Name: setcfgx
**  Purpose:
**    set single key and value pairs into conf.
**  Parameters:
**    filpath - full path of configuration file
**    nam   - key
**    val     - value
**  Return:
**    0       - success
**    other - failure  (refer to eOpCfgError for details)
**  Notes:
**    None.
**************************************************************************/
int setcfgx(const char *filpath, const char *nam, char *val)
{
    struct file_content tempfile;
    struct file_content *file = NULL;

    char tmpline[LINE_CACHE_MAX] = {0};
    char tmpname[NAME_LEN] = {0};
    char realname[NAME_LEN] = {0};
    char *saveptr = NULL, *pname = NULL;
    int i = 0, ret = 0, file_size = 0, exist = 0, firstchar = 1;
    char dir_name[FILE_PATH_MAX] = {0};
    char file_name[FILE_PATH_MAX] = {0};
    char tmp_file_name[FILE_PATH_MAX] = {0};
    char real_file_path[FILE_PATH_MAX] = {0};
    char *ptoken = NULL;
    char *file_buffer = NULL;

    //parameter value in conf has been modified 
    BOOL bModified = FALSE;
    //DEBUG_LOG("****bModified parameter original value is false\n");

    if (NULL == ptool_lock)
    {
        ptool_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));

        if (NULL == ptool_lock)
        {
            DEBUG_LOG("malloc pthread_mutex_t failed.\n");
            return eOP_ERR_ERROR;        
        }

        if(pthread_mutex_init(ptool_lock, NULL) != 0)
        {
            DEBUG_LOG("Init Mutex agent_co
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值