From: <ai...@us...> - 2011-01-11 07:57:10
|
Revision: 11476 http://plplot.svn.sourceforge.net/plplot/?rev=11476&view=rev Author: airwin Date: 2011-01-11 07:57:04 +0000 (Tue, 11 Jan 2011) Log Message: ----------- Add an alternative to handle an Octave cell array of strings for the char ** ArrayCk typemap. Demonstrate this alternative for some of the pllegend calls in example 33. The Octave result still remained consistent with the C result for this example. Modified Paths: -------------- trunk/bindings/octave/plplot_octave.i trunk/examples/octave/x33c.m Modified: trunk/bindings/octave/plplot_octave.i =================================================================== --- trunk/bindings/octave/plplot_octave.i 2011-01-11 07:51:42 UTC (rev 11475) +++ trunk/bindings/octave/plplot_octave.i 2011-01-11 07:57:04 UTC (rev 11476) @@ -1145,38 +1145,84 @@ %} // No count but check consistency with previous -%typemap(in) char **ArrayCk (charMatrix temp) { - int i, max_length, non_blank_length; +%typemap(in) char **ArrayCk { + charMatrix temp_matrix; + Cell temp_cell; + char *tmp_cstring; + std::string str; + size_t max_length, non_blank_length; + int i, ifcell; if ( _n_dims($input) > 2 ) - { error("argument must be a scalar or vector or matrix"); SWIG_fail; } + { + error("argument must be a scalar or vector or matrix"); SWIG_fail; + } if ( _dim($input, 0) != Alen ) - { error("first dimension must be same length as previous vector"); SWIG_fail; } - // Allow one extra space for null termination. - max_length= _dim($input, 1) + 1; + { + error("first dimension must be same length as previous vector"); SWIG_fail; + } $1 = new char*[Alen]; - temp = $input.char_matrix_value(); + ifcell = $input.is_cell(); + if (ifcell) + { + temp_cell = $input.cell_value(); + } + else + { + temp_matrix = $input.char_matrix_value(); + // Allow one extra space for null termination. + max_length= _dim($input, 1) + 1; + } + for(i=0; i<Alen; i++) { // Must copy string to "permanent" location because the string // location corresponding to tmp_cstring gets // overwritten for each iteration of loop. - const char *tmp_cstring = temp.row_as_string(i).c_str(); + if(ifcell) + { + if(temp_cell.elem(i).is_string()) + { + str = temp_cell.elem(i).string_value(); + // leave room for null termination. + max_length = str.size() + 1; + tmp_cstring = (char *)str.c_str(); + } + else + { + // Use null string if user attempts to pass a cell array + // with a non-string element (likely an empty element + // since that should be allowed by the PLplot interface + // if that element is going to be unused). + // leave room for null termination. + max_length = 1; + tmp_cstring = (char*)""; + } + } + else + { + tmp_cstring = (char *)temp_matrix.row_as_string(i).c_str(); + } $1[i] = new char[max_length]; strncpy( $1[i], tmp_cstring, max_length-1 ); $1[i][max_length-1] = '\0'; - // remove trailing-blank padding that is used by the - // charMatrix class to insure all strings in a given - // charMatrix instance have the same length. - // This transformation also removes legitimate trailing - // blanks but there is nothing we can do about that - // for the charMatrix class. + // All the trailing blank crapola should not be needed for + // string cell arrays. + if(!ifcell) + { + // remove trailing-blank padding that is used by the + // charMatrix class to insure all strings in a given + // charMatrix instance have the same length. + // This transformation also removes legitimate trailing + // blanks but there is nothing we can do about that + // for the charMatrix class. - // Look for trailing nulls first (just in case, although that - // shouldn't happen if charMatrix implemented as documented) - // before looking for trailing blanks. - non_blank_length = max_length-2; - while(non_blank_length >= 0 && $1[i][non_blank_length] == '\0') {non_blank_length--;} - while(non_blank_length >= 0 && $1[i][non_blank_length] == ' ') {non_blank_length--;} - $1[i][non_blank_length+1] = '\0'; + // Look for trailing nulls first (just in case, although that + // shouldn't happen if charMatrix implemented as documented) + // before looking for trailing blanks. + non_blank_length = max_length-2; + while(non_blank_length >= 0 && $1[i][non_blank_length] == '\0') {non_blank_length--;} + while(non_blank_length >= 0 && $1[i][non_blank_length] == ' ') {non_blank_length--;} + $1[i][non_blank_length+1] = '\0'; + } } } Modified: trunk/examples/octave/x33c.m =================================================================== --- trunk/examples/octave/x33c.m 2011-01-11 07:51:42 UTC (rev 11475) +++ trunk/examples/octave/x33c.m 2011-01-11 07:57:04 UTC (rev 11476) @@ -782,17 +782,19 @@ max_height = max(max_height, legend_height); ## Set up line legend entries with various colours. - ## Reset text and symbols, because otherwise legend may come out with - ## the wrong length text/symbol with prior results embedded. - text = " "; - text(nlegend,1:length(" ")) = " "; - symbols = " "; - symbols(nlegend,1:length(" ")) = " "; + ## From here on use the cell array of strings paradigm for + ## text (and symbols) to show that the PLplot interface works properly for + ## this case (and also to demonstrate how to use a cell array of strings + ## with PLplot). + ## Reset text and symbols cell arrays. + text = {}; + text{nlegend} = ""; + symbols = {}; + symbols{nlegend} = ""; for i=0:nlegend-1 opt_array(i+1) = PL_LEGEND_LINE; - string = sprintf("%s %d", "Line Color",i+1); - text(i+1,1:length(string)) = string; + text{i+1} = sprintf("%s %d", "Line Color",i+1); text_colors(i+1) = i+1; line_colors(i+1) = i+1; line_styles(i+1) = 1; @@ -808,24 +810,22 @@ [legend_width, legend_height] = \ pllegend( opt, x, y, 0.1, 15, 1, 1, 0, 0, opt_array, 1.0, text_scale, 2.0, - 0., text_colors, text, + 0., text_colors, text', box_colors, box_patterns, box_scales, box_line_widths, line_colors, line_styles, line_widths, - symbol_colors, symbol_scales, symbol_numbers, symbols ); + symbol_colors, symbol_scales, symbol_numbers, symbols' ); max_height = max(max_height, legend_height); ## Set up line legend entries with various styles - ## Reset text and symbols, because otherwise legend may come out with - ## the wrong length text/symbol with prior results embedded. - text = " "; - text(nlegend,1:length(" ")) = " "; - symbols = " "; - symbols(nlegend,1:length(" ")) = " "; + ## Reset text and symbols cell arrays. + text = {}; + text{nlegend} = ""; + symbols = {}; + symbols{nlegend} = ""; for i=0:nlegend-1 opt_array(i+1) = PL_LEGEND_LINE; - string = sprintf("%s %d", "Line Style",i+1); - text(i+1,1:length(string)) = string; + text{i+1} = sprintf("%s %d", "Line Style",i+1); text_colors(i+1) = 2; line_colors(i+1) = 2; line_styles(i+1) = i+1; @@ -837,24 +837,22 @@ [legend_width, legend_height] = \ pllegend( opt, x, y, 0.1, 15, 1, 1, 0, 0, opt_array, 1.0, text_scale, 2.0, - 0., text_colors, text, + 0., text_colors, text', box_colors, box_patterns, box_scales, box_line_widths, line_colors, line_styles, line_widths, - symbol_colors, symbol_scales, symbol_numbers, symbols ); + symbol_colors, symbol_scales, symbol_numbers, symbols' ); max_height = max(max_height, legend_height); ## Set up line legend entries with various widths. - ## Reset text and symbols, because otherwise legend may come out with - ## the wrong length text/symbol with prior results embedded. - text = " "; - text(nlegend,1:length(" ")) = " "; - symbols = " "; - symbols(nlegend,1:length(" ")) = " "; + ## Reset text and symbols cell arrays. + text = {}; + text{nlegend} = ""; + symbols = {}; + symbols{nlegend} = ""; for i=0:nlegend-1 opt_array(i+1) = PL_LEGEND_LINE; - string = sprintf("%s %d", "Line Width",i+1); - text(i+1,1:length(string)) = string; + text{i+1} = sprintf("%s %d", "Line Width",i+1); text_colors(i+1) = 2; line_colors(i+1) = 2; line_styles(i+1) = 1; @@ -867,10 +865,10 @@ [legend_width, legend_height] = \ pllegend( opt, x, y, 0.1, 15, 1, 1, 0, 0, opt_array, 1.0, text_scale, 2.0, - 0., text_colors, text, + 0., text_colors, text', box_colors, box_patterns, box_scales, box_line_widths, line_colors, line_styles, line_widths, - symbol_colors, symbol_scales, symbol_numbers, symbols ); + symbol_colors, symbol_scales, symbol_numbers, symbols' ); max_height = max(max_height, legend_height); ## Color bar examples This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |