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

我如何获得传达const wchar_t *wstraddwstr的Perl 6签名?

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;

关于ncurses - native 调用接口(interface): how to translate “wchar_t” ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35797181/

10-15 04:50