2016-03-04 23 views
5

Vorrei utilizzare la funzione ncurses int addwstr(const wchar_t *wstr); in Perl6.Interfaccia di chiamata nativa: come tradurre "wchar_t"?

Come posso ottenere una firma Perl 6 che trasmetta const wchar_t *wstr di addwstr?

use v6; 
use NativeCall; 

constant LIB = 'libncursesw.so.5'; 

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

Se non sbaglio sarebbe 'Str è codificato ('utf32')' su linux. – ElderBug

+0

ElderBug: questo mi dà: "Codifica della stringa sconosciuta per la chiamata nativa: utf32 in ..." (su linux). –

+0

Non sono davvero un ragazzo perl quindi non ne sono sicuro. 'wchar_t' su linux è UTF-32. Forse funzionerà con "UTF-32"? – ElderBug

risposta

2

wchar_t è 32 bit sulla mia macchina. Da NativeCall doco, è possibile dichiarare un array e il nome dell'array agirà da puntatore;

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

Questo stampa "☺ ✓" sulla mia macchina. Non funziona senza la chiamata a setlocale.

Come parte, non è utilizzare le funzioni "w" - è possibile passare semplicemente normali stringhe perl6 (presumibilmente codificate UTF-8) e funziona. Questo produce lo stesso risultato;

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