c++实现读取配置文件

编程实例:

CUtil.h

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
std::string Ltrim(std::string& str)
{
	if(str.empty())
	{
		return str;
	}
	
	return str.erase(0, str.find_first_not_of(" "));
}

std::string& Rtrim(std::string& str)
{
	if(str.empty())
		return str;

	return str.erase(str.find_last_not_of(" ")+1);
}

std::string& Trim(std::string& str)
{
	str.erase(0, str.find_first_not_of(" "));
	str.erase(str.find_last_not_of(" ")+1);
	return str;
}

class CConfig
{
public:
	static std::string getValueString(std::string configFile, std::string section, std::string key, std::string defaultvalue);
	static int getValueInt(std::string configFile, std::string section, std::string key, int defaultvalue);
	static bool getValueBool(std::string configFile, std::string section, std::string key, bool defaultvalue);
};

std::string CConfig::getValueString(std::string configFile, std::string section, std::string key, std::string defaultvalue)
{
	if(configFile.empty() || section.empty() || key.empty())
	{
		return defaultvalue;
	}

	std::fstream fin(configFile.c_str(), std::ios::in);
	if(!fin)
	{
		return defaultvalue;
	}
	char buf[256];
	bool isFind = false;
	int index = 0;
	std::string strline, value, cursecton, curkey;
    while(!fin.eof())
	{
		fin.getline(buf, 256);
		strline = buf;
		Trim(strline);
		switch(strline[0])
		{
		case '#':
		case ';':
			continue;
		case '[':
			index = strline.find_first_of(']');
			if(index == std::string::npos)
				continue;
            cursecton = strline.substr(1, index -1);
			Trim(cursecton);
			break;
		default:
			if(cursecton != section)
				continue;
			index = strline.find_first_of('=');
			curkey = strline.substr(0, index);
			Trim(curkey);
			if(curkey == key)
			{
				value = strline.substr(index+1);
				Trim(value);
				isFind = true;
			}
		}
	}
	fin.close();
	if(isFind)		
		return value;
	else
		return defaultvalue;
} 

int CConfig::getValueInt(std::string configFile, std::string section, std::string key, int defaultvalue)
{
	char buf[256];
	sprintf(buf, "%d", defaultvalue);
	return atoi(getValueString(configFile, section, key, buf).c_str());
}

bool CConfig::getValueBool(std::string configFile, std::string section, std::string key, bool defaultvalue)
{
	std::string res;
	res = getValueString(configFile, section, key, defaultvalue?"true":"false");
	if(res == "true")
		return true;
    else
		return false;
}

TestUtil.cpp

#include "CUtil.h"

int main(int argc, char *argv[])
{
	std::string str = "    haha  dd  ";
	std::cout << str.size() << "\n";
	std::cout << Ltrim(str) << std::endl;
	std::cout << str.size() << "\n";
	std::cout << Rtrim(str) << std::endl;
	std::cout << str.size() << "\n";
	
	std::string v1 = CConfig::getValueString("./config", "test1", "key1", "sd");
	int v2 = CConfig::getValueInt("./config", "test2", "key2", 9);
	bool v3 = CConfig::getValueBool("./config", "test3","key3", true);

	std::cout << v1 << " "<< v2 << " " << v3 << "\n";
    return 0;
}

编译:

g++ -g -o testUtil CUtil.h TestUtil.cpp


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值