@@ -3,25 +3,25 @@ extern crate libc;
33use array:: Array ;
44use defines:: AfError ;
55use defines:: ColorMap ;
6- use self :: libc:: { c_int, c_uint, c_double} ;
6+ use self :: libc:: { c_int, c_uint, c_double, c_char} ;
7+ use std:: ffi:: CString ;
78
89type MutWndHandle = * mut self :: libc:: c_ulonglong ;
910type WndHandle = self :: libc:: c_ulonglong ;
10- type AfArray = self :: libc:: c_longlong ;
11+ type AfArray = self :: libc:: c_longlong ;
1112type CellPtr = * const self :: libc:: c_void ;
1213
1314#[ allow( dead_code) ]
1415extern {
15- fn af_create_window ( out : MutWndHandle , w : c_int , h : c_int , title : * const u8 ) -> c_int ;
16+ fn af_create_window ( out : MutWndHandle , w : c_int , h : c_int , title : * const c_char ) -> c_int ;
1617 fn af_set_position ( wnd : WndHandle , x : c_uint , y : c_uint ) -> c_int ;
17- fn af_set_title ( wnd : WndHandle , title : * const u8 ) -> c_int ;
18+ fn af_set_title ( wnd : WndHandle , title : * const c_char ) -> c_int ;
1819 fn af_draw_image ( wnd : WndHandle , arr : AfArray , props : CellPtr ) -> c_int ;
1920 fn af_draw_plot ( wnd : WndHandle , x : AfArray , y : AfArray , props : CellPtr ) -> c_int ;
2021 fn af_grid ( wnd : WndHandle , rows : c_int , cols : c_int ) -> c_int ;
2122 fn af_show ( wnd : WndHandle ) -> c_int ;
2223 fn af_is_window_closed ( out : * mut c_int , wnd : WndHandle ) -> c_int ;
2324 fn af_destroy_window ( wnd : WndHandle ) -> c_int ;
24-
2525 fn af_draw_hist ( wnd : WndHandle , x : AfArray ,
2626 minval : c_double , maxval : c_double , props : CellPtr ) -> c_int ;
2727}
@@ -65,12 +65,18 @@ impl Window {
6565 pub fn new ( width : i32 , height : i32 , title : String ) -> Result < Window , AfError > {
6666 unsafe {
6767 let mut temp: u64 = 0 ;
68- let err_val = af_create_window ( & mut temp as MutWndHandle ,
69- width as c_int , height as c_int ,
70- title. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ) ;
71- match err_val {
72- 0 => Ok ( Window :: from ( temp) ) ,
73- _ => Err ( AfError :: from ( err_val) ) ,
68+ let cstr_ret = CString :: new ( title. as_bytes ( ) ) ;
69+ match cstr_ret {
70+ Ok ( cstr) => {
71+ let err_val = af_create_window ( & mut temp as MutWndHandle
72+ , width as c_int , height as c_int
73+ , cstr. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ) ;
74+ match err_val {
75+ 0 => Ok ( Window :: from ( temp) ) ,
76+ _ => Err ( AfError :: from ( err_val) ) ,
77+ }
78+ } ,
79+ Err ( _) => Err ( AfError :: ERR_INTERNAL ) ,
7480 }
7581 }
7682 }
@@ -87,11 +93,17 @@ impl Window {
8793
8894 pub fn set_title ( & self , title : String ) -> Result < ( ) , AfError > {
8995 unsafe {
90- let err_val = af_set_title ( self . handle as WndHandle ,
91- title. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ) ;
92- match err_val {
93- 0 => Ok ( ( ) ) ,
94- _ => Err ( AfError :: from ( err_val) ) ,
96+ let cstr_ret = CString :: new ( title. as_bytes ( ) ) ;
97+ match cstr_ret {
98+ Ok ( cstr) => {
99+ let err_val = af_set_title ( self . handle as WndHandle
100+ , cstr. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ) ;
101+ match err_val {
102+ 0 => Ok ( ( ) ) ,
103+ _ => Err ( AfError :: from ( err_val) ) ,
104+ }
105+ } ,
106+ Err ( _) => Err ( AfError :: ERR_INTERNAL ) ,
95107 }
96108 }
97109 }
0 commit comments