Native call interface: how to translate "wchar_t"?

I would like to use the function ncurses int addwstr(const wchar_t *wstr); in Perl6.

How can I get Perl 6 signatures that pass const wchar_t *wstr from addwstr ?

 use v6; use NativeCall; constant LIB = 'libncursesw.so.5'; sub addwstr( ? ) returns int32 is native(LIB) is export {*}; 
+5
source share
1 answer

wchar_t - 32 bits on my machine. From the NativeCall doco, you can declare an array of them, and the name of the array will act as a pointer;

 #!/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; 

This prints "☺ βœ“" on my machine. This does not work without calling setlocale.

As an aside, you don’t need to use the β€œw” functions, you can just wrap normal perl6 lines (supposedly encoded by UTF-8), and that just works. This gives the same result:

 #!/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; 
+2
source

Source: https://habr.com/ru/post/1244433/


All Articles