ncurses - native 调用接口(interface) : how to translate "wchar_t"?

标签 ncurses ffi raku nativecall

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

我怎样才能得到传达 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 {*};

最佳答案

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/

相关文章:

raku - 如何使用 perl6 正则表达式元语法 <foo regex>?

string - 在 Raku 中不创建字符串上下文的情况下进行插值?

raku - 如何在 Perl 6 中将 sub 放入正则表达式中?

c - 在 ncurses 中定义新颜色

python - OSX iTerm2 可以将鼠标与 python-ncurses 一起使用,但不能与 C 中的curses一起使用

ruby - 通过 RVM 在 Ubuntu、Ruby 1.9.2 上安装使用 native 扩展的 gem 时出错

c++ - 使用 C 中内存地址的 Julia 读/写结构数据

c - 从 OCaml 获取 C 二进制数据

c - 在 ncurses 中打印盲文字符

ncurses 捕获扩展键(Control-left、Shift-Function 等)