sid*_*com 7 ncurses ffi perl6 nativecall
我想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 {*};
Run Code Online (Sandbox Code Playgroud)
wchar_t我的机器上是32位的。从NativeCall doco中,您可以声明它们的数组,数组名称将充当指针;
#!/usr/bin/env perl6\nuse v6;\nuse NCurses; # To get iniscr(), endwin() etc\nuse NativeCall;\n\n# Need to run setlocale from the C library\nmy int32 constant LC_ALL = 6; # From locale.h\nmy sub setlocale(int32, Str) returns Str is native(Str) { * }\n\nconstant LIB = \'libncursesw.so.5\';\nsub addwstr(CArray[int32]) returns int32 is native(LIB) { * }\n\n# The smiley : Codepoint 0x263a\n# Latin space : Codepoint 0x20 (Ascii decimal ord 32)\n# Check mark (tick) : Codepoint 0x2713\n\nmy CArray[int32] $wchar_str .= new(0x263a, 0x20, 0x2713);\n\nsetlocale(LC_ALL, "");\ninitscr();\nmove(2,2);\naddwstr( $wchar_str );\nnc_refresh;\nwhile getch() < 0 {};\nendwin;\nRun Code Online (Sandbox Code Playgroud)\n\n这会在我的机器上打印“\xe2\x98\xba \xe2\x9c\x93”。如果不调用 setlocale,它就无法工作。
\n\n顺便说一句,您不必使用“w”函数 - 您只需传递普通的 perl6 字符串(大概是编码的 UTF-8)即可,它就可以工作。这会产生相同的结果;
\n\n#!/usr/bin/env perl6\nuse v6;\nuse NCurses;\nuse NativeCall;\n\n# Need to run setlocale from the standard C library\nmy int32 constant LC_ALL = 6; # From locale.h\nmy sub setlocale(int32, Str) returns Str is native(Str) { * }\n\nmy $ordinary_scalar = "\xe2\x98\xba \xe2\x9c\x93";\n\nsetlocale(LC_ALL, "");\ninitscr();\nmove(2,2);\naddstr( $ordinary_scalar ); # No \'w\' necessary\nnc_refresh;\nwhile getch() < 0 {};\nendwin;\nRun Code Online (Sandbox Code Playgroud)\n