freecplus framework - load parameter file

1, Source code description

Freecplus is a C/C + + open source framework under Linux system. Please go to the C language technology network (www.freecplus.net) to download the source code.

This paper introduces the method of loading parameter file in freecplus framework.

The declaration file for functions and classes is freecplus / 65124; freecplus. H.

The definition file for functions and classes is freecplus / 65124; freecplus.cpp.

The sample program is located in the freecplus/demo directory.

The compile rule file is freecplus/demo/makefile.

2, Meaning of parameter file

In project development, a complete system is composed of several C/C + + service programs, which have common parameters, such as database connection parameters, directory where log files are stored, directory where data files are stored, etc.

The traditional method is to put parameters in a text file, such as hssms.ini, in the following format:

logpath=/log/hssms              # Directory where log files are stored.
connstr=hssms/smspwd@hssmszx  # Database connection parameters.
datapath=/data/hssms            # The root directory where the data files are stored.
serverip=192.168.1.1            # ip address of the central server.
port=5058                         # The communication port of the central server.
online=true                       # Whether long connection is adopted.

Now there is a better way to put parameters in an XML file, such as hssms.xml. The format is as follows:

<?xml version="1.0" encoding="gbk" ?>
<root>
    <!-- The name of the log file where the program runs. -->
    <logpath>/log/hssms</logpath>

    <!-- Database connection parameters. -->
    <connstr>hssms/smspwd@hssmszx</connstr>

    <!-- The root directory where the data files are stored. -->
    <datapath>/data/hssms</datapath>

    <!-- Central server's ip Address. -->
    <serverip>192.168.1.1</serverip>

    <!-- The communication port of the central server. -->
    <port>5058</port>

    <!-- Long connection or not, true-Yes. false-No. -->
    <online>true</online>
</root>

Generally speaking, a project is developed by multiple languages. xml file format is more convenient than traditional ini file format.

3, CIniFile class

The CIniFile class is used by the service program to load parameters from the parameter file.

1. Class declaration

// Parameter file operation class.
// The CIniFile class operates on the parameter file in xml format, not the traditional ini file.
class CIniFile
{
public:
  // Store all the contents of the parameter file and load it into this variable by LoadFile.
  string m_xmlbuffer;

  CIniFile();

  // Load the contents of the parameter file into the m * xmlbuffer variable.
  bool LoadFile(const char *filename);

  // Gets the contents of a parameter file field.
  // fieldname: the label name of the field.
  // value: the address of the input variable, which is used to store the field content. It supports bool, int, insigned int, long, unsigned long, double and char[].
  // Note that when the data type of the value parameter is char [], the memory of the value array must be sufficient, otherwise memory overflow may occur,
  // You can also use the ilen parameter to limit the length of the obtained field content. The default value of ilen is 0, indicating that the length of the obtained field content is not limited.
  // Return value: true - get success; false - get failure.
  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. Sample program

Example (demo45.cpp)

/*
 *  Program name: demo45.cpp, this program demonstrates the use of freecplus framework CIniFile class to load parameter files.
 *  Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include "../_freecplus.h"

// The data structure used to store the operation parameters of this program.
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 the parameters entered when executing the program are not correct, give help information.
  if (argc != 2) 
  { 
    printf("\nusing:/freecplus/demo/demo45 inifile\n"); 
    printf("samples:/freecplus/demo/demo45 /freecplus/ini/hssms.xml\n\n"); 
    return -1;
  }

  // Load the parameter file.
  CIniFile IniFile;
  if (IniFile.LoadFile(argv[1])==false)
  {
    printf("IniFile.LoadFile(%s) failed.\n",argv[1]); return -1;
  } 

  // Obtain the parameters and store them in the stargs structure.
  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);

  // The following can write more main program code.
}

Operation effect

4, Copyright notice

C language technology net original article, reprint please explain the article source, the author and the original link.
Source: C language technology network (www.freecplus.net)
Author: Manon Youdao

If this article is helpful to you, please like support or forward my article in your blog, thank you!!!
If there is a mistake in the article, or there is a mistake in the content, or other suggestions and opinions, please leave a message for correction, thank you very much!!!

Keywords: C++ xml C network Database

Added by natbrazil on Mon, 20 Apr 2020 19:41:01 +0300