Commits

ph...@9ae0c189-cd1f-4510-a509-f4891f5cf20d  committed 6b890fa

Modified Files:
dom.c
this is the correct commit, since i commited the wrong file :/
(check the last log message)

for non conform encodings we still need some examples to test. but
it should work :)

  • Participants
  • Parent commits ec46643

Comments (0)

Files changed (1)

 domEncodeString( const char *encoding, const char *string ){
     xmlCharEncoding enc;
     xmlChar *ret = NULL;
+    xmlBufferPtr in, out;
+    xmlCharEncodingHandlerPtr coder = NULL;
     
     if ( string != NULL ) {
         if( encoding != NULL ) {
             enc = xmlParseCharEncoding( encoding );
-            if ( enc > 0 ) {
-                if( enc > 1 ) {
-                    xmlBufferPtr in, out;
-                    xmlCharEncodingHandlerPtr coder ;
-                    in  = xmlBufferCreate();
-                    out = xmlBufferCreate();
-                    
-                    coder = xmlGetCharEncodingHandler( enc );
-                    
-                    xmlBufferCCat( in, string );
-                    
-                    if ( xmlCharEncInFunc( coder, out, in ) >= 0 ) {
-                        ret = xmlStrdup( out->content );
-                    }
-                    else {
-                        /* printf("encoding error\n"); */
-                    }
-                    
-                    xmlBufferFree( in );
-                    xmlBufferFree( out );
+            if ( enc > 1 ) {
+                coder= xmlGetCharEncodingHandler( enc );
+            }
+            else if ( enc == 1 ) {
+                ret = xmlStrdup( string );
+            }
+            else if ( enc == XML_CHAR_ENCODING_ERROR ){
+                coder = xmlFindCharEncodingHandler( encoding );
+            }
+            else {
+                /* fprintf(stderr, "NO XML ENCODING!\n"); */ 
+            }
+
+            if ( coder != NULL ) {
+                in    = xmlBufferCreate();
+                out   = xmlBufferCreate();
+                xmlBufferCCat( in, string );
+                if ( xmlCharEncInFunc( coder, out, in ) >= 0 ) {
+                    ret = xmlStrdup( out->content );
                 }
                 else {
-                    /* if utf-8 is requested we do nothing */
-                    ret = xmlStrdup( string );
+                    /* fprintf(stderr, "b0rked encoiding!\n"); */
                 }
+                    
+                xmlBufferFree( in );
+                xmlBufferFree( out );
             }
             else {
-                
-                /* printf( "encoding error: no enciding\n" ); */
+                /*  fprintf(stderr, "no coder found\n"); */
             }
         }
         else {
 char*
 domDecodeString( const char *encoding, const xmlChar *string){
     char *ret=NULL;
+    xmlCharEncoding enc;
     xmlBufferPtr in, out;
-    
+    xmlCharEncodingHandlerPtr coder = NULL;
+
     if ( string != NULL ) {
         if( encoding != NULL ) {
-            xmlCharEncoding enc = xmlParseCharEncoding( encoding );
-            /*      printf("encoding: %d\n", enc ); */
-            if ( enc > 0 ) {
-                if( enc > 1 ) {
-                    xmlBufferPtr in, out;
-                    xmlCharEncodingHandlerPtr coder;
-                    in  = xmlBufferCreate();
-                    out = xmlBufferCreate();
+            enc = xmlParseCharEncoding( encoding );
+            if ( enc > 1 ) {
+                coder= xmlGetCharEncodingHandler( enc );
+            }
+            else if ( enc == 1 ) {
+                ret = xmlStrdup( string );
+            }
+            else if ( enc == XML_CHAR_ENCODING_ERROR ) {
+                coder = xmlFindCharEncodingHandler( encoding );
+            }
+            else {
+                /*  fprintf(stderr, "NO XML ENCODING!\n"); */
+            }
+
+            if ( coder != NULL ) {
+                in  = xmlBufferCreate();
+                out = xmlBufferCreate();
                     
-                    coder = xmlGetCharEncodingHandler( enc );
-                    xmlBufferCat( in, string );        
-                    
-                    if ( xmlCharEncOutFunc( coder, out, in ) >= 0 ) {
-                        ret=xmlStrdup(out->content);
-                    }
-                    else {
-                        /* printf("decoding error \n"); */
-                    }
-                    
-                    xmlBufferFree( in );
-                    xmlBufferFree( out );
+                xmlBufferCat( in, string );        
+                if ( xmlCharEncOutFunc( coder, out, in ) >= 0 ) {
+                    ret=xmlStrdup(out->content);
                 }
                 else {
-                    ret = xmlStrdup(string);
+                    /* printf("decoding error \n"); */
                 }
-            }
-            else {
-                /* warn( "decoding error:no encoding\n" ); */
-                ret = xmlStrdup( string );
+            
+                xmlBufferFree( in );
+                xmlBufferFree( out );
             }
         }
         else {
-            /* if utf-8 is requested we do nothing */
-            ret = xmlStrdup( string );
+            ret = xmlStrdup(string);
         }
     }
     return ret;