@@ -11,28 +11,42 @@ use util::*;
1111
1212use std:: slice;
1313
14- unsafe fn read (
14+ unsafe fn dispatch (
1515 nlhs : c_int ,
1616 plhs : * mut MxArrayMut ,
1717 nrhs : c_int ,
1818 prhs : * const MxArray ,
1919) -> Result < ( ) > {
20- let rhs = match nrhs == 2 {
21- true => slice:: from_raw_parts ( prhs, nrhs as usize ) ,
22- false => return Err ( "Invalid number of input arguments" . to_string ( ) ) ,
20+ let rhs = if nrhs > 0 {
21+ slice:: from_raw_parts ( prhs, nrhs as usize )
22+ } else {
23+ return Err ( "Invalid number of input arguments" . to_string ( ) ) ;
2324 } ;
2425
25- let lhs = match nlhs == 1 {
26- true => slice:: from_raw_parts_mut ( plhs, nlhs as usize ) ,
27- false => return Err ( "Invalid number of output arguments" . to_string ( ) ) ,
26+ let lhs = if nlhs >= 0 {
27+ slice:: from_raw_parts_mut ( plhs, nlhs as usize )
28+ } else {
29+ return Err ( "Invalid number of output arguments" . to_string ( ) ) ;
2830 } ;
2931
30- let mat_arr = crate :: read:: read ( rhs) ?;
31-
32- // set output
33- lhs[ 0 ] = mat_arr;
32+ let command = mx_array_to_str ( rhs[ 0 ] ) ?;
33+ let rhs = & rhs[ 1 ..] ;
34+
35+ match command {
36+ "read" => {
37+ if lhs. len ( ) != 1 {
38+ return Err ( format ! (
39+ "Invalid number of output arguments. Expected 1, got {}." ,
40+ lhs. len( )
41+ ) ) ;
42+ }
43+ let mat_arr = crate :: read:: read ( rhs) ?;
44+ lhs[ 0 ] = mat_arr;
45+ }
46+ _ => return Err ( format ! ( "Unknown command {:?}" , command) ) ,
47+ }
3448
3549 Ok ( ( ) )
3650}
3751
38- mex_function ! ( nlhs, lhs, nrhs, rhs, { read ( nlhs, lhs, nrhs, rhs) } ) ;
52+ mex_function ! ( nlhs, lhs, nrhs, rhs, { dispatch ( nlhs, lhs, nrhs, rhs) } ) ;
0 commit comments