Re: [xml] Newbie question about an example from the web site



Hello,

Same problem on CentOS 7.1.

My environment contains:

23:25:30 (690) menu@ - ~ > echo $LC_TYPE 
iso_8859_1
23:25:48 (691) menu@ - ~ > echo $LANG
C
23:26:00 (692) menu@ - ~ > echo GDM_LANG
GDM_LANG

and I get:

23:27:02 (695) menu@ - ~/examples_libxml2 > ./EncodingConversionExample foo
--> content = foo
--> size = 4
--> out_size = 7
--> temp = 3
--> temp-size+1 = 0
conversion wasn't successful.
--> out = (null)
<?xml version="1.0" encoding="ISO-8859-1"?>
<root/>
23:27:20 (696) menu@ - ~/examples_libxml2 > ./EncodingConversionExample éöîà
--> content = éöîà
--> size = 9
--> out_size = 17
--> temp = 8
--> temp-size+1 = 0
conversion wasn't successful.
--> out = (null)
<?xml version="1.0" encoding="ISO-8859-1"?>
<root/>

I’m clearly doing something wrong, but what?

JM

Le 25 nov. 2015 à 13:08, Menu Jacques <imj-219 bluewin ch> a écrit :

I use:

13:07:34 (254) menu@ - ~/libxml2-git > apt list libxml2 libxml2-dev
Listing... Done
libxml2/stable,now 2.9.1+dfsg1-5 i386 [installed,automatic]
libxml2-dev/stable,now 2.9.1+dfsg1-5 i386 [installed]

on Debian 8 32bit (jessie)

JM

Le 25 nov. 2015 à 11:17, Menu Jacques <imj-219 bluewin ch> a écrit :

Hello folks,

I’ve successfully built the examples from http://xmlsoft.org/tutorial/index.html, except the last one 
about encoding conversion.

I added printouts to the original code to help (see below) and get:

10:51:35 (250) menu@ - ~/libxml2-git > ./EncodingConversionExample foo
--> content = foo
--> size = 4
--> out_size = 7
--> temp = 3
--> temp-size+1 = 0
conversion wasn't successful.
--> out = (null)
<?xml version="1.0" encoding="ISO-8859-1"?>
<root/>

Thanks for your help!

JM

—

11:15:24 (251) menu@ - ~/libxml2-git > cat EncodingConversionExample.c
#include <string.h>
#include <libxml/parser.h>


unsigned char*
convert (unsigned char *in, char *encoding)
{
     unsigned char *out;
      int ret,size,out_size,temp;
      xmlCharEncodingHandlerPtr handler;

      size = (int)strlen(in)+1; 
      out_size = size*2-1; 
      out = malloc((size_t)out_size); 

      printf("--> size = %d\n", size);
      printf("--> out_size = %d\n", out_size);

      if (out) {
              handler = xmlFindCharEncodingHandler(encoding);

              if (!handler) {
                      printf("--> %s encoding handler not found\n", encoding);
                      free(out);
                      out = NULL;
              }
      }

      if (out) {
              temp=size-1;
              printf("--> temp = %d\n", temp);

              ret = handler->input(out, &out_size, in, &temp);

              if (ret || temp-size+1) {
                      printf("--> temp-size+1 = %d\n", temp-size+1);

                      if (ret) {
                              printf("conversion wasn't successful.\n");
                      } else {
                              printf("conversion wasn't successful. converted: %i octets.\n",temp);
                      }

                      free(out);
                      out = NULL;
              } else {
                      out = realloc(out,out_size+1); 
                      out[out_size]=0; /*null terminating out*/

              }
      } else {
              printf("no mem\n");
      }
      return (out);
}    


int
main(int argc, char **argv) {

     unsigned char *content, *out;
     xmlDocPtr doc;
     xmlNodePtr rootnode;
     char *encoding = "ISO-8859-1";
     
             
     if (argc <= 1) {
             printf("Usage: %s content\n", argv[0]);
             return(0);
     }

     content = argv[1];
      printf("--> content = %s\n", content);

     out = convert(content, encoding);
      printf("--> out = %s\n", out);

     doc = xmlNewDoc ("1.0");
     rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)"root", out);
     xmlDocSetRootElement(doc, rootnode);

     xmlSaveFormatFileEnc("-", doc, encoding, 1);
     return (1);
}





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]