Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » UppHub » Simple XML keymap parser
Re: Simple XML keymap parser [message #24531 is a reply to message #24512] Sat, 23 January 2010 05:35 Go to previous messageGo to previous message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

Another variant:

#ifndef XML_CONFIG_HPP
#define XML_CONFIG_HPP

#include <Core/Core.h>
using namespace Upp;

template<class K, class V>
class XMLConfig : public VectorMap<K, V>
{
public:
	XMLConfig(const String& name = "", const String& lang = "EN-EN")
		: Name(name), Language(::LNGFromText(lang)) {}

	XMLConfig& Set(const K& param, const V& value);
	XMLConfig& SetLanguage(const String& lang);
	XMLConfig& Dump();

	void Xmlize(XmlIO xml);
	bool Load(const String& path = "");
	bool Save(const String& path = "");

	String Name;
	String Path;
	int Language;
};

template<class K, class V>
inline XMLConfig<K, V>& XMLConfig<K, V>::SetLanguage(const String& lang)
{
	Language = ::LNGFromText(lang);
	return *this;
}

template<class K, class V>
inline bool XMLConfig<K, V>::Load(const String& path)
{
	Path = path;
	return path == "" ? LoadFromXMLFile(*this) : LoadFromXMLFile(*this, path);
}

template<class K, class V>
inline bool XMLConfig<K, V>::Save(const String& path)
{
	Path = path;
	return path == "" ? StoreAsXMLFile(*this, "data") : StoreAsXMLFile(*this, "data", path);
}

template<class K, class V>
inline XMLConfig<K, V>& XMLConfig<K, V>::Set(const K& param, const V& value)
{
	if (VectorMap<K, V>::Find(param) >= 0) Get(param) = value; else Add(param, value);
	return *this;
}

template<class K, class V>
inline void XMLConfig<K, V>::Xmlize(XmlIO xml)
{
	xml("name", Name)("map", *((VectorMap<K, V>*)this));
	XmlizeLang(xml, "language", Language);
}

template<class K, class V>
inline XMLConfig<K, V>& XMLConfig<K, V>::Dump()
{
	DUMP(Name);
	DUMP(Path);
	for (int i = 0; i < VectorMap<K, V>::GetCount(); i++)
		LOG(AsString(VectorMap<K, V>::GetKey(i)) + " = " +
			AsString(VectorMap<K, V>::Get(VectorMap<K, V>::GetKey(i))));
	return *this;
}

#endif // .. XML_CONFIG_HPP


Example:
#include "XMLConfig.hpp"

CONSOLE_APP_MAIN
{
	if (!FileExists(ConfigFile("XMLConfig.xml")))
	{
		XMLConfig<String, String> x("Test configuration!", "RU-RU");
		x.Set("K01", "V01");
		x.Set("K02", "V02");
		x.Save();
	}
}
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: GetUppFromDir - Creates Upp package for given directory
Next Topic: New Bazaar web page
Goto Forum:
  


Current Time: Thu May 09 21:00:36 CEST 2024

Total time taken to generate the page: 0.02280 seconds