C++ ini configuration file format and read and write operations

C++ ini configuration file format and read and write operations

1 Why use ini or other (such as xml, json) configuration files?
如果我们程序没有任何配置文件时,这样的程序对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,这样很不好,所以要用配置文件,让程序发布后还能根据需要进行必要的配置;配置文件有很多如INI配置文件,XML配置文件,还有就是可以使用系统注册表等。注意:ini的后缀名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。因为ini文件实质就是txt文本文件。

2 The format of the ini configuration file
1. The INI file is composed of sections, keys and values.
节:
  [section]
参数(键=值)
  name=value
注解
  注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。

1) Explain the section:
什么是sections ?
所有的parameters都是以sections为单位结合在一起的。所有的section名称都是独占一行,并且sections名字都被方括号包围着([ and ])。在section声明后的所有parameters都是属于该section。对于一个section没有明显的结束标志符,一个section的开始就是上一个section的结束,或者是end of the file。Sections一般情况下不能被nested,当然特殊情况下也可以实现sections的嵌套。

2) Explain the parameters:
INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value,name和value是由等号“=”隔开。name在等号的左边。

3) Explain the content of the note:
在INI文件中注释语句是以分号“;”开始的(有些人定义类是由#注释,例如下面的RrConfig)。所有的所有的注释语句不管多长都是独占一行直到结束的,在分号和行结束符之间的所有内容都是被忽略的。

Well, the above is the format of the ini configuration file, which is the simplest configuration file.

3 C++ reads and writes the ini configuration file
and involves the following four functions in VC.
1) Read the string

DWORD GetPrivateProfileString( 
								LPCTSTR lpAppName,        // INI文件中的一个字段名[节名]可以有很多个节名(配置文件的section名)
						 	   	LPCTSTR lpKeyName,        // lpAppName 下的一个键名,也就是里面具体的变量名(配置文件的key名)
 							   	LPCTSTR lpDefault,        // 如果lpReturnedString为空,则把这个变量赋给lpReturnedString
  							   	LPTSTR lpReturnedString,  // 存放键值的指针变量,用于接收INI文件中键值(数据)的接收缓冲区
  						 	   	DWORD nSize,              // lpReturnedString的缓冲区大小
  							   	LPCTSTR lpFileName        // INI文件的路径
							 );

2) Read the integer value

UINT GetPrivateProfileInt(
 		LPCTSTR lpAppName,   // INI文件中的一个字段名[节名]可以有很多个节名
 		LPCTSTR lpKeyName,   // lpAppName 下的一个键名,也就是里面具体的变量名
 		INT nDefault,        // 如果没有找到指定的数据返回,则把个变量值赋给返回值
  		LPCTSTR lpFileName   // INI文件的路径
		);

3) Write a string

BOOL WritePrivateProfileString(
  			LPCTSTR lpAppName,  // INI文件中的一个字段名[节名]可以有很多个节名
  			LPCTSTR lpKeyName,  // lpAppName 下的一个键名,也就是里面具体的变量名
  			LPCTSTR lpString,   // 键值,也就是数据
  			LPCTSTR lpFileName  // INI文件的路径
			);

4) Write an integer value (there is no related function, it can be achieved by parameter conversion through WritePrivateProfileString)

4 Code example
由于VC提供的函数部分功能比较少,一般解析配置文件都是调用库或者别人在项目中使用过的类,所以上面只是为了让大家了解ini的格式及相关参数的意义。下面的代码其实不需要看懂,只需要你会用即可。

ReConfig.h文件

#ifndef RR_CONFIG_H_
#define RR_CONFIG_H_
#include <string>
#include <map>
namespace rr
{
    
    
	class RrConfig
	{
    
    
	public:
		RrConfig()
		{
    
    
		}
		~RrConfig()
		{
    
    
		}
		bool ReadConfig(const std::string & filename);
		std::string ReadString(const char* section, const char* item, const char* default_value);
		int ReadInt(const char* section, const char* item, const int& default_value);
		float ReadFloat(const char* section, const char* item, const float& default_value);
	private:
		bool IsSpace(char c);
		bool IsCommentChar(char c);
		void Trim(std::string & str);
		bool AnalyseLine(const std::string & line, std::string& section, std::string & key, std::string & value);

	private:
		//std::map<std::string, std::string> settings_;
		std::map<std::string, std::map<std::string, std::string> >settings_;
	};
}
#endif

ReConfig.cpp文件

#include "RrConfig.h"
#include <fstream>
#include <stdlib.h>

namespace rr
{
    
    

	bool RrConfig::IsSpace(char c)
	{
    
    
		if (' ' == c || '\t' == c)
			return true;
		return false;
	}

	bool RrConfig::IsCommentChar(char c)
	{
    
    
		switch (c) {
    
    
		case '#':
			return true;
		default:
			return false;
		}
	}

	void RrConfig::Trim(std::string & str)
	{
    
    
		if (str.empty())
		{
    
    
			return;
		}
		int i, start_pos, end_pos;
		for (i = 0; i < str.size(); ++i) {
    
    
			if (!IsSpace(str[i])) {
    
    
				break;
			}
		}
		if (i == str.size())
		{
    
    
			str = "";
			return;
		}
		start_pos = i;
		for (i = str.size() - 1; i >= 0; --i) {
    
    
			if (!IsSpace(str[i])) {
    
    
				break;
			}
		}
		end_pos = i;
		str = str.substr(start_pos, end_pos - start_pos + 1);
	}

	bool RrConfig::AnalyseLine(const std::string & line, std::string& section, std::string & key, std::string & value)
	{
    
    
		if (line.empty())
			return false;
		int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
		if ((pos = line.find("#")) != -1)
		{
    
    
			if (0 == pos)
			{
    
    
				return false;
			}
			end_pos = pos - 1;
		}
		if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
		{
    
    
			section = line.substr(s_startpos + 1, s_endpos - 1);
			return true;
		}
		std::string new_line = line.substr(start_pos, start_pos + 1 - end_pos);
		if ((pos = new_line.find('=')) == -1)
			return false;
		key = new_line.substr(0, pos);
		value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
		Trim(key);
		if (key.empty()) {
    
    
			return false;
		}
		Trim(value);
		if ((pos = value.find("\r")) > 0)
		{
    
    
			value.replace(pos, 1, "");
		}
		if ((pos = value.find("\n")) > 0)
		{
    
    
			value.replace(pos, 1, "");
		}
		return true;
	}

	bool RrConfig::ReadConfig(const std::string & filename)
	{
    
    
		settings_.clear();
		std::ifstream infile(filename.c_str());//构造默认调用open,所以可以不调用open
		//std::ifstream infile;
		//infile.open(filename.c_str());
		//bool ret = infile.is_open()
		if (!infile) {
    
    
			return false;
		}
		std::string line, key, value, section;
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		while (getline(infile, line))
		{
    
    
			if (AnalyseLine(line, section, key, value))
			{
    
    
				it = settings_.find(section);
				if (it != settings_.end())
				{
    
    
					k_v[key] = value;
					it->second = k_v;
				}
				else
				{
    
    
					k_v.clear();
					settings_.insert(std::make_pair(section, k_v));
				}
			}
			key.clear();
			value.clear();
		}
		infile.close();
		return true;
	}

	std::string RrConfig::ReadString(const char* section, const char* item, const char* default_value)
	{
    
    
		std::string tmp_s(section);
		std::string tmp_i(item);
		std::string def(default_value);
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::string>::iterator it_item;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		it = settings_.find(tmp_s);
		if (it == settings_.end())
		{
    
    
			//printf("111");
			return def;
		}
		k_v = it->second;
		it_item = k_v.find(tmp_i);
		if (it_item == k_v.end())
		{
    
    
			//printf("222");
			return def;
		}
		return it_item->second;
	}

	int RrConfig::ReadInt(const char* section, const char* item, const int& default_value)
	{
    
    
		std::string tmp_s(section);
		std::string tmp_i(item);
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::string>::iterator it_item;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		it = settings_.find(tmp_s);
		if (it == settings_.end())
		{
    
    
			return default_value;
		}
		k_v = it->second;
		it_item = k_v.find(tmp_i);
		if (it_item == k_v.end())
		{
    
    
			return default_value;
		}
		return atoi(it_item->second.c_str());
	}

	float RrConfig::ReadFloat(const char* section, const char* item, const float& default_value)
	{
    
    
		std::string tmp_s(section);
		std::string tmp_i(item);
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::string>::iterator it_item;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		it = settings_.find(tmp_s);
		if (it == settings_.end())
		{
    
    
			return default_value;
		}
		k_v = it->second;
		it_item = k_v.find(tmp_i);
		if (it_item == k_v.end())
		{
    
    
			return default_value;
		}
		return atof(it_item->second.c_str());
	}
}

测试main.cpp
The premise is to first configure an .ini file in the path you want, mine is config.ini.

#include <iostream>
#include "RrConfig.h"
#include <fstream>
#include <cassert>

int main() {
    
    

	rr::RrConfig config;
	bool ret = config.ReadConfig("config.ini.txt");
	if (ret == false) {
    
    
		printf("ReadConfig is Error,Cfg=%s", "config.ini");
		return 1;
	}
	std::string HostName = config.ReadString("MYSQL", "HostName", "");
	int Port = config.ReadInt("MYSQL", "Port", 0);
	std::string UserName = config.ReadString("MYSQL", "UserName", "");

	std::cout << "HostName=" << HostName << std::endl;
	std::cout << "Port=" << Port << std::endl;
	std::cout << "UserName=" << UserName << std::endl;

	return 0;
}

ini file content:
Insert picture description here

result:
Insert picture description here

5 Summary I
encountered a problem during the test. In fact, I encountered it a long time ago, that is, if the opened file does not carry .txt, the file cannot be opened and it keeps returning false. I don't know if you will encounter this problem. , Here is also a record to avoid everyone wasting time.
After reading this article, I can help you solve the project requirements for ini configuration.

//bool ret = config.ReadConfig("config.ini");//error
bool ret = config.ReadConfig("config.ini.txt");

6 Configuration file parsing library
1) libconfig, C++ version is the
first article of libconfig++ libconfig-user guide
http://blog.csdn.net/crazyhacking/article/details/9668981

2) C++ Json parsing library: jsoncpp and boost.
http://www.cnblogs.com/lidabo/archive/2012/10/31/2748026.html
Since there is no requirement for json in the project, here is just a record. Have a chance to play again.

Guess you like

Origin blog.csdn.net/weixin_44517656/article/details/109014236