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 » DeXml() incomplete
DeXml() incomplete [message #26960] Mon, 14 June 2010 00:35 Go to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hi,

I've been trying to put PNG images in an XML file, but it always failed.

After some investigation it seems that DeXml function is incomplete:

It takes a 'char*' as input data and then searches for the '0' value to find end of string !

With PNG images this does not work !

If you change 'char*' to String and use Begin() & End() methods instead of searching for '0' value, then it works.

Here is a sample code that illustrates what I say:
#include <Core/Core.h>
#include <Draw/Draw.h>
#include <plugin/png/png.h>
using namespace Upp;

#define IMAGECLASS CtrlImg
#define IMAGEFILE <CtrlLib/Ctrl.iml>
#include <Draw/iml_header.h>

#define IMAGECLASS CtrlImg
#define IMAGEFILE <CtrlLib/Ctrl.iml>
#include <Draw/iml_source.h>

String MyDeXml(const String& str, byte charset, bool escapelf)
{
	if(charset == CHARSET_DEFAULT)
		charset = GetDefaultCharset();
	StringBuffer result;
	const char* s= str.Begin();
	const char* end= str.End();
	for(; s != end; s++)
		/**/ if(*s == '<')  result.Cat("&lt;");
		else if(*s == '>')  result.Cat("&gt;");
		else if(*s == '&')  result.Cat("&amp;");
		else if(*s == '\'') result.Cat("&apos;");
		else if(*s == '\"') result.Cat("&quot;");
		else if((byte)*s < ' ' && (escapelf || *s != '\n' || *s != '\t'))
			result.Cat(NFormat("&#x%02x;", (byte)*s));
		else if(!(*s & 0x80) || charset == CHARSET_UTF8) result.Cat(*s);
		else result.Cat(ToUtf8(ToUnicode(*s, charset)));
	return result;
}


CONSOLE_APP_MAIN
{
			PNGEncoder pngEncoder;
			String str = pngEncoder.SaveString( CtrlImg::error() );
			
			XmlNode xmlDoc;
			XmlNode& myDoc = xmlDoc.Add("MY_DOC_TAG");
			
			XmlNode& myTextTag = myDoc.Add("MyTextTag");
			myTextTag.AddText( str );
			
			LOG("----------- STR() =");
			LOG(String("length=") << str.GetLength());
			LOG(str);
			LOG("-----------");
			LOG("-----------AsXml() =");
			LOG(String("length=") << AsXML(xmlDoc).GetLength());
			LOG(AsXML(xmlDoc));
			LOG("-----------");
			LOG("-----------DeXml() =");
			LOG(String("length=") << DeXml(str).GetLength());
			LOG(DeXml(str));
			LOG("-----------");
			LOG("-----------DeXml(CHARSET_DEFAULT, true) =");
			LOG(String("length=") << DeXml(str, CHARSET_DEFAULT, true).GetLength());
			LOG(DeXml(str, CHARSET_DEFAULT, true));
			LOG("-----------");
			LOG("-----------MyDeXml() =");
			LOG(String("length=") << MyDeXml(str, CHARSET_DEFAULT, true).GetLength());
			LOG(MyDeXml(str, CHARSET_DEFAULT, true));
}




the resulting log file is the following:
----------- STR() =
length=419
PNG

IHDR0-JjIDAThn1g7OP6O&!dgˎl6;90ߊE c i?tv+B9偌9{np"+wmgL|-ArNpAxl	W|#"މeKl݅yEv `~H&B=gжшxs`$PDz,z6*@k "PlBgmsHt(kI'9vBȴ@[%9
਌ fH#&">ȼ7N/k\z-銤,|42hxf5I6~nJ<+!=WC⁅ћfazRIENDB`
-----------
-----------AsXml() =
length=186
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE MY_DOC_TAG>
<MY_DOC_TAG>
	<MyTextTag xml:spaces="preserve">PNG&#x0d;&#x0a;&#x1a;&#x0a;</MyTextTag>
</MY_DOC_TAG>

-----------
-----------DeXml() =
length=28
PNG&#x0d;&#x0a;&#x1a;&#x0a;
-----------
-----------DeXml(CHARSET_DEFAULT, true) =
length=28
PNG&#x0d;&#x0a;&#x1a;&#x0a;
-----------
-----------MyDeXml() =
length=837
PNG&#x0d;&#x0a;&#x1a;&#x0a;&#x00;&#x00;&#x00;&#x0d;IHDR&#x00;&#x00;&#x00;0&#x00;&#x00;&#x00;-&#x08;&#x06;&#x00;&#x00;&#x00;J&#x00;&#x00;&#x01;jIDAThn&#x02;1&#x0c;g7O&#x1e;P6O&amp;!&#x01;dg&#x1c;ˎ&#x02;l6;9&#x0b;&#x12;0ߊE c i&#x08;&#x15;?tv+B9&#x03;偌&#x0f;9{np&quot;+wmgL|-ArNpAx&#x07;&#x04;l&#x09;W|#&quot;މeKl&#x18;&#x1b;݅y&#x03;E&#x12;v&#x1f;&#x08;&#x19; `~H&#x01;&#x00;&#x19;&amp;B=&#x13;gжшxs`$P&#x0d;,z&#x03;6*@k &quot;PlBgmsHt&#x0e;&#x1c;(kI&#x13;&apos;9&#x10;vBȴ@[%9&#x0c;&#x0a;&#x1b;਌ f&#x07;H&#x00;#&amp;&quot;&#x03;&#x1f;&gt;ȼ&#x01;7&#x01;N&#x05;&#x1f;&#x11;&#x0f;&#x14;/k\z-銤,|42hxf5I6~nJ&lt;+!=WC⁅&#x19;ћf&#x03;azR&#x06;&#x00;&#x00;&#x00;&#x00;IENDB`



Note: there is a 100% growth factor on the data size when converted to xml: this could be optimized using base 64 encoding for example (30% growth factor)

I suggest adding overloads with 'String' parameter for some functions/methods in order to avoid this drawback.


Re: DeXml() incomplete [message #26963 is a reply to message #26960] Mon, 14 June 2010 10:23 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, while I find it a little bit tricky to put binary data into XML directly, I have added proposed DeXml variants to amend this problem.

Mirek
Re: DeXml() incomplete [message #26967 is a reply to message #26963] Tue, 15 June 2010 20:11 Go to previous message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hi Mirek,

Thanks for the correction, it works fine: I can write my xml file with images just fine.

But when I wan't to open the file .... I get a "Unexpected end of file" exception.

I'll look into it tonight


When finished, I'm gonna post an example app with the following things:
- Image editing using the fresh EditImage ctrl
- storing the image in database
- exporting the image in xml




[Updated on: Tue, 15 June 2010 20:15]

Report message to a moderator

Previous Topic: Thread::ShutdownThreads not safe
Next Topic: PatternMatchMulti problem
Goto Forum:
  


Current Time: Mon Apr 29 13:24:44 CEST 2024

Total time taken to generate the page: 0.02442 seconds