Home » U++ Library support » U++ Core » DeXml() incomplete
| DeXml() incomplete [message #26960] |
Mon, 14 June 2010 00:35  |
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("<");
else if(*s == '>') result.Cat(">");
else if(*s == '&') result.Cat("&");
else if(*s == '\'') result.Cat("'");
else if(*s == '\"') result.Cat(""");
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

</MyTextTag>
</MY_DOC_TAG>
-----------
-----------DeXml() =
length=28
‰PNG


-----------
-----------DeXml(CHARSET_DEFAULT, true) =
length=28
‰PNG


-----------
-----------MyDeXml() =
length=837
‰PNG

���
IHDR���0���-���è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`‚
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 #26967 is a reply to message #26963] |
Tue, 15 June 2010 20:11  |
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
|
|
|
|
Goto Forum:
Current Time: Sun Nov 15 23:58:48 GMT+1 2026
Total time taken to generate the page: 0.02200 seconds
|