includeconvert.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <![CDATA[
  2. #include <string.h>
  3. #include <libxml/parser.h>
  4. unsigned char*
  5. convert (unsigned char *in, char *encoding)
  6. {
  7. unsigned char *out;
  8. int ret,size,out_size,temp;
  9. xmlCharEncodingHandlerPtr handler;
  10. size = (int)strlen(in)+1;
  11. out_size = size*2-1;
  12. out = malloc((size_t)out_size);
  13. if (out) {
  14. handler = xmlFindCharEncodingHandler(encoding);
  15. if (!handler) {
  16. free(out);
  17. out = NULL;
  18. }
  19. }
  20. if (out) {
  21. temp=size-1;
  22. ret = handler->input(out, &out_size, in, &temp);
  23. if (ret || temp-size+1) {
  24. if (ret) {
  25. printf("conversion wasn't successful.\n");
  26. } else {
  27. printf("conversion wasn't successful. converted: %i octets.\n",temp);
  28. }
  29. free(out);
  30. out = NULL;
  31. } else {
  32. out = realloc(out,out_size+1);
  33. out[out_size]=0; /*null terminating out*/
  34. }
  35. } else {
  36. printf("no mem\n");
  37. }
  38. return (out);
  39. }
  40. int
  41. main(int argc, char **argv) {
  42. unsigned char *content, *out;
  43. xmlDocPtr doc;
  44. xmlNodePtr rootnode;
  45. char *encoding = "ISO-8859-1";
  46. if (argc <= 1) {
  47. printf("Usage: %s content\n", argv[0]);
  48. return(0);
  49. }
  50. content = argv[1];
  51. out = convert(content, encoding);
  52. doc = xmlNewDoc ("1.0");
  53. rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)"root", out);
  54. xmlDocSetRootElement(doc, rootnode);
  55. xmlSaveFormatFileEnc("-", doc, encoding, 1);
  56. return (1);
  57. }
  58. ]]>