本机调用接口:如何翻译“wchar_t”?

时间:2016-03-04 13:22:19

标签: ncurses ffi perl6 nativecall

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

我怎样才能获得传递const wchar_t *wstr addwstr的{​​1}}的Perl 6签名?

use v6;
use NativeCall;

constant LIB = 'libncursesw.so.5';

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

1 个答案:

答案 0 :(得分: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;

这打印&#34;☺✓&#34;在我的机器上。如果没有调用setlocale,它就无法工作。

顺便说一句,你不 使用&#39; w&#39;函数 - 你可以只传递普通的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;