--- dclib/core/cxml.cpp 2005-02-04 23:23:26.000000000 +0200 +++ dclib/core/cxml.cpp 2005-05-23 09:39:13.000000000 +0300 @@ -23,7 +23,67 @@ #include +#include + #include "cxml.h" +/** + * xmlIconvWrapper: + * @cd: iconv converter data structure + * @out: a pointer to an array of bytes to store the result + * @outlen: the length of @out + * @in: a pointer to an array of ISO Latin 1 chars + * @inlen: the length of @in + * + * Returns 0 if success, or + * -1 by lack of space, or + * -2 if the transcoding fails (for *in is not valid utf8 string or + * the result of transformation can't fit into the encoding we want), or + * -3 if there the last byte can't form a single output char. + * + * The value of @inlen after return is the number of octets consumed + * as the return value is positive, else unpredictable. + * The value of @outlen after return is the number of ocetes consumed. + */ +static int +xmlIconvWrapper(iconv_t cd, + unsigned char *out, int *outlen, + const unsigned char *in, int *inlen) { + + size_t icv_inlen = *inlen, icv_outlen = *outlen; + const char *icv_in = (const char *) in; + char *icv_out = (char *) out; + int ret; + + ret = iconv(cd, (char **) &icv_in, &icv_inlen, &icv_out, &icv_outlen); + if (in != NULL) { + *inlen -= icv_inlen; + *outlen -= icv_outlen; + } else { + *inlen = 0; + *outlen = 0; + } + if ((icv_inlen != 0) || (ret == -1)) { +#ifdef EILSEQ + if (errno == EILSEQ) { + return -2; + } else +#endif +#ifdef E2BIG + if (errno == E2BIG) { + return -1; + } else +#endif +#ifdef EINVAL + if (errno == EINVAL) { + return -3; + } else +#endif + { + return -3; + } + } + return 0; +} /** */ CXml::CXml() @@ -34,6 +94,9 @@ { xmlInitParser(); } + + enc=strrchr(getenv("LANG"),'.'); + enc++; } /** */ @@ -134,7 +197,8 @@ CString r = ""; unsigned char *b; int inlen,outlen,res; - + iconv_t cd; + if ( (s == 0) || (len <= 0) ) { return ""; @@ -148,8 +212,10 @@ if ( b == 0 ) return r; - res = UTF8Toisolat1( b, &outlen, (unsigned char*)s, &inlen ); - + cd = iconv_open(enc,"UTF-8"); + res = xmlIconvWrapper(cd, b, &outlen, (unsigned char*)s, &inlen); + iconv_close(cd); + if ( res == -2 ) printf("UTF8Toisolat1 transcoding fail: '%s'\n",s); else if ( res == -1 ) @@ -170,7 +236,8 @@ CString r = ""; unsigned char *b; int inlen,outlen,res; - + iconv_t cd; + if ( (s == 0) || (len <= 0) ) { return ""; @@ -183,13 +250,14 @@ if ( b == 0 ) return r; - - res = isolat1ToUTF8( b, &outlen, (unsigned char*)s, &inlen ); - + + cd = iconv_open("UTF-8",enc); + res = xmlIconvWrapper(cd, b, &outlen, (unsigned char*)s, &inlen); + iconv_close(cd); if ( res >= 0 ) r = (char*)b; else - printf("isolat1ToUTF8 fail: '%s'\n",s); + printf("isolat1ToUTF8 fail: %s with error %d \n",s,res); free(b); @@ -346,3 +414,4 @@ else return FALSE; } + --- dclib/core/cxml.h 2004-05-24 21:39:02.000000000 +0300 +++ dclib/core/cxml.h 2005-05-23 09:38:07.000000000 +0300 @@ -89,6 +89,8 @@ /** */ xmlDocPtr pDoc; + + char * enc; }; inline xmlDocPtr CXml::doc()