# HG changeset patch # User John W. Eaton # Date 1294686312 18000 # Node ID 8a40037533e2199e378501878b4e50265452c673 # Parent 44032aac5223795aee4463e197b4d151eaa87d6d struct printing changes diff --git a/doc/ChangeLog b/doc/ChangeLog --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2011-01-10 John W. Eaton + + * interpreter/container.txi (Basic Usage and Examples): + Document print_struct_array_contents. + 2011-01-05 Rik * interpreter/octave.texi: Put statistics detailmenu in correct order. diff --git a/doc/interpreter/container.txi b/doc/interpreter/container.txi --- a/doc/interpreter/container.txi +++ b/doc/interpreter/container.txi @@ -159,11 +159,15 @@ @noindent This prevents long and confusing output from large deeply nested -structures. The number of levels to print for nested structures can be -set with the function @code{struct_levels_to_print}: +structures. The number of levels to print for nested structures may be +set with the function @code{struct_levels_to_print}, and the function +@code{print_struct_array_contents} may be used to enable printing of the +contents of structure arrays. @DOCSTRING(struct_levels_to_print) +@DOCSTRING(print_struct_array_contents) + Functions can return structures. For example, the following function separates the real and complex parts of a matrix and stores them in two elements of the same structure variable. diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,18 @@ +2011-01-10 John W. Eaton + + * ov-cell.cc (octave_cell::print_as_scalar): Always return true. + (octave_cell::print_raw): Handle extra newlines here. + * ov-struct.cc (Fstruct_levels_to_print): Move here from pr-output.cc + (Vstruct_levels_to_print): Move here from pr-output.cc. Now static. + (Vprint_struct_array_contents): New static variable. + (Fprint_struct_array_contents): New function. + (octave_struct::print_raw): Use Vprint_struct_array_contents. + Simplify and improve output. + (octave_scalar_struct::print_raw): Simplify and improve output. + * pr-output.h (Vstruct_levels_to_print): Delete decl. + * ov-class.cc (octave_class::print_raw): Don't unwind_protect + Vstruct_levels_to_print. + 2011-01-09 David Bateman * ls-mat5.cc (save_mat5_array_length (const float*, octave_idx_type, diff --git a/src/ov-cell.cc b/src/ov-cell.cc --- a/src/ov-cell.cc +++ b/src/ov-cell.cc @@ -678,7 +678,7 @@ bool octave_cell::print_as_scalar (void) const { - return (ndims () > 2 || numel () == 0); + return true; } void @@ -699,6 +699,7 @@ if (nr > 0 && nc > 0) { + newline (os); indent (os); os << "{"; newline (os); diff --git a/src/ov-class.cc b/src/ov-class.cc --- a/src/ov-class.cc +++ b/src/ov-class.cc @@ -956,8 +956,6 @@ { unwind_protect frame; - frame.protect_var (Vstruct_levels_to_print); - indent (os); os << " "; newline (os); diff --git a/src/ov-struct.cc b/src/ov-struct.cc --- a/src/ov-struct.cc +++ b/src/ov-struct.cc @@ -51,6 +51,13 @@ DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); +// How many levels of structure elements should we print? +static int Vstruct_levels_to_print = 2; + +// TRUE means print struct array contents, up to the number of levels +// specified by struct_levels_to_print. +static bool Vprint_struct_array_contents = false; + octave_base_value * octave_struct::try_narrowing_conversion (void) { @@ -601,26 +608,20 @@ if (Vstruct_levels_to_print >= 0) { - bool print_keys_only = Vstruct_levels_to_print-- == 0; + bool max_depth_reached = Vstruct_levels_to_print-- == 0; - indent (os); - os << "{"; - newline (os); + bool print_fieldnames_only + = (max_depth_reached || ! Vprint_struct_array_contents); increment_indent_level (); - octave_idx_type n = map.numel (); + newline (os); + indent (os); + dim_vector dv = dims (); + os << dv.str () << " struct array containing the fields:"; + newline (os); - if (n != 1 || print_keys_only) - { - indent (os); - dim_vector dv = dims (); - os << dv.str () << " struct array containing the fields:"; - newline (os); - newline (os); - - increment_indent_level (); - } + increment_indent_level (); string_vector key_list = map.fieldnames (); @@ -630,31 +631,25 @@ Cell val = map.contents (key); - octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); + newline (os); - if (n != 1 || print_keys_only) + if (print_fieldnames_only) { indent (os); os << key; - if (n == 1) - { - dim_vector dv = tmp.dims (); - os << ": " << dv.str () << " " << tmp.type_name (); - } - newline (os); } else - tmp.print_with_name (os, key); + { + octave_value tmp (val); + tmp.print_with_name (os, key); + } } - if (n != 1 || print_keys_only) - decrement_indent_level (); + if (print_fieldnames_only) + newline (os); decrement_indent_level (); - - indent (os); - os << "}"; - newline (os); + decrement_indent_level (); } else { @@ -1323,26 +1318,19 @@ if (Vstruct_levels_to_print >= 0) { - bool print_keys_only = Vstruct_levels_to_print-- == 0; + bool max_depth_reached = Vstruct_levels_to_print-- == 0; - indent (os); - os << "{"; - newline (os); + bool print_fieldnames_only = max_depth_reached; increment_indent_level (); - octave_idx_type n = 1; + newline (os); + indent (os); + os << "scalar structure containing the fields:"; + newline (os); + newline (os); - if (n != 1 || print_keys_only) - { - indent (os); - dim_vector dv = dims (); - os << dv.str () << " struct array containing the fields:"; - newline (os); - newline (os); - - increment_indent_level (); - } + increment_indent_level (); string_vector key_list = map.fieldnames (); @@ -1352,31 +1340,22 @@ Cell val = map.contents (key); - octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); + octave_value tmp = val(0); - if (n != 1 || print_keys_only) + if (print_fieldnames_only) { indent (os); os << key; - if (n == 1) - { - dim_vector dv = tmp.dims (); - os << ": " << dv.str () << " " << tmp.type_name (); - } + dim_vector dv = tmp.dims (); + os << ": " << dv.str () << " " << tmp.type_name (); newline (os); } else tmp.print_with_name (os, key); } - if (n != 1 || print_keys_only) - decrement_indent_level (); - decrement_indent_level (); - - indent (os); - os << "}"; - newline (os); + decrement_indent_level (); } else { @@ -2194,3 +2173,28 @@ %! assert (size (y), [1, 6]); */ +DEFUN (struct_levels_to_print, args, nargout, + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {@var{val} =} struct_levels_to_print ()\n\ +@deftypefnx {Built-in Function} {@var{old_val} =} struct_levels_to_print (@var{new_val})\n\ +Query or set the internal variable that specifies the number of\n\ +structure levels to display.\n\ +@end deftypefn") +{ + return SET_INTERNAL_VARIABLE_WITH_LIMITS (struct_levels_to_print, + -1, INT_MAX); +} + +DEFUN (print_struct_array_contents, args, nargout, + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {@var{val} =} print_struct_array_contents ()\n\ +@deftypefnx {Built-in Function} {@var{old_val} =} print_struct_array_contents (@var{new_val})\n\ +Query or set the internal variable that specifies whether to print struct\n\ +array contents. If true, values of struct array elements are printed.\n\ +This variable does not affect scalar structures. Their elements\n\ +are always printed. In both cases, however, printing will be limited to\n\ +the number of levels specified by @var{struct_levels_to_print}.\n\ +@end deftypefn") +{ + return SET_INTERNAL_VARIABLE (print_struct_array_contents); +} diff --git a/src/pr-output.cc b/src/pr-output.cc --- a/src/pr-output.cc +++ b/src/pr-output.cc @@ -79,9 +79,6 @@ // smaller slices that fit on the screen. static bool Vsplit_long_rows = true; -// How many levels of structure elements should we print? -int Vstruct_levels_to_print = 2; - // TRUE means don't do any fancy formatting. static bool free_format = false; @@ -4028,15 +4025,3 @@ { return SET_INTERNAL_VARIABLE_WITH_LIMITS (output_precision, -1, INT_MAX); } - -DEFUN (struct_levels_to_print, args, nargout, - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {@var{val} =} struct_levels_to_print ()\n\ -@deftypefnx {Built-in Function} {@var{old_val} =} struct_levels_to_print (@var{new_val})\n\ -Query or set the internal variable that specifies the number of\n\ -structure levels to display.\n\ -@end deftypefn") -{ - return SET_INTERNAL_VARIABLE_WITH_LIMITS (struct_levels_to_print, - -1, INT_MAX); -} diff --git a/src/pr-output.h b/src/pr-output.h --- a/src/pr-output.h +++ b/src/pr-output.h @@ -257,7 +257,4 @@ // like this: x = [](2x0). extern bool Vprint_empty_dimensions; -// How many levels of structure elements should we print? -extern OCTINTERP_API int Vstruct_levels_to_print; - #endif