freecplus frame - Load parameter file

A source code description

freecplus is a C / C ++ open source framework under a Linux system, go to the C language source code Technology Network (www.freecplus.net) download.

This article describes a method to load the parameter file freecplus framework.

Declaration file functions and classes are freecplus / _freecplus.h.

Definition file functions and classes are freecplus / _freecplus.cpp.

Sample program located freecplus / demo directory.

Compiled rules file is freecplus / demo / makefile.

Second, the significance of the parameter file

In project development, a complete system consists of multiple C / C ++ service program, these programs have a common service parameters, such as the database connection parameters, log files are stored in directories, data files and other directories.

The traditional method is the parameter in a text file, for example as follows hssms.ini, format:

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

Now there is a better way in the xml file parameters such as hssms.xml, format:

<?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>

In general, a project is completed by the multilingual development, xml file format is more convenient than the traditional ini file format.

Three, CIniFile class

CIniFile class service for the program to load parameters from the parameter file.

1, class declaration

// 参数文件操作类。
// 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);
};

2, the sample program

Example (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);

  // 以下可以写更多的主程序的代码。
}

running result

Here Insert Picture Description

Fourth, the copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 159 original articles · won praise 458 · views 120 000 +

Guess you like

Origin blog.csdn.net/wucz122140729/article/details/105187523