原生调用接口:如何翻译"wchar_t"?

Native calling interface: how to translate "wchar_t"?

我想在 Perl6 中使用 ncurses int addwstr(const wchar_t *wstr); 函数。

我怎样才能得到一个 Perl 6 签名来传达 const wchar_t *wstr of addwstr

use v6;
use NativeCall;

constant LIB = 'libncursesw.so.5';

sub addwstr(  ?  ) returns int32 is native(LIB) is export {*};

wchar_t 在我的机器上是 32 位。从NativeCall doco开始,你可以声明一个数组,数组名作为指针;

#!/usr/bin/env perl6
use v6;
use NCurses;                   # To get iniscr(), endwin() etc
use NativeCall;

# Need to run setlocale from the C library
my int32 constant LC_ALL = 6;           # From locale.h
my sub setlocale(int32, Str) returns Str is native(Str) { * }

constant LIB = 'libncursesw.so.5';
sub addwstr(CArray[int32]) returns int32 is native(LIB) { * }

# The smiley        : Codepoint 0x263a
# Latin space       : Codepoint 0x20  (Ascii decimal ord 32)
# Check mark (tick) : Codepoint 0x2713

my CArray[int32] $wchar_str .= new(0x263a, 0x20, 0x2713);

setlocale(LC_ALL, "");
initscr();
move(2,2);
addwstr( $wchar_str );
nc_refresh;
while getch() < 0 {};
endwin;

这会在我的机器上打印“☺✓”。不调用 setlocale 是行不通的。

顺便说一句,您 没有 来使用 'w' 函数 - 您可以只传递普通的 perl6 字符串(大概是编码的 UTF-8)并且它只是工作。这会产生相同的结果;

#!/usr/bin/env perl6
use v6;
use NCurses;
use NativeCall;

# Need to run setlocale from the standard C library
my int32 constant LC_ALL = 6;           # From locale.h
my sub setlocale(int32, Str) returns Str is native(Str) { * }

my $ordinary_scalar = "☺ ✓";

setlocale(LC_ALL, "");
initscr();
move(2,2);
addstr( $ordinary_scalar );   # No 'w' necessary
nc_refresh;
while getch() < 0 {};
endwin;