Ejemplo n.º 1
0
TParam* TSection::add(std::string strName, std::string strComment)
{
	std::map<std::string, TParam*>::iterator found = m_mParams.find(trim(strName));

	if(found == m_mParams.end())
	{
		TParam* param = new TParam(strName);

		param->comment()   = strComment;
		m_mParams[strName] = param;

		return param;
	}

	return found->second;
}
Ejemplo n.º 2
0
void TIniFile::saveFile(std::string strFilename)
{
	std::fstream file;

	file.open(trim(strFilename).c_str(), std::fstream::out | std::fstream::trunc);

	if(!file.is_open())
		return;

	std::map<std::string, TSection*>::iterator iter = m_mSections.begin();
	TParam* param = NULL;

	if(!m_strHeader.empty())
	{
		size_t      pos;
		std::string str_aux = m_strHeader;
		while((pos = str_aux.find('\n')) != std::string::npos)
		{
			file << "# " << str_aux.substr(0, pos) << std::endl;
			str_aux = str_aux.substr(pos + 1);
		}
		file << "# " << str_aux.substr(0, pos) << std::endl << std::endl;
	}

	while(iter != m_mSections.end())
	{
		if(iter->second)
		{
			if(iter != m_mSections.begin())
				file << std::endl;

			if(iter->second->comment().size())
			{
				size_t      pos;
				std::string comment = iter->second->comment();
				while((pos = comment.find('\n')) != std::string::npos)
				{
					file << "# " << comment.substr(0, pos) << std::endl;
					comment = comment.substr(pos + 1);
				}
				file << "# " << comment.substr(0, pos) << std::endl;
			}

			file << "[" << iter->second->name() << "]" << std::endl;
			for(size_t i = 0; i < iter->second->size(); ++i)
			{
				param = (*(iter->second))[i];
				if(param)
				{
					file << param->name() << "=" << param->value().str();
					if(param->comment().size())
						file << " # " << param->comment();
					file << std::endl;
				}
			}
		}

		++iter;
	}

	file.close();
}