U++ framework
Do not panic. Ask here before giving up.

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: 740
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-èJèÂjIDATh�í˜Ín1„g«¾7³OîªPŠ6ñO&À!ŸÄÖdgËŽl6›Í;9Ô0ç¹ô�ߊEø úæ‡ßc¹ �i˜?tv+B9íå�Œ9ÿÖ{Ýnp"ë+wãmâgL|é-ÎArÝNp”A³x¶±l‰	W|#"Þ‰e¢”æKÈlü½Ý…yÇE÷»võÛÕˆ˜ `Ã~ï™HŠ¿‘&ìB=…Ìgжш°¤x¯Œôs`$P˜ùÆÇ²,z™6*@k "PlBg ×m¢s¢Htç(Àk•I'ðÂ9íó‹vB”È´Ñ@[%9
਌š�Ì ëÄfÊHî#&"ˆÄ>ȼòŠ7ôN§ž³/¶øk\z­’-�ÆÔ銤�,›èÕ|4û€à2©™hxfþ5�Iñ€ð6ì~n÷J¡ó<+¼!= W¯Cªâ�…÷‘ž™Ñ›Íf³‘ñ£´aázR´IEND®B`‚
-----------
-----------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;jIDATh�í˜Ín&#x02;1&#x0c;„g«¾7³Oî&#x1e;ªPŠ6ñO&amp;À!ŸÄ&#x01;Ödg&#x1c;ËŽ&#x02;l6›Í;9Ô&#x0b;&#x12;0ç¹ô�ߊEø úæ‡ßc¹ �i&#x08;˜&#x15;?tv+B9&#x03;íå�Œ&#x0f;9ÿÖ{Ýnp&quot;ë+wãmâgL|é-ÎArÝNp”A³x¶&#x07;±&#x04;l‰&#x09;W|#&quot;Þ‰e¢”æKÈlü½&#x18;&#x1b;Ý…y&#x03;ÇE÷»&#x12;võÛÕ&#x1f;ˆ˜&#x08;&#x19; `Ã~ï™HŠ¿&#x01;&#x00;&#x19;‘&amp;ìB=&#x13;…Ìgжш°¤x¯Œôs`$P˜ùÆÇ&#x0d;²,z&#x03;™6*@k &quot;PlBg ×m¢s¢Ht&#x0e;&#x1c;ç(Àk•I&#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­’-�ÆÔ銤�,›èÕ|4û€à2©™hxfþ5�Iñ€ð6ì~n÷J¡ó&lt;+¼!= W¯Cªâ�…÷‘ž™&#x19;Ñ›Íf³‘ñ&#x03;£´aázR&#x06;´&#x00;&#x00;&#x00;&#x00;IEND®B`‚



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: 14291
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: 740
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: Sun Nov 15 23:24:47 GMT+1 2026

Total time taken to generate the page: 0.00626 seconds