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

📄 chini.cpp

📁 此文档是用vistual studio 2005 开发的用来描述3D-tree 的生长过程
💻 CPP
字号:
#include "stdafx.h"
#include "ChIni.h"

ChIni::ChIni( char* fileName ) {
	this->fileName = fileName;
}

ChIni::~ChIni() {
}

string ChIni::readValueAsString( string section, string key ) {
	try {
		m_listini.erase( m_listini.begin(), m_listini.end() );
		if ( !readFile() ) {
			cout << "Read File Error!" << fileName << endl;
			return "";
		}
		section = "[" + section + "]";
		LIST_INI::iterator it;
		for ( it = m_listini.begin(); it != m_listini.end(); it++ ) {
			_node& t_node = *it;
			if ( t_node.section == section ) {  //  find section
				MAP_KEY_VALUE& map_node = t_node.section_node;
				MAP_KEY_VALUE::iterator mapit = map_node.find( key );
				if ( mapit == map_node.end() ) {  // can't find key
					return "";
				} else {  // find key
					return mapit->second;
				}
			}
		}
		return "";  //  can't find section
	}
	catch ( ... ) {
		return "";
	}
}

int ChIni::readValueAsInt( string section, string key ) {
	string tmp = readValueAsString( section, key );
	return atoi( tmp.c_str() );
}

double ChIni::readValueAsDouble( string section, string key ) {
	string tmp = readValueAsString( section, key );
	return atof( tmp.c_str() );
}

bool ChIni::readValueAsBoolean( string section, string key ) {
	string tmp = readValueAsString( section, key );
	if ( tmp == "true" ) {
		return true;
	} else {
		return false;
	}
}

bool ChIni::writeValue( string section, string key, string value ) {
	try {
		m_listini.erase( m_listini.begin(), m_listini.end() );
		readFile();
		bool IsSet( false );
		section = "[" + section + "]" ;
		LIST_INI::iterator it;
		for ( it = m_listini.begin(); it != m_listini.end(); it++ ) {
			_node& t_node = *it;
			if ( t_node.section == section ) {
				MAP_KEY_VALUE& map_node = t_node.section_node;
				map_node[ key ] = value;
				IsSet = true;
			}
		}
		if ( !IsSet ) {
			_node t_node;
			t_node.section = section;
			t_node.section_node[ key ] = value;
			m_listini.push_back( t_node );
		}
		if ( writeFile() ) {
			return false;
		}
	return true;
	}
	catch ( ... ) {
		return false;
	}
}

bool ChIni::writeValue( string section, string key, int value ) {
	ostringstream stramStr;
	stramStr << value;
	string str = stramStr.str();
	return writeValue( section, key, str );
}

bool ChIni::writeValue( string section, string key, float value ) {
	ostringstream stramStr;
	stramStr << value;
	string str = stramStr.str();
	return writeValue( section, key, str );
}

bool ChIni::writeValue( string section, string key, double value ) {
	ostringstream stramStr;
	stramStr << value;
	string str = stramStr.str();
	return writeValue( section, key, str );
}

bool ChIni::writeValue( string section, string key, bool value ) {
	string str = "false";
	if ( value ) {
		str = "true";
	}
	return writeValue( section, key, str );
}

bool ChIni::readFile() {
	try {
		bool IsHead = true;
		ifstream fi( fileName.c_str() );

		_node node;
		MAP_KEY_VALUE& a_node = node.section_node;

		char buf[ 1024 ];
		while ( fi.getline( buf, 1024 ) ) {
			if ( buf[ 0 ] == '[' ) {
				if ( IsHead ) {
					IsHead = false;
				} else {
					m_listini.push_back( node );
					node.section = "";
					node.section_node.empty();
				}
				node.section = buf;
				node.section.erase( remove_if( node.section.begin(), node.section.end(), bind2nd( equal_to<char>(), '\n' ) ), node.section.end() );
				node.section.erase( remove_if( node.section.begin(), node.section.end(), bind2nd( equal_to<char>(), '\r' ) ), node.section.end() );

			} else {
				string tmp;	
				tmp  = buf;
				tmp.erase( remove_if( tmp.begin(), tmp.end(), bind2nd( equal_to<char>(), '\n' ) ), tmp.end() );
				tmp.erase( remove_if( tmp.begin(), tmp.end(), bind2nd( equal_to<char>(), '\r' ) ), tmp.end() );
				size_t it = tmp.find( '=' );
				if ( it == tmp.size() ) {
					continue;
				}
				string key( tmp, 0, it );
				string value( tmp, it + 1, tmp.size() );
				a_node[ key ] = value;
			}
		}
		if ( !node.section.empty() ) {
			m_listini.push_back( node );
		}
		return true;
	}
	catch ( ... ) {
		return false;
	}
}

bool ChIni::writeFile() {
	try {
		ofstream fo( fileName.c_str() );
		LIST_INI::iterator it;
		for ( it = m_listini.begin(); it != m_listini.end(); it++ ) {
			_node& node = *it;
			fo << node.section << endl;
			MAP_KEY_VALUE::iterator map_it;
			MAP_KEY_VALUE& map_node = node.section_node;
			for ( map_it = map_node.begin(); map_it != map_node.end(); map_it++ ) {
				fo << map_it->first << "=" << map_it->second << endl;
			}
		}
		return true;
	}
	catch (...) {
		return false;
	}
}

⌨️ 快捷键说明

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