c++读取配置文件.ini文件

  • Post author:
  • Post category:其他


RrConfig.h

#ifndef RR_CONFIG_H_
#define RR_CONFIG_H_
#include <string>
#include <map>

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_;
};



class RrConfigMgr
{
protected:
	RrConfigMgr();

public:
	static RrConfigMgr& GetInstance();

	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);
	virtual ~RrConfigMgr();
	
private:
	static RrConfigMgr m_oInstance;
	RrConfig config;

};


#endif


RrConfig.cpp

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

#ifdef _WIN32

#include <Windows.h>
#endif

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());
	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())
	{
		return def;
	}
	k_v = it->second;
	it_item = k_v.find(tmp_i);
	if (it_item == k_v.end())
	{
		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());
}

std::string GetProgramDir()

{
#ifdef _WIN32
	char exeFullPath[MAX_PATH] = { 0 }; // Full path   
	std::string strPath = "";
	GetModuleFileNameA(NULL,exeFullPath, MAX_PATH);
	strPath = (std::string)exeFullPath;    // Get full path of the file   
	int pos = strPath.find_last_of('\\', strPath.length());
	return strPath.substr(0, pos);  // Return the directory without the file name  
#else

	return "./";

#endif

}
RrConfigMgr RrConfigMgr::m_oInstance;
RrConfigMgr::RrConfigMgr()
{
	std::string firstName = GetProgramDir();
	std::string lastName = "config.ini";
	std::string allName = firstName + "./" + lastName;
	bool ret = config.ReadConfig(allName);
	if (ret)
	{
		printf("ReadConfig config.ini succeed .. \n");
	}
}

RrConfigMgr &RrConfigMgr::GetInstance()
{
	return m_oInstance;
}

std::string RrConfigMgr::ReadString(const char * section, const char * item, const char * default_value)
{
	return config.ReadString(section, item, default_value);
}

int RrConfigMgr::ReadInt(const char * section, const char * item, const int & default_value)
{
	return config.ReadInt(section, item, default_value);
}

float RrConfigMgr::ReadFloat(const char * section, const char * item, const float & default_value)
{
	return config.ReadFloat(section, item, default_value);;
}

RrConfigMgr::~RrConfigMgr()
{
}


main.cpp

#include <stdio.h>
#include "RrConfig.h"
int main()
{
	int port  = RrConfigMgr::GetInstance().ReadInt("MYSQL", "Port", 0);
	std::string HostName = RrConfigMgr::GetInstance().ReadString("MYSQL", "HostName", "");
	float Scale = RrConfigMgr::GetInstance().ReadFloat("MYSQL", "Scale", 10.01);

	printf("prot:%d HostName:%s Scale:%f .. \n", port, HostName.c_str(), Scale);
	return 0;
}

读取的config.ini文件

[MYSQL]

HostName=127.0.0.1

Port=3306

Scale=10.322



版权声明:本文为baidu_31872269原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。