Discussion:
wchar_t and XS
(too old to reply)
Alberto Simões
2012-02-27 16:29:57 UTC
Permalink
Hello

I really need to make my code work with XS and wchar_t.
As far as I can tell there is no typemap for that in the core:

xs/NATools.c:733: warning: passing argument 3 of ‘Perl_sv_setpv’ from
incompatible pointer type

I googled for that, and found a typemap, but I do not understand how
typemaps work. My typemap looks like:

------ 8< ----------


TYPEMAP
wchar_t

INPUT
T_WCHAR
{
// Alloc memory for wide char string. This could be a bit more
// then necessary.
Newz(0, $var, SvLEN($arg), wchar_t);

U8* src = (U8*) SvPV_nolen($arg);
wchar_t* dst = (wchar_t*) $var;

if (SvUTF8($arg)) {
// UTF8 to wide char mapping
STRLEN len;
while (*src) {
*dst++ = utf8_to_uvuni(src, &len);
src += len;
}
} else {
// char to wide char mapping
while (*src) {
*dst++ = (wchar_t) *src++;
}
}
*dst = 0;
SAVEFREEPV($var);
}
------ 8< -------------

But then, this seems to be a Perl => wchar only. I would need the other
way around too.

Any help?

Thanks
Ambs
Marvin Humphrey
2012-02-27 17:21:04 UTC
Permalink
Post by Alberto Simões
I googled for that, and found a typemap, but I do not understand how
typemaps work.
Take a look at the Foo.c file that is generated off of your Foo.xs file. XS
"typemaps" are not very sophisticated -- they're just strings that get
inserted into a template for an XS function and then expanded. It so happens
that some variables are available ($var, $arg), but you seem to be using those
right already.
Post by Alberto Simões
------ 8< ----------
TYPEMAP
wchar_t
INPUT
T_WCHAR
{
// Alloc memory for wide char string. This could be a bit more
// then necessary.
Newz(0, $var, SvLEN($arg), wchar_t);
U8* src = (U8*) SvPV_nolen($arg);
wchar_t* dst = (wchar_t*) $var;
if (SvUTF8($arg)) {
// UTF8 to wide char mapping
STRLEN len;
while (*src) {
*dst++ = utf8_to_uvuni(src, &len);
src += len;
}
} else {
// char to wide char mapping
while (*src) {
*dst++ = (wchar_t) *src++;
}
}
*dst = 0;
SAVEFREEPV($var);
}
------ 8< -------------
Looks good to me, aside from concerns about 16-bit wchar_t systems and code
points above the BMP.

(Although I don't know what SAVEFREEPV does, I assume it sets up the memory
you allocated to be freed once the function you are wrapping returns.)

Cheers,

Marvin Humphrey
Alberto Simões
2012-02-27 17:25:28 UTC
Permalink
Post by Marvin Humphrey
Post by Alberto Simões
I googled for that, and found a typemap, but I do not understand how
typemaps work.
Take a look at the Foo.c file that is generated off of your Foo.xs file. XS
"typemaps" are not very sophisticated -- they're just strings that get
inserted into a template for an XS function and then expanded. It so happens
that some variables are available ($var, $arg), but you seem to be using those
right already.
In fact, looking to the typemap that is shipped with Perl, it seems that
wchar_t should be supported...
Alberto Simões
2012-02-27 20:02:49 UTC
Permalink
Stolen from lucene interface on CPAN.

\o/
Post by Alberto Simões
Hello
I really need to make my code work with XS and wchar_t.
xs/NATools.c:733: warning: passing argument 3 of ‘Perl_sv_setpv’ from
incompatible pointer type
I googled for that, and found a typemap, but I do not understand how
------ 8< ----------
TYPEMAP
wchar_t
INPUT
T_WCHAR
{
// Alloc memory for wide char string. This could be a bit more
// then necessary.
Newz(0, $var, SvLEN($arg), wchar_t);
U8* src = (U8*) SvPV_nolen($arg);
wchar_t* dst = (wchar_t*) $var;
if (SvUTF8($arg)) {
// UTF8 to wide char mapping
STRLEN len;
while (*src) {
*dst++ = utf8_to_uvuni(src, &len);
src += len;
}
} else {
// char to wide char mapping
while (*src) {
*dst++ = (wchar_t) *src++;
}
}
*dst = 0;
SAVEFREEPV($var);
}
------ 8< -------------
But then, this seems to be a Perl => wchar only. I would need the other
way around too.
Any help?
Thanks
Ambs
Loading...