#ifndef __INIPARSERHELPER_H_
#define __INIPARSERHELPER_H_ #define IN
#define OUT
#define INOUT typedef enum iniPraseValueType
{
INIP_STR = ,
INIP_INT,
INIP_DOUBLE,
INIP_BOOL,
}IniPraseType; typedef struct stIniParserResult
{
char * pcStrResult;
int iIntResult;
double dDoubleResult;
int iBoolResult;
int iErrorCode;
}STIniParserResult; #define RESULT_OK 0x00000000
#define DEFAULT_ERROR 0x00000001
#define PARAM_ERROR 0x00000002
#define MALLOC_ERROR 0x00000003
#define DATA_ERROR 0x00000004
#define FILE_NOTEXIST_ERROR 0x00000005 #include "iniparser.h" //初始化
int initIniParser(IN const char *pcFileName ,IN dictionary **dicIni); //获取section的个数
int getSectionCount(dictionary * dicIni, int *iSenctionCount); //获取指定位置的section的值
int getSectionByIndex(dictionary * dicIni, unsigned int n, char **pcResult); //获取section下key的个数
int getSectionKeyCount(dictionary * dicIni, const char *pcSectionName, unsigned int *n); //获取section的键的集合
int getSectionKeys(dictionary * dicIni, const char *pcSectionName, char **pcKeys); //获取指定section:key的值
STIniParserResult * getIniValue(dictionary * dicIni, const char *pcSectionName, const char *pcKey, IniPraseType enValueType); //获取指定section:key的字符串值
char * getIniValueExtend(dictionary * dicIni, const char *pcSectionName, const char *pcKey); //获取ini文件中字符串的值(section已经拼接完成)
char * inipGetStringExtend(IN dictionary * dicIni, IN const char * pcKey); //释放资源
void releaseIniParser(dictionary * dicIni); #endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include "iniParserHelper.h" //double类型允许的精度误差
#define ACCURACY 0.00001 /********************************************************
Func Name: inipGetString
Date Created: 2018-7-3
Description: 获取ini文件中字符串的值
Input: dicIni: dictionary struct
pcKey:key
Output: pstResult:返回结构体
Return: 无
Caution:
*********************************************************/
static void inipGetString(IN dictionary * dicIni, IN const char * pcKey, OUT STIniParserResult *pstResult)
{
if (NULL == dicIni || pcKey == NULL || NULL == pstResult)
{
return;
}
pstResult->pcStrResult = (char *)iniparser_getstring(dicIni, pcKey, NULL);
if (NULL == pstResult->pcStrResult)
{
pstResult->iErrorCode = DATA_ERROR;
}else
{
pstResult->iErrorCode = RESULT_OK;
}
} /********************************************************
Func Name: inipGetInt
Date Created: 2018-7-3
Description: 获取ini文件中int类型的值
Input: dicIni: dictionary struct
pcKey:key
Output: pstResult:返回结构体
Return: 无
Caution:
*********************************************************/
static void inipGetInt(IN dictionary * dicIni, IN const char * pcKey, OUT STIniParserResult *pstResult)
{
int iResCode1 = , iResCode2 = , iResCode = ;
int iFlag = ;
iResCode = iniparser_getint(dicIni, pcKey, iResCode1);
if (iResCode == iResCode1)
{
iFlag += ;
}
iResCode = iniparser_getint(dicIni, pcKey, iResCode2);
if (iResCode == iResCode1)
{
iFlag += ;
}
if (iFlag > )
{
pstResult->iErrorCode = DATA_ERROR;
}else
{
pstResult->iIntResult = iResCode;
pstResult->iErrorCode = RESULT_OK;
}
} /********************************************************
Func Name: inipGetDouble
Date Created: 2018-7-3
Description: 获取ini文件中double类型的值
Input: dicIni: dictionary struct
pcKey:key
Output: pstResult:返回结构体
Return: 无
Caution:
*********************************************************/
static void inipGetDouble(IN dictionary * dicIni, IN const char * pcKey, OUT STIniParserResult *pstResult)
{
double iResCode1 = 0.0001, iResCode2 = 0.0002, iResCode = 0.0;
int iFlag = ;
iResCode = iniparser_getdouble(dicIni, pcKey, iResCode1);
if (iResCode >= iResCode1-ACCURACY && iResCode <= iResCode1+ACCURACY)
{
iFlag += ;
}
iResCode = iniparser_getdouble(dicIni, pcKey, iResCode2);
if (iResCode >= iResCode2-ACCURACY && iResCode <= iResCode2+ACCURACY)
{
iFlag += ;
}
if (iFlag > )
{
pstResult->iErrorCode = DATA_ERROR;
}else
{
pstResult->dDoubleResult = iResCode;
pstResult->iErrorCode = RESULT_OK;
}
} /********************************************************
Func Name: inipGetBool
Date Created: 2018-7-3
Description: 获取ini文件中bool类型的值
Input: dicIni: dictionary struct
pcKey:key
Output: pstResult:返回结构体
Return: 无
Caution:
*********************************************************/
static void inipGetBool(IN dictionary * dicIni, IN const char * pcKey, OUT STIniParserResult *pstResult)
{
int iResCode = -, iTestNum = -;
iResCode = iniparser_getboolean(dicIni, pcKey, iTestNum);
if (iResCode == iTestNum)
{
pstResult->iErrorCode = DATA_ERROR;
}else
{
pstResult->iBoolResult = iResCode;
pstResult->iErrorCode = RESULT_OK;
}
} /********************************************************
Func Name: getIniValue
Date Created: 2018-7-3
Description: 获取ini文件的值
Input: dicIni : dictionary struct
pcSectionName : section name
pcKey :key
enValueType : value type
Output:
Return: result struct
Caution: need free return value
*********************************************************/
STIniParserResult * getIniValue(dictionary * dicIni, const char *pcSectionName, const char *pcKey, IniPraseType enValueType)
{
char *pcSectionKey = NULL;
size_t uiLen = ;
STIniParserResult * pstResult = NULL; pstResult = malloc(sizeof(STIniParserResult));
if(NULL == pstResult)
{
return NULL;
}
memset(pstResult, , sizeof(STIniParserResult)); if (NULL == dicIni || NULL == pcSectionName || NULL == pcKey)
{
pstResult->iErrorCode = PARAM_ERROR;
return pstResult;
}
//多一个":"和"/0"
uiLen = strlen(pcSectionName) + strlen(pcKey) + + ;
pcSectionKey = (char *)malloc(uiLen);
if(NULL == pcSectionKey)
{
pstResult->iErrorCode = MALLOC_ERROR;
return pstResult;
}
memset(pcSectionKey, , uiLen);
sprintf(pcSectionKey,"%s:%s",pcSectionName,pcKey);
switch (enValueType)
{
case INIP_STR:
inipGetString(dicIni, pcSectionKey, pstResult);
break;
case INIP_INT:
inipGetInt(dicIni, pcSectionKey, pstResult);
break;
case INIP_DOUBLE:
inipGetDouble(dicIni, pcSectionKey, pstResult);
break;
case INIP_BOOL:
inipGetBool(dicIni, pcSectionKey, pstResult);
break;
default:
pstResult->iErrorCode = DATA_ERROR;
break;
}
if(pcSectionKey)
{
free(pcSectionKey);
pcSectionKey = NULL;
}
return pstResult;
} /********************************************************
Func Name: initIniParser
Date Created: 2018-7-3
Description: 初始化ini解析器
Input: pcFileName : Name of the ini file to read
dicIni : dictionary object
Output:
Return: errcode
Caution: need call releaseIniParser()
*********************************************************/
int initIniParser(IN const char *pcFileName ,IN dictionary **dicIni)
{
int iResultCode = DEFAULT_ERROR; if (NULL == pcFileName || NULL == dicIni)
{
iResultCode = PARAM_ERROR;
return iResultCode;
} if (access(pcFileName, F_OK))
{
iResultCode = FILE_NOTEXIST_ERROR;
return iResultCode;
}
*dicIni = iniparser_load(pcFileName);
if(NULL == *dicIni)
{
iResultCode = DEFAULT_ERROR;
return iResultCode;
}
return RESULT_OK;
} /********************************************************
Func Name: releaseIniParser
Date Created: 2018-7-3
Description: 释放ini解析器
Input: dicIni : dictionary object
Output:
Return:
Caution:
*********************************************************/
void releaseIniParser(IN dictionary * dicIni)
{
if (dicIni)
{
iniparser_freedict(dicIni);
}
} /********************************************************
Func Name: getSectionCount
Date Created: 2018-7-4
Description: 获取section的个数
Input: dicIni : dictionary object
iSenctionCount :section numbers
Output:
Return: error code
Caution:
*********************************************************/
int getSectionCount(IN dictionary *dicIni, IN int *iSenctionCount)
{
int iResultCode = DEFAULT_ERROR;
if (NULL == dicIni || NULL == iSenctionCount)
{
iResultCode = PARAM_ERROR;
return iResultCode;
}
iResultCode = iniparser_getnsec(dicIni);
if (- == iResultCode)
{
iResultCode = DATA_ERROR;
return iResultCode;
}
*iSenctionCount = iResultCode;
return RESULT_OK;
} /********************************************************
Func Name: getSectionByIndex
Date Created: 2018-7-4
Description: 获取指定位置的section
Input: dicIni : dictionary object
n : section numbers
Output: pcResult : section name
Return: error code
Caution: Do not free or modify the returned pcResult
*********************************************************/
int getSectionByIndex( IN dictionary * dicIni, IN unsigned int n, OUT char **pcResult)
{
int iResultCode = DEFAULT_ERROR;
char *pcTmp = NULL;
if (NULL == dicIni || NULL == pcResult)
{
iResultCode = PARAM_ERROR;
return iResultCode;
}
pcTmp = (char *)iniparser_getsecname(dicIni, (int)n);
if (NULL == pcTmp)
{
iResultCode = DATA_ERROR;
return iResultCode;
}
*pcResult = pcTmp;
return RESULT_OK;
} /********************************************************
Func Name: getSectionKeyCount
Date Created: 2018-7-4
Description: 获取section下key的个数
Input: dicIni : dictionary object
pcSectionName : section name
Output: n : key numbers
Return: error code
Caution:
*********************************************************/
int getSectionKeyCount( IN dictionary * dicIni, IN const char *pcSectionName, OUT unsigned int *n)
{
int iResultCode = DEFAULT_ERROR;
unsigned int num = ;
if (NULL == dicIni || NULL == pcSectionName || NULL == n)
{
iResultCode = PARAM_ERROR;
return iResultCode;
}
num = (unsigned int)iniparser_getsecnkeys(dicIni,pcSectionName);
if ( == num)
{
iResultCode = DATA_ERROR;
return iResultCode;
}
*n = num;
return RESULT_OK;
} /********************************************************
Func Name: getSectionKeys
Date Created: 2018-7-4
Description: 获取section的键的集合
Input: dicIni : dictionary object
pcSectionName : section name
OutInput: pcKeys : keys in a section of a dictionary
Return: error code
Caution: pcKeys必须有函数调用者进行分配,但是pcKeys数组中的元素的内存不需要由调用者释放
*********************************************************/
int getSectionKeys(IN dictionary * dicIni, IN const char *pcSectionName,INOUT char **pcKeys)
{
int iResultCode = DEFAULT_ERROR;
if (NULL == dicIni || NULL == pcSectionName || NULL == pcKeys)
{
iResultCode = PARAM_ERROR;
return iResultCode;
}
iniparser_getseckeys(dicIni, pcSectionName, pcKeys);
return RESULT_OK;
} /********************************************************
Func Name: getIniValueExtend
Date Created: 2018-7-4
Description: 获取指定section:key的字符串值
Input: dicIni : dictionary object
pcSectionName : section name
pcKey : key name
OutInput:
Return: error code
Caution:
*********************************************************/
char * getIniValueExtend(IN dictionary * dicIni, IN const char *pcSectionName, IN const char *pcKey)
{
STIniParserResult *pstData = NULL;
char * pcResult = NULL; pstData = getIniValue(dicIni, pcSectionName, pcKey, INIP_STR);
if (pstData)
{
pcResult = pstData->pcStrResult;
free(pstData);
pstData = NULL;
}
return pcResult;
} /********************************************************
Func Name: inipGetStringExtend
Date Created: 2018-7-3
Description: 获取ini文件中字符串的值(section已经拼接完成)
Input: dicIni: dictionary struct
pcKey:key
Output:
Return: key的值
Caution:
*********************************************************/
char * inipGetStringExtend(IN dictionary * dicIni, IN const char * pcKey)
{
char *pcResult = NULL;
if (NULL == dicIni || pcKey == NULL)
{
return NULL;
}
pcResult= (char *)iniparser_getstring(dicIni, pcKey, NULL);
return pcResult;
}

该配置文件读取使用了iniparser开源库,需要从开源网站上下载源码编译生成动态库文件调用

Linux共享库 配置文件读取的更多相关文章

  1. linux共享库

    linux共享库 linux中共享库一般以.so.x.y.z 命名,其中x,y,z分别为主版本号.次版本号.发布版本号.同一个库,主版本号不同则相互不兼容:主版本相同,次版本号高的库比次版本号低的库有 ...

  2. Linux共享库两种加载方式简述

      Linux共享库两种加载方式简述  动态库技术通常能减少程序的大小,节省空间,提高效率,具有很高的灵活性,对于升级软件版本也更加容易.与静态库不同,动态库里面的函数不是执行程序本身 的一部分,而是 ...

  3. linux环境 :Linux 共享库LIBRARY_PATH, LD_LIBRARY_PATH 与ld.so.conf

    参考: 1. Linux 共享库:LD_LIBRARY_PATH 与ld.so.conf Linux环境变量名,该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径.(该路径在默 ...

  4. linux共享库加载

    参考自: <<程序员的自我修养--链接.装载与库>> 第八章 Linux共享库的组织 以下截取部分内容 (这本书比较好的讲解了从程序的链接,装载,到运行) 共享库的兼容性 li ...

  5. linux共享库的版本控制

    前几天看到一篇介绍linux共享库版本控制及使用的文章,觉得不错,这里就与大家分享一下. 1. Linux约定 经常看到Linux中,共享库的名字后面跟了一串数字,比如:libperl.so.5.18 ...

  6. Linux共享库、静态库、动态库详解

    1. 介绍 使用GNU的工具我们如何在Linux下创建自己的程序函数库?一个“程序函数库”简单的说就是一个文件包含了一些编译好的代码和数据,这些编译好的代码和数据可以在事后供其他的程序使用.程序函数库 ...

  7. Linux 共享库(动态库)

    Linux 系统上有两类根本不同的 Linux 可执行程序.第一类是静态链接的可执行程序.静态可执行程序包含执行所需的所有函数 — 换句话说,它们是“完整的”.因为这一原因,静态可执行程序不依赖任何外 ...

  8. Linux Linux共享库

    so文件在linux中为共享库,与windows下的dll类似. so文件中的函数可供多个进程调用,最大可能的提供二进制代码复用. 共享库可以使代码的维护工作大大简化,当修正了一些错误或者添加了新特性 ...

  9. Linux共享库 日志方法

    mylog.h #ifdef __cplusplus extern "C" { #endif //写日志函数 //path:日志文件名 //msg:日志信息 int writelo ...

随机推荐

  1. 微信支付错误,页面URL末注册

    最近在做个项目用到微信支付的JSSDK支付时候碰到“URL末注册的问题”,可是我已经在公众平台里的支付目录里添加了,测试了几次都是这个问题,最后才发现原来是大小写的问题,还有我的支付页面是ASP.NE ...

  2. 含有按钮的ScrollView在iOS8中无法滚动的解决办法 | ScrollView with UIControl/UIButton subviews not scrollable under iOS 8

    转自:http://zcw.me/blogwp/%E5%90%AB%E6%9C%89%E6%8C%89%E9%92%AE%E7%9A%84scrollview%E5%9C%A8ios8%E4%B8%A ...

  3. redisHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceS ...

  4. SSH框架整合开发具体解释(个人笔记)

    一.创建数据库并设置编码. A) create database oa default character set utf8. 二.MyEclipseproject A) 在Myeclipse里创建w ...

  5. python2/3 中删除字典中value为空的键值对方法

    python2 data_info = { 'account': 1, 'remark': 2, 'sort': '', 'weight': '', } for key in data_info.ke ...

  6. 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-1导入JavaScript插件

    导入JavaScript插件 Bootstrap除了包含丰富的Web组件之外,如前面介绍的下拉菜单.按钮组.导航.分页等.他还包括一些JavaScript的插件. Bootstrap的JavaScri ...

  7. CentOS 7修改管理员密码

    之前安装CentOS 7的时候随便输入了一个密码,现在使用需要root权限,只能重置密码了.下面是处理方法:(进入单用户模式修改root密码为例) 在启动菜单中,按e编辑所选启动项 一页内容,感觉挺陌 ...

  8. 内核参数SEMMSL SEMMNS SEMOPM SEMMNI参数的设置

    内核参数SEMMSL SEMMNS SEMOPM SEMMNI参数的设置  转自:http://www.dbafree.net/?p=92   这四个参数自己一直没搞清楚 今天问了下同事,大概整了一下 ...

  9. Python通过ssh连接服务器并执行命令

    [本文出自天外归云的博客园] 脚本示例如下: # coding:utf-8 import time,paramiko,re,StringIO def exec_shell(command): ''' ...

  10. Delphi调用JAVA的WebService上传XML文件(XE10.2+WIN764)

    相关资料:1.http://blog.csdn.net/luojianfeng/article/details/512198902.http://blog.csdn.net/avsuper/artic ...