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 » U++ Library support » U++ Core » Xmlize and double
Xmlize and double [message #739] Mon, 30 January 2006 07:55 Go to next message
pivica is currently offline  pivica
Messages: 57
Registered: November 2005
Location: Belgrade, Serbia
Member
How to save and load double into xml. On way to save double is to convert him to String, but when loading I can't find a way to convert that String back to double.



class SomeClass
{
  double d;
  Vector<double> vd;

  void Xmlize(XmlIO xml)
  {
    String sd;
    Vector<String> svd;
	
    if (xml.IsStoring())
    {
      sd = AsString(d);
      int n = vd.GetCount();
      for (int i = 0; i < n; ++i)
        svd.Add(AsString(vd[i]));
    }

    xml
     ("d", sd)
     ("vd", svd)
    ;
	
    if (xml.IsLoading())
    {
    // now what???
    }
  }
}
Re: Xmlize and double [message #741 is a reply to message #739] Mon, 30 January 2006 11:53 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, we should definitely add "xmlizers" for "double":

template<> inline String XmlAttrStore(const double& var)
{
return AsString(var);
}

template<> inline void XmlAttrLoad(double& var, const String& text)
{
CParser p(text);
if(p.IsDouble()) var = p.ReadDouble();
}

template<> inline
void Xmlize(XmlIO xml, double& var)
{
xml.Attr("value", var);
}

In fact, it is just my ommision that I forgot to have them in Xmlize. They are now there...

This also shows one convenient way how convert String to double...

Mirek
Re: Xmlize and double [message #756 is a reply to message #741] Tue, 31 January 2006 00:47 Go to previous messageGo to next message
pivica is currently offline  pivica
Messages: 57
Registered: November 2005
Location: Belgrade, Serbia
Member
Mirek thanks very much for your help.
Re: Xmlize and double [message #882 is a reply to message #756] Mon, 06 February 2006 23:16 Go to previous messageGo to next message
pivica is currently offline  pivica
Messages: 57
Registered: November 2005
Location: Belgrade, Serbia
Member
There isn't CParser::IsDouble() method but there is IsNumber(). Is it OK to use it.

Xmlize for bool variables is missing. It's not big problem, just convert bool to int and back, but it would be nice to have it.
Problems with null doubles and Xmlize in 602b version. [message #903 is a reply to message #882] Tue, 07 February 2006 16:55 Go to previous messageGo to next message
pivica is currently offline  pivica
Messages: 57
Registered: November 2005
Location: Belgrade, Serbia
Member
I've just downloaded 602b. Xmlize class for doubles works OK when numbers are not DOUBLE_NULL. But in application which I am developing user can leave empty EditDouble ctrls and in that case double from that ctrls has DOUBLE_NULL value. When storing with Xmlize such double value has empty string value, but when trying to load it XmlError("expected floating point number") is raised.

For example:

class A
{
public:
	void Xmlize(XmlIO xml) { xml ("d", d); }
	double d;
};

//...

EditDouble ctrl;
ctrl.Clear();

A a;
a.d = ctrl;	// Now d has DOUBLE_NULL value
StoreAsXMLFile(a, "a", "a.xml");
LoadFromXMLFile(a, "a.xml"); // return false!


"a.xml" after StoreAsXMLFile is something like this:

<a>
<d value=""/>
</a>

Problem is in Xmlize.cpp in XmlAttrLoad(double& var, const String& text) method, XmlError is raised when text is empty.

template<> void XmlAttrLoad(double& var, const String& text)
{
	CParser p(text);
	if(!p.IsDouble())
		throw XmlError("expected floating point number");
	var = p.ReadDouble();
}


My temporary workaround is to determine if text is empty:

template<> void XmlAttrLoad(double& var, const String& text)
{
	CParser p(text);
	/////
	if (text.IsEmpty())
	{
		var = DOUBLE_NULL;
		return;
	}
	/////
	if(!p.IsDouble())
		throw XmlError("expected floating point number");
	var = p.ReadDouble();
}


Same problem maybe exist in XmlAttrLoad(int& var, const String& text) and XmlAttrLoad(dword& var, const String& text). They are also throwing XmlError.
Re: Problems with null doubles and Xmlize in 602b version. [message #906 is a reply to message #903] Tue, 07 February 2006 20:15 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Hm, stupid me. NULL is something I have not considered there....

Thanks.

Mirek
Re: Problems with null doubles and Xmlize in 602b version. [message #907 is a reply to message #906] Tue, 07 February 2006 20:35 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Should be now fixed (and "bool" added).

Mirek
Previous Topic: signature of the stou function
Next Topic: Serbian translation
Goto Forum:
  


Current Time: Sun May 05 01:22:11 CEST 2024

Total time taken to generate the page: 0.02836 seconds