7

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

我怎样才能获得传达const wchar_t *wstr的Perl 6 签名addwstr

use v6;
use NativeCall;

constant LIB = 'libncursesw.so.5';

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

1 回答 1

2

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;
于 2016-03-14T10:11:04.367 回答