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 #24588 is a reply to message #24512] Mon, 25 January 2010 11:02 Go to previous messageGo to previous message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

Final code (for me =)):

#ifndef XML_CONFIG_HPP
#define XML_CONFIG_HPP

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

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

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

	int GetNumber(const String& param, int min = -32768, int max = 32767);

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

	String Name;
	String Path;
	int Language;
};

inline XMLConfig& XMLConfig::SetLanguage(const String& lang)
{
	Language = ::LNGFromText(lang);
	return *this;
}

inline bool XMLConfig::Load(const String& path)
{
	Path = path;
	return path == "" ? LoadFromXMLFile(*this) : LoadFromXMLFile(*this, path);
}

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

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

inline XMLConfig& XMLConfig::Set(const String& param, int value)
{
	return Set(param, AsString(value));
}

inline int XMLConfig::GetNumber(const String& param, int min, int max)
{
	int r = ScanInt(Get(param));
	if (r < min) r = min;
	if (r > max) r = max;
	return r;
}

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

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

#endif // .. XML_CONFIG_HPP


Best regards!
Anton

--
 
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 10:59:10 CEST 2024

Total time taken to generate the page: 0.02244 seconds