[mw-devel] MARVIN r82 - branches/smonkey
Andrew Price
welshbyte at sucs.org
Wed Jul 26 03:33:41 BST 2006
On 26/07/06 03:22, welshbyte at sucs.org wrote:
> Started doing the JSString creation needed by js_exec() but having trouble
> with character sets and iconv etc. Copied and modified some of pwb's iconv
> code from mw to write local2jschars() but it's not working at all at the
> moment.
Pete (or anyone else who groks this stuff) - could you take a look at my
local2jschars() function that I started writing. It doesn't yet actually
find the local charset to use in the conversion but I still can't get
the conversion working properly manually anyway. Apologies for probably
churning out some disgusting code in this branch, all the concepts are
very new to me.
<snip>
> +/* Convert a string from local charset to a string of jschar which
> + JS_NewUCString() needs to create a new JSString with unicode
> + characters in. An appropriate jschar* is created by casting
> + UTF-16 data to jschar*, which is why we encode to UTF-16 here. */
> +jschar *
> +local2jschars(wchar_t * local) {
> + jschar * utf16;
> + char * charset;
> + iconv_t conv;
> + int nconv;
> + size_t localbytesleft;
> + size_t utf16bytesleft;
> + char * localcpy;
> + char * utf16cpy;
> +
> + charset = "WCHAR_T";
> + conv = iconv_open("UTF-16", charset);
> + if (conv == (iconv_t)-1) {
> + fprintf(stderr, "local2utf16 bombed.\n");
> + return NULL;
> + }
> +
> + localbytesleft = (wcslen(local)) * sizeof(wchar_t);
> + utf16 = malloc(wcslen(local) * sizeof(jschar));
> + if (utf16 == NULL) {
> + fprintf(stderr, "Could not allocate memory for iconv\n");
> + return NULL;
> + }
> +
> + localcpy = (char *)local;
> + utf16cpy = (char *)utf16;
> +
> + while (localbytesleft > 0) {
> + nconv = iconv(conv,
> + &localcpy, &localbytesleft,
> + &utf16cpy, &utf16bytesleft);
> + if (nconv == (size_t)-1) {
> + fprintf(stderr, "local2jschars barfed (%d)\n", errno);
> + /* iconv barfed, but why? */
> + if (errno == EILSEQ || errno == EINVAL) {
> + /* invalid input sequence, skip it */
> + fprintf(stderr, "Invalid input sequence\n");
> + local++;
> + localbytesleft--;
> + errno = 0;
> + continue;
> + } else {
> + /* some other error, recover what we can */
> + fprintf(stderr, "Some other error\n");
> + *(char *)utf16 = '\0';
> + perror("iconv");
> + errno = 0;
> + break;
> + }
> + }
> + }
> + iconv_close(conv);
> + return utf16;
> +}
More information about the mw-devel
mailing list