@@ -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}
@@ -102,12 +102,18 @@ impl Window {
102102 pub fn new ( width : i32 , height : i32 , title : String ) -> Result < Window , AfError > {
103103 unsafe {
104104 let mut temp: u64 = 0 ;
105- let err_val = af_create_window ( & mut temp as MutWndHandle ,
106- width as c_int , height as c_int ,
107- title. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ) ;
108- match err_val {
109- 0 => Ok ( Window :: from ( temp) ) ,
110- _ => Err ( AfError :: from ( err_val) ) ,
105+ let cstr_ret = CString :: new ( title. as_bytes ( ) ) ;
106+ match cstr_ret {
107+ Ok ( cstr) => {
108+ let err_val = af_create_window ( & mut temp as MutWndHandle
109+ , width as c_int , height as c_int
110+ , cstr. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ) ;
111+ match err_val {
112+ 0 => Ok ( Window :: from ( temp) ) ,
113+ _ => Err ( AfError :: from ( err_val) ) ,
114+ }
115+ } ,
116+ Err ( _) => Err ( AfError :: ERR_INTERNAL ) ,
111117 }
112118 }
113119 }
@@ -126,11 +132,17 @@ impl Window {
126132 /// Set window title
127133 pub fn set_title ( & self , title : String ) -> Result < ( ) , AfError > {
128134 unsafe {
129- let err_val = af_set_title ( self . handle as WndHandle ,
130- title. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ) ;
131- match err_val {
132- 0 => Ok ( ( ) ) ,
133- _ => Err ( AfError :: from ( err_val) ) ,
135+ let cstr_ret = CString :: new ( title. as_bytes ( ) ) ;
136+ match cstr_ret {
137+ Ok ( cstr) => {
138+ let err_val = af_set_title ( self . handle as WndHandle
139+ , cstr. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ) ;
140+ match err_val {
141+ 0 => Ok ( ( ) ) ,
142+ _ => Err ( AfError :: from ( err_val) ) ,
143+ }
144+ } ,
145+ Err ( _) => Err ( AfError :: ERR_INTERNAL ) ,
134146 }
135147 }
136148 }
0 commit comments