freecplus框架-加载参数文件

1、源代码说明

freecplus是一个Linux系统下的C/C++开源框架,源代码请前往C语言技术网(www.freecplus.net)下载。数据库

本文介绍的是freecplus框架中加载参数文件的方法。数组

函数和类的声明文件是freecplus/_freecplus.h。服务器

函数和类的定义文件是freecplus/_freecplus.cpp。数据结构

示例程序位于freecplus/demo目录中。框架

编译规则文件是freecplus/demo/makefile。ide

2、参数文件的意义

在项目开发中,一个完整的系统由多个C/C++服务程序组成,这些服务程序有共同的参数,例如数据库的链接参数、日志文件存放的目录、数据文件存放的目录等。函数

传统的方法是把参数放在文本文件中,例如hssms.ini,格式以下:.net

logpath=/log/hssms              # 日志文件存放的目录。
connstr=hssms/smspwd@hssmszx  # 数据库链接参数。
datapath=/data/hssms            # 数据文件存放的根目录。
serverip=192.168.1.1            # 中心服务器的ip地址。
port=5058                         # 中心服务器的通讯端口。
online=true                       # 是否采用长链接。

如今有更好的方法是把参数放在xml文件中,例如hssms.xml,格式以下:日志

<?xml version="1.0" encoding="gbk" ?>
<root>
    <!-- 程序运行的日志文件名。 -->
    <logpath>/log/hssms</logpath>

    <!-- 数据库链接参数。 -->
    <connstr>hssms/smspwd@hssmszx</connstr>

    <!-- 数据文件存放的根目录。 -->
    <datapath>/data/hssms</datapath>

    <!-- 中心服务器的ip地址。 -->
    <serverip>192.168.1.1</serverip>

    <!-- 中心服务器的通讯端口。 -->
    <port>5058</port>

    <!-- 是否采用长链接,true-是;false-否。 -->
    <online>true</online>
</root>

通常来讲,一个项目是由多种语言开发完成,xml文件格式比传统的ini文件格式更方便。code

3、CIniFile类

CIniFile类用于服务程序从参数文件中加载参数。

一、类的声明

// 参数文件操做类。
// CIniFile类操做的是xml格式的参数文件,并非传统的ini文件。
class CIniFile
{
public:
  // 存放参数文件所有的内容,由LoadFile载入到本变量中。
  string m_xmlbuffer;

  CIniFile();

  // 把参数文件的内容载入到m_xmlbuffer变量中。
  bool LoadFile(const char *filename);

  // 获取参数文件字段的内容。
  // fieldname:字段的标签名。
  // value:传入变量的地址,用于存放字段内容,支持bool、int、insigned int、long、unsigned long、double和char[]。
  // 注意,当value参数的数据类型为char []时,必须保证value数组的内存足够,不然可能发生内存溢出的问题,
  // 也能够用ilen参数限定获取字段内容的长度,ilen的缺省值为0,表示不限定获取字段内容的长度。
  // 返回值:true-获取成功;false-获取失败。
  bool GetValue(const char *fieldname,bool *value);
  bool GetValue(const char *fieldname,int  *value);
  bool GetValue(const char *fieldname,unsigned int *value);
  bool GetValue(const char *fieldname,long *value);
  bool GetValue(const char *fieldname,unsigned long *value);
  bool GetValue(const char *fieldname,double *value);
  bool GetValue(const char *fieldname,char *value,const int ilen=0);
};

二、示例程序

示例(demo45.cpp)

/*
 *  程序名:demo45.cpp,此程序演示采用freecplus框架的CIniFile类加载参数文件。
 *  做者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"

// 用于存放本程序运行参数的数据结构。
struct st_args
{
  char logpath[301];
  char connstr[101];
  char datapath[301];
  char serverip[51];
  int  port;
  bool online;
}stargs;

int main(int argc,char *argv[])
{
  // 若是执行程序时输入的参数不正确,给出帮助信息。
  if (argc != 2) 
  { 
    printf("\nusing:/freecplus/demo/demo45 inifile\n"); 
    printf("samples:/freecplus/demo/demo45 /freecplus/ini/hssms.xml\n\n"); 
    return -1;
  }

  // 加载参数文件。
  CIniFile IniFile;
  if (IniFile.LoadFile(argv[1])==false)
  {
    printf("IniFile.LoadFile(%s) failed.\n",argv[1]); return -1;
  } 

  // 获取参数,存放在stargs结构中。
  memset(&stargs,0,sizeof(struct st_args));
  IniFile.GetValue("logpath",stargs.logpath,300);
  IniFile.GetValue("connstr",stargs.connstr,100);
  IniFile.GetValue("datapath",stargs.datapath,300);
  IniFile.GetValue("serverip",stargs.serverip,50);
  IniFile.GetValue("port",&stargs.port);
  IniFile.GetValue("online",&stargs.online);

  printf("logpath=%s\n",stargs.logpath);
  printf("connstr=%s\n",stargs.connstr);
  printf("datapath=%s\n",stargs.datapath);
  printf("serverip=%s\n",stargs.serverip);
  printf("port=%d\n",stargs.port);
  printf("online=%d\n",stargs.online);

  // 如下能够写更多的主程序的代码。
}

运行效果

在这里插入图片描述

4、版权声明

C语言技术网原创文章,转载请说明文章的来源、做者和原文的连接。
来源:C语言技术网(www.freecplus.net)
做者:码农有道

若是这篇文章对您有帮助,请点赞支持,或在您的博客中转发个人文章,谢谢!!!若是文章有错别字,或者内容有错误,或其余的建议和意见,请您留言指正,很是感谢!!!

相关文章
相关标签/搜索