Resend - I did not seen this on perl-xs list

Sisyphus <[EMAIL PROTECTED]> writes:
>Hi,
>
>In an Inline C script I'm trying to pass a wide character string from perl.
>
>The perl side would look like (eg):
>
>$s =3D chr(12345) . chr(54321);

SvPV of s now has two chars utf8 encoded.

>pass_wide_string($s);
>
>and the Inline C function:
>
>void pass_wide_string(SV * w) {
>      wchar_t * widestring =3D SvPV_nolen(w);
>      .
>      .
>
>Is that a correct way for pass_wide_string() to grab the argument ?=20

No.

>I=20
>get a compiler warning that the line in question performs an=20
>"initialization from incompatible pointer type".

Because SvPV is a char * not a wchar_t *

You need to convert to wide chars somewhere.
Which may mean knowing what kind of wide char it is.

e.g.
    pass_wide_string(encode('UCS-2',$s));   # say
and then cast in XS side:
    wchar_t * widestring =3D (wchar_t *) SvPV_nolen(w);

Or assuing wchar_t hold Unicode probably more robust leave perl=20
side way it is and build a wchar_t string on XS side:

    U8 *src =3D (U8 *) SvPV_nolen(sv);
    wchar_t *dst;
    STRLEN len;
    wchar_t * widestring;
    Newz(32,widestring,wchar_t,SvCUR(sv)); // Bigger than needed
    dst =3D widestring;
    while (*src)
    {
     *dst++ =3D utf8_to_uv(src,&len);
     src +=3D len;
    }=20=20
    *dst++ =3D 0;
    ...
    Safefree(widestring);
=20=20=20=20



Reply via email to