C + + implementation of log reading following ini configuration file format

Keywords: Windows

I prefer the ini format configuration file, but the ini reading classes found on the Internet that can be used directly are all made by the Windows API, and there is no way. After all, it is a log format in the windows environment, so I made a similar log reading class in the afternoon by imitating this format.

The format of the configuration file is similar to this:

[SwanQPeMicroserver1]
port=8080
IP=0.0.0.0
[SwanQPeMicroserver2]
port=8080
IP=0.0.0.0
[end]

The specific usage types are as follows:

string ss(". \ \ profile. zy");
 ConFigInTxt test(ss,"");
 test.SetConfigInfo();
 config_return_type temp_config_info_map;
 temp_config_info_map = test.GetConfigInfo();
 for (auto a : temp_config_info_map)
 {
  cout << a.first << endl;
  for (auto b:a.second)
  {
   cout << b.first << " == " << b.second << endl;
  }
 }

Where config ﹣ return ﹣ type is map < string, map < string, string > >

The operation results are as follows:

This is just a process from scratch. There are many things to do next, such as adding type (int, float,string) to the value, allowing adding comments, etc...

Code on the back, if you have any ideas, suggestions, you can teach, thank you!

#pragma once

#include  <fstream>
#include  <iostream>
#include  <iomanip>
#include  <string>
#include  <map>
using config_return_type = map<string, map<string, string>> ;
class ConFigInTxt
{
public:

	//************************************
	// Date of establishment: January 24, 2018
	// Function name: configintx
	// Full name: configintx:: configintx
	// Function Description: initialize fstream object 
	// Return Description: void
	//************************************
	ConFigInTxt();


	//************************************
	// Date of establishment: January 24, 2018
	// Function name: configintx
	// Full name: configintx:: configintx
	// Function Description:  
	// Return Description:  
	// Parameter Description: STD:: String config \ \ u name
	// Parameter Description: STD:: String config \ \ path
	//************************************
	ConFigInTxt(std::string config_name,std::string config_path );


	//************************************
	// Date of establishment: January 24, 2018
	// Function name: OpenConfig
	// Full name: configintx:: openconfig
	// Function Description: open the profile according to the passed in profile name and path
	// Return Description: bool
	// Parameter Description: STD:: String config \ \ u name
	// Parameter Description: STD:: String config \ \ path
	//************************************
	bool OpenConfig(std::string config_name, std::string config_path );

	//************************************
	// Date of establishment: January 24, 2018
	// Function name: GetConfigInfo
	// Full name: configintx:: getconfiginfo
	// Function Description: transfer information from configuration file to map
	// Return Description: bool
	//************************************
	bool SetConfigInfo();

	//************************************
	// Date of establishment: January 24, 2018
	// Function name: GetConfigInfo
	// Full name: configintx:: getconfiginfo
	// Function Description: return ConfigInfoMap
	// Return Description: STD:: Map < STD:: string, STD:: Map < STD:: string, STD:: String > >
	//************************************
	const std::map<std::string, std::map<std::string, std::string>> GetConfigInfo();

	//************************************
	// Date of establishment: January 24, 2018
	// Function name: ~ configintx
	// Full name: configintx:: ~ configintx
	// Functional description: Deconstruction: releasing resources
	// Return Description: void
	//************************************
	~ConFigInTxt();
private:
	//std::map<std::string, std::string>  item_info;
	std::map<std::string, std::map<std::string, std::string>>  config_info;
	std::string    config_name;
	std::string    config_path;
	bool           config_tag;
private:
	bool ConfigToMap();
};

#include "config_read_txt.h"


using namespace std;

ConFigInTxt::ConFigInTxt()
{
	//Use namespace std in this function 
	using namespace std;
	//The configuration object of the default construction cannot directly use the operation method
	this->config_tag = false;

}

ConFigInTxt::ConFigInTxt(std::string config_name, std::string config_path)
{

	//First, determine whether the path has been included in the config name
	if (config_name.find("\\") != string::npos || config_name.find("/") != string::npos)
	{
		//Then judge whether the config path is NULL
		if (config_path.size()){
			cout << "Duplicate path!" << endl;
			return;
		}
		else{
			clog << "[WARNING:] Path entered in profile name" << endl;
			//Extract the path from the filename to place and initialize the member variable
			string tmp_name = config_name.substr(config_name.find_last_of("\\") == string::npos ? config_name.find_last_of("/") : config_name.find_last_of("\\"));
			string tmp_path = config_name.substr(0, (config_name.find_last_of("\\") == string::npos ? config_name.find_last_of("/") : config_name.find_last_of("\\")) - string::npos);
			this->config_name = tmp_name;
			this->config_path = tmp_path;
		}
	}
	else
	{
		this->config_name = config_name;
		this->config_path = config_path;
	}

	this->config_tag = true;
}

bool ConFigInTxt::OpenConfig(std::string config_name, std::string config_path)
{
	//Use namespace std in this function 
	using namespace std;
	//First, determine whether the path has been included in the config name
	if (config_name.find("\\") != string::npos || config_name.find("/") != string::npos)
	{
		//Then judge whether the config path is NULL
		if (config_path.size()) {
			cout << "Duplicate path!" << endl;
			return false;
		}
		else {
			clog << "[WARNING:] Path entered in profile name" << endl;
			//Extract the path from the filename to place and initialize the member variable
			string tmp_name = config_name.substr(config_name.find_last_of("\\") == string::npos ? config_name.find_last_of("/") : config_name.find_last_of("\\"));
			string tmp_path = config_name.substr(0, (config_name.find_last_of("\\") == string::npos ? config_name.find_last_of("/") : config_name.find_last_of("\\")) - string::npos);
			this->config_name = tmp_name;
			this->config_path = tmp_path;
		}
	}
	else
	{
		this->config_name = config_name;
		this->config_path = config_path;
	}
	
	return true;
}

bool ConFigInTxt::SetConfigInfo()
{
	//Use namespace std in this function 
	using namespace std;
	if(!this->ConfigToMap())
			cerr << "[ERROR:] The profile is not available" << endl;
	return true;
}

const std::map<std::string, std::map<std::string, std::string>> ConFigInTxt::GetConfigInfo()
{
	return (this->config_info);
}

bool ConFigInTxt::ConfigToMap()
{
	//Use namespace std in this function 
	using namespace std;

	//Initialize ifstream
	ifstream config_ifstream(this->config_path + "\\" + this->config_name);
	//If the file fails to open, an error will be reported and the system will return
	if (!config_ifstream)
	{
		cerr << "[ERROR:] Profile open failed!" << endl;
		this->config_tag = false;
		return false;
	}
	this->config_tag = true;
	string s;
	string flag_item ("=") ;
	string flag_class_left("[");
	string flag_class_right("]");
	string temp_class ;
	string temp_item_Key;
	string temp_item_Value;
	map<string, string> temp_map ;
	while (getline(config_ifstream, s))
	{
		
		//Extract class
		if (s.find(flag_class_left) != string::npos)
		{   
			//Insert the previous itemMap first
			if((temp_map.size()!=0)&& (temp_class.size()!=0))
			{
				pair<string, map<string, string>> temp_pair(temp_class, temp_map);
				this->config_info.insert(temp_pair);
				//Clear temp map
				temp_map.clear();
				temp_class.clear();
			}
			
			temp_class = s.substr(1, s.find_first_of(flag_class_right)-1);

		}
		//Extract subsequent key values
		else if((s.find(flag_item)!=string::npos)&&(temp_class.size()!=0))
		{
			temp_item_Key = s.substr(0, s.find(flag_item));
			temp_item_Value = s.substr(s.find(flag_item)+1);
			pair<string, string> temp_pair(temp_item_Key, temp_item_Value);
			temp_map.insert(temp_pair);
		}
	}
	config_ifstream.close();
	return true;
}
ConFigInTxt::~ConFigInTxt()
{
	//Use namespace std in this function 
	using namespace std;


	clog << "[INFO:] Profile object is destructed" << endl;
}





Posted by WebDesEyen on Tue, 05 May 2020 01:33:32 -0700