⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfgmanage.cpp

📁 用STL模板库实现的配置文件读写库
💻 CPP
字号:
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceE/DisableWarnings.h>
#include <IceE/StringUtil.h>
#include <IceE/Initialize.h>
#include <IceE/LocalException.h>
#include <iostream>
#include <fstream>

#include "cfgmanage.h"

using namespace std;
using namespace Ice;
using namespace IceInternal;

string ETAH::cfgManage::getProperty(const string& key)
{
	IceUtil::Mutex::Lock sync(*this);

	map<string, CfgItem>::const_iterator p = _properties.find(key);
	if (p != _properties.end())
	{
		return p->second.value;
	}
	else
	{
		return string();
	}
}

string ETAH::cfgManage::getPropertyWithDefault(const string& key,
	const string& value)
{
	IceUtil::Mutex::Lock sync(*this);

	map<string, CfgItem>::const_iterator p = _properties.find(key);
	if (p != _properties.end())
	{
		return p->second.value;
	}
	else
	{
		return value;
	}
}

Int ETAH::cfgManage::getPropertyAsInt(const string& key)
{
	return getPropertyAsIntWithDefault(key, 0);
}

Int ETAH::cfgManage::getPropertyAsIntWithDefault(const string& key, Int value)
{
	IceUtil::Mutex::Lock sync(*this);

	map<string, CfgItem>::const_iterator p = _properties.find(key);
	if (p != _properties.end())
	{
		value = atoi(p->second.value.c_str());
	}

	return value;
}


void ETAH::cfgManage::insertProperty(const string& key, const string& value,
	int line)
{
	if (key.empty())
	{
		return;
	}

	IceUtil::Mutex::Lock sync(*this);

	//
	// Set or clear the property.
	//
	if (!value.empty())
	{
		_properties[key].value = value;
		_properties[key].lineNumber = line;
	}
	else
	{
		_properties.erase(key);
	}
}

void ETAH::cfgManage::setProperty(const string& key, const string& value)
{
	if (key.empty())
	{
		return;
	}

	IceUtil::Mutex::Lock sync(*this);

	//
	// Set or clear the property.
	//
	if (!value.empty())
	{
		_properties[key].value = value;
		fileBuffer[_properties[key].lineNumber].clear();
		fileBuffer[_properties[key].lineNumber] = key + "=" + value + "\n";
	}
	else
	{
		_properties.erase(key);
	}
	modifyFlag = 1;
}

int ETAH::cfgManage::getModifyFlag(void)
{
	return modifyFlag;
}

void ETAH::cfgManage::load(const std::string& file)
{
	fileName = file;

	FILE* in = fopen(file.c_str(), "r");
	if (!in)
	{
		FileException ex(__FILE__, __LINE__);
		ex.path = file;
		ex.error = getSystemErrno();
		throw ex;
	}

	char line[1024];
	int i = 0;
	while (fgets(line, 1024, in) != NULL)
	{
		string fileLine;
		fileBuffer.push_back(fileLine.assign(line));
		parseLine(line, i);
		i++;
	}
	fclose(in);

	chkSum = calcChecksum();
}

void ETAH::cfgManage::save(void)
{
	ofstream handle(fileName.c_str(), ios::trunc);

	if (!handle)
	{
		FileException ex(__FILE__, __LINE__);
		ex.path = fileName;
		ex.error = getSystemErrno();
		throw ex;
	}

	std::vector<string>::iterator iter = fileBuffer.begin();
	for (; iter != fileBuffer.end(); iter++)
	{
		handle << *iter;
	}
	handle.close();
	modifyFlag = 0;
	chkSum = calcChecksum();
}

int ETAH::cfgManage::getCheckSum(void)
{
	return chkSum;
}

int ETAH::cfgManage::calcChecksum(void)
{
	int checkSum = 0;
	std::vector<string> tmpFileBuffer;

	FILE* in = fopen(fileName.c_str(), "r");
	if (!in)
	{
		FileException ex(__FILE__, __LINE__);
		ex.path = fileName;
		ex.error = getSystemErrno();
		throw ex;
	}

	char line[1024];
	int i = 0;
	while (fgets(line, 1024, in) != NULL)
	{
		string fileLine;
		tmpFileBuffer.push_back(fileLine.assign(line));
		i++;
	}
	fclose(in);

	std::vector<string>::iterator iter = tmpFileBuffer.begin();
	int tmp = 0, state = 0;
	for (; iter != tmpFileBuffer.end(); iter++)
	{
		string::iterator strIter = iter->begin();
		for (; strIter != iter->end(); strIter++)
		{
			if (state == 0)
			{
				tmp = (int) (*strIter) << 8;
				state = 1;
			}
			else
			{
				tmp += (int) (*strIter);
				checkSum += tmp;
				state = 0;
			}
		}
	}
	if (state == 1) // the last one even byte has not added yet
	{
		checkSum += tmp;
	}

	return checkSum;
}

void ETAH::cfgManage::parseLine(const string& line, int lineNum)
{
	const string delim = " \t\r\n";
	string s = line;

	string::size_type idx = s.find('#');
	if (idx != string::npos)
	{
		s.erase(idx);
	}

	idx = s.find_last_not_of(delim);
	if (idx != string::npos && idx + 1 < s.length())
	{
		s.erase(idx + 1);
	}

	string::size_type beg = s.find_first_not_of(delim);
	if (beg == string::npos)
	{
		return;
	}

	string::size_type end = s.find_first_of(delim + "=", beg);
	if (end == string::npos)
	{
		return;
	}

	string key = s.substr(beg, end - beg);

	end = s.find('=', end);
	if (end == string::npos)
	{
		return;
	}
	++end;

	string value;
	beg = s.find_first_not_of(delim, end);
	if (beg != string::npos)
	{
		end = s.length();
		value = s.substr(beg, end - beg);
	}

	insertProperty(key, value, lineNum);
}


void ETAH::cfgManage::printFileBuffer(void)
{
	std::vector<string>::iterator iter = fileBuffer.begin();
	for (; iter != fileBuffer.end(); iter++)
	{
		cout << *iter;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -