Exemplo n.º 1
0
bool try_save_config(const string& fname, GLEInterface* iface, bool isUser) {
	ConfigCollection* collection = iface->getConfig()->getRCFile();
	if (collection->allDefaults()) {
		return true;
	}
	if (fname == "") {
		return false;
	}
	if (IsAbsPath(fname)) {
		std::string dirname;
		GetDirName(fname, dirname);
		EnsureMkDir(dirname);
	}
	ofstream fout(fname.c_str());
	if (!fout.is_open()) {
		return false;
	}
	CmdLineOption* versionOption = collection->getSection(GLE_CONFIG_GLE)->getOption(GLE_CONFIG_GLE_VERSION);
	ostringstream out;
	out << "Save configuration to: '" << fname << "'";
	GLEOutputStream* output = iface->getOutput();
	output->println(out.str().c_str());
	for (int i = 0; i < collection->getNbSections(); i++) {
		ConfigSection* sec = collection->getSection(i);
		if (!sec->allDefaults()) {
			fout << "begin config " << sec->getName() << endl;
			for (int j = 0; j < sec->getNbOptions(); j++) {
				CmdLineOption* option = sec->getOption(j);
				if (!option->allDefaults() && (!isUser || option != versionOption)) {
					fout << "\t" << option->getName() << " = ";
					for (int k = 0; k < option->getMaxNbArgs(); k++) {
						if (k != 0) fout << " ";
						CmdLineOptionArg* arg = option->getArg(k);
						arg->write(fout);
					}
					fout << endl;
				}
			}
			fout << "end config" << endl << endl;
		}
	}
	fout.close();
	return true;
}
Exemplo n.º 2
0
// This is a rather ugly and slow way to delete a section.  It can leave stray
// comments in the middle of sections, but will remove the section entirely,
// even if it is multiply declared.  This whole function was a bit of an
// afterthought, since it isn't very likely to be used in the application this
// class was written for.  Sorry to all the purists out there.  :-(
void SimpleConfig::delSection(std::string const &section_name) {
	ConfigDictionary::iterator i = m_last.find(section_name);
	if (i != m_last.end()) {
		ElementList rebuild;
		ElementList::iterator at;
		bool blank = false;
		for (at = m_elements.begin(); at != m_elements.end(); at++) {
			ConfigSection *section = dynamic_cast<ConfigSection *>(*at);
			if (section && section->getName() == section_name) {
				delete *at;
				continue;
			}
			ConfigValue *value = dynamic_cast<ConfigValue *>(*at);
			if (value && value->getSection() == section_name) {
				std::string index = _hash_index(section_name, value->getKey());
				ConfigDictionary::iterator i = m_dict.find(index);
				if (i != m_dict.end()) m_dict.erase(i);
				delete *at;
				continue;
			}
			ConfigComment *comment = dynamic_cast<ConfigComment *>(*at);
			if (comment && comment->getComment() == "") {
				if (blank) {
					delete *at;
					continue;
				}
				blank = true;
			} else blank = false;
			rebuild.push_back(*at);
		}
		m_elements = rebuild;
		m_modified = true;
		if (m_autosave) save();
		m_last.erase(i);
	}
}