annotate libinterp/octave-value/ov-struct.cc @ 33634:4a70f390c85e default tip @

maint: Merge stable to default.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 28 May 2024 15:25:54 +0200
parents 2949b89f9711
children 9108c5b24626
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 32546
diff changeset
3 // Copyright (C) 1996-2024 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21691
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21211
diff changeset
27 # include "config.h"
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
28 #endif
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
29
25438
cb1606f78f6b prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents: 25343
diff changeset
30 #include <istream>
cb1606f78f6b prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents: 25343
diff changeset
31 #include <ostream>
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
32
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
33 #include "Cell.h"
23454
30b6eccd6708 use builtin-defun-decls.h to ensure declarations of interpreter functions
John W. Eaton <jwe@octave.org>
parents: 23450
diff changeset
34 #include "builtin-defun-decls.h"
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
35 #include "defun.h"
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
36 #include "error.h"
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
37 #include "errwarn.h"
15149
62a35ae7d6a2 use forward decls for mxArray in ov.h and ov-base.h
John W. Eaton <jwe@octave.org>
parents: 15057
diff changeset
38 #include "mxarray.h"
2979
a3556d2adec9 [project @ 1997-05-15 22:35:37 by jwe]
jwe
parents: 2962
diff changeset
39 #include "oct-lvalue.h"
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
40 #include "oct-hdf5.h"
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
41 #include "ov-struct.h"
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
42 #include "unwind-prot.h"
6811
3fe394f8502b [project @ 2007-08-10 19:07:34 by jwe]
jwe
parents: 6686
diff changeset
43 #include "utils.h"
2948
56be458e237f [project @ 1997-05-09 13:37:35 by jwe]
jwe
parents: 2916
diff changeset
44 #include "variables.h"
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
45
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
46 #include "Array-util.h"
8377
25bc2d31e1bf improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents: 8150
diff changeset
47 #include "oct-locbuf.h"
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
48
4687
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
49 #include "byte-swap.h"
20447
c6224b4e7774 maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents: 20232
diff changeset
50 #include "ls-oct-text.h"
4687
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
51 #include "ls-oct-binary.h"
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
52 #include "ls-hdf5.h"
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
53 #include "ls-utils.h"
5759
ce79d238f142 [project @ 2006-04-13 13:04:32 by jwe]
jwe
parents: 5756
diff changeset
54 #include "pr-output.h"
32145
00740c1f8e82 new function to get for loop value from octave_value object
Petter T. <petter.vilhelm@gmail.com>
parents: 31814
diff changeset
55 #include "ov-inline.h"
4687
e95c86d48732 [project @ 2004-01-06 21:53:34 by jwe]
jwe
parents: 4675
diff changeset
56
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
57
4612
d44675070f1a [project @ 2003-11-14 19:49:56 by jwe]
jwe
parents: 4604
diff changeset
58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct");
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
59
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
60 // How many levels of structure elements should we print?
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
61 static int Vstruct_levels_to_print = 2;
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
62
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
63 // TRUE means print struct array contents, up to the number of levels
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
64 // specified by struct_levels_to_print.
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
65 static bool Vprint_struct_array_contents = false;
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
66
29476
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
67 void
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
68 octave_struct::break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame)
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
69 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
70 for (octave_idx_type j = 0; j < m_map.nfields (); j++)
29476
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
71 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
72 Cell& c = m_map.contents (j);
29476
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
73
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
74 for (octave_idx_type i = 0; i < c.numel (); i++)
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
75 c(i).break_closure_cycles (frame);
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
76 }
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
77 }
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
78
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
79 octave_base_value *
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31718
diff changeset
80 octave_struct::try_narrowing_conversion ()
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
81 {
23457
21baad6b35c4 maint: Use C++11 nullptr rather than 0 or NULL when possible.
Rik <rik@octave.org>
parents: 23454
diff changeset
82 octave_base_value *retval = nullptr;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
83
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
84 if (numel () == 1)
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
85 retval = new octave_scalar_struct (m_map.checkelem (0));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
86
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
87 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
88 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
89
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
90 Cell
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
91 octave_struct::dotref (const octave_value_list& idx, bool auto_add)
2962
5e0fe4c5d52f [project @ 1997-05-12 03:13:57 by jwe]
jwe
parents: 2954
diff changeset
92 {
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
93 Cell retval;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
94
30723
08b08b7f05b2 Replace direct calls to C library assert() with Octave specialty functions in libinterp/ (bug #61753)
Arun Giridhar <arungiridhar@gmail.com> and Rik <rik@octave.org>
parents: 30588
diff changeset
95 panic_if (idx.length () != 1);
2962
5e0fe4c5d52f [project @ 1997-05-12 03:13:57 by jwe]
jwe
parents: 2954
diff changeset
96
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
97 std::string nm = idx(0).string_value ();
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
98
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
99 octave_map::const_iterator p = m_map.seek (nm);
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
100
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
101 if (p != m_map.end ())
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
102 retval = m_map.contents (p);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
103 else if (auto_add)
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23576
diff changeset
104 retval = (isempty ()) ? Cell (dim_vector (1, 1)) : Cell (dims ());
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
105 else
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
106 error_with_id ("Octave:invalid-indexing",
15731
18f168880226 error_ids: Adding ids and documentation
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 15614
diff changeset
107 "structure has no member '%s'", nm.c_str ());
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
108
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
109 return retval;
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
110 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
111
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
112 static void
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31718
diff changeset
113 err_invalid_index_for_assignment ()
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
114 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
115 error ("invalid index for structure array assignment");
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
116 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
117
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
118 static void
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
119 err_invalid_index_type (const std::string& nm, char t)
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
120 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
121 error ("%s cannot be indexed with %c", nm.c_str (), t);
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
122 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
123
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
124 static void
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
125 maybe_warn_invalid_field_name (const std::string& key, const char *who)
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
126 {
25646
4d565baa475e move libinterp/utils functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 25438
diff changeset
127 if (! octave::valid_identifier (key))
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
128 {
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
129 if (who)
19852
e9a0bd0b125c Rename 'matlab-incompatible' warning to 'language-extension'.
Carnë Draug <carandraug@octave.org>
parents: 19697
diff changeset
130 warning_with_id ("Octave:language-extension",
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
131 "%s: invalid structure field name '%s'",
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
132 who, key.c_str ());
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
133 else
19852
e9a0bd0b125c Rename 'matlab-incompatible' warning to 'language-extension'.
Carnë Draug <carandraug@octave.org>
parents: 19697
diff changeset
134 warning_with_id ("Octave:language-extension",
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
135 "invalid structure field name '%s'",
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
136 key.c_str ());
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
137 }
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
138 }
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
139
7651
443a8f5a50fd require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents: 7622
diff changeset
140 octave_value_list
4247
fc9a075d10fb [project @ 2002-12-30 23:05:27 by jwe]
jwe
parents: 4219
diff changeset
141 octave_struct::subsref (const std::string& type,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
142 const std::list<octave_value_list>& idx,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
143 int nargout)
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
144 {
7651
443a8f5a50fd require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents: 7622
diff changeset
145 octave_value_list retval;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
146
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
147 int skip = 1;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
148
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
149 switch (type[0])
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
150 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
151 case '(':
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
152 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
153 if (type.length () > 1 && type[1] == '.')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
154 {
25337
3ff9192b676e use auto keyword to declare iterator variables where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
155 auto p = idx.begin ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
156 octave_value_list key_idx = *++p;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
157
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
158 const Cell tmp = dotref (key_idx);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
159
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
160 const Cell t = tmp.index (idx.front ());
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
161
28546
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
162 // Avoid creating a comma-separated list if the result is a
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
163 // single element.
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
164
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
165 retval(0) = (t.numel () == 1) ? t(0) : octave_value (t, true);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
166
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
167 // We handled two index elements, so tell
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
168 // next_subsref to skip both of them.
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
169
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
170 skip++;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
171 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
172 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
173 retval(0) = do_index_op (idx.front ());
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
174 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
175 break;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
176
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
177 case '.':
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
178 {
27139
1bad33112465 Emit an error for invalid fieldnames of 0x0 struct array (bug #34967).
Rik <rik@octave.org>
parents: 27061
diff changeset
179 const Cell t = dotref (idx.front ());
1bad33112465 Emit an error for invalid fieldnames of 0x0 struct array (bug #34967).
Rik <rik@octave.org>
parents: 27061
diff changeset
180
28546
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
181 // Avoid creating a comma-separated list if the result is a
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
182 // single element.
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
183
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
184 retval(0) = (t.numel () == 1) ? t(0) : octave_value (t, true);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
185 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
186 break;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
187
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
188 case '{':
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
189 err_invalid_index_type (type_name (), type[0]);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
190 break;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
191
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
192 default:
33362
2949b89f9711 replace most uses of panic_impossible with error in octave_value classes
John W. Eaton <jwe@octave.org>
parents: 32633
diff changeset
193 error ("unexpected: index not '(', '{', or '.' in octave_struct::subsref - please report this bug");
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
194 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
195
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
196 // FIXME: perhaps there should be an
4994
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
197 // octave_value_list::next_subsref member function? See also
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
198 // octave_user_function::subsref.
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
199
48d0defe9445 [project @ 2004-09-15 20:31:31 by jwe]
jwe
parents: 4944
diff changeset
200 if (idx.size () > 1)
7651
443a8f5a50fd require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents: 7622
diff changeset
201 retval = retval(0).next_subsref (nargout, type, idx, skip);
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
202
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
203 return retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
204 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
205
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
206 octave_value
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
207 octave_struct::subsref (const std::string& type,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
208 const std::list<octave_value_list>& idx,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
209 bool auto_add)
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
210 {
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
211 octave_value retval;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
212
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
213 int skip = 1;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
214
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
215 switch (type[0])
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
216 {
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
217 case '(':
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
218 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
219 if (type.length () > 1 && type[1] == '.')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
220 {
25337
3ff9192b676e use auto keyword to declare iterator variables where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
221 auto p = idx.begin ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
222 octave_value_list key_idx = *++p;
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
223
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
224 const Cell tmp = dotref (key_idx, auto_add);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
225
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
226 const Cell t = tmp.index (idx.front (), auto_add);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
227
28546
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
228 // Avoid creating a comma-separated list if the result is a
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
229 // single element.
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
230
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
231 retval = (t.numel () == 1) ? t(0) : octave_value (t, true);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
232
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
233 // We handled two index elements, so tell
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
234 // next_subsref to skip both of them.
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
235
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
236 skip++;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
237 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
238 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
239 retval = do_index_op (idx.front (), auto_add);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
240 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
241 break;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
242
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
243 case '.':
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
244 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
245 if (m_map.numel () > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
246 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
247 const Cell t = dotref (idx.front (), auto_add);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
248
28546
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
249 // Avoid creating a comma-separated list if the result is a
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
250 // single element.
ac7ab2a9018e create empty c-s list for x.a (bug #58695)
John W. Eaton <jwe@octave.org>
parents: 28179
diff changeset
251
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20218
diff changeset
252 retval = (t.numel () == 1) ? t(0) : octave_value (t, true);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
253 }
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
254 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
255 break;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
256
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
257 case '{':
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
258 err_invalid_index_type (type_name (), type[0]);
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
259 break;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
260
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
261 default:
33362
2949b89f9711 replace most uses of panic_impossible with error in octave_value classes
John W. Eaton <jwe@octave.org>
parents: 32633
diff changeset
262 error ("unexpected: index not '(', '{', or '.' in octave_struct::subsref - please report this bug");
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
263 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
264
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
265 // FIXME: perhaps there should be an
8551
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
266 // octave_value_list::next_subsref member function? See also
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
267 // octave_user_function::subsref.
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
268
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
269 if (idx.size () > 1)
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
270 retval = retval.next_subsref (auto_add, type, idx, skip);
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
271
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
272 return retval;
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
273 }
906f976d35a8 further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents: 8546
diff changeset
274
8031
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
275 /*
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
276 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
277 %! x(1).a.a = 1;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
278 %! x(2).a.a = 2;
8031
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
279 %! assert (size (x), [1, 2]);
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
280 %! assert (x(1).a.a, 1);
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
281 %! assert (x(2).a.a, 2);
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
282 */
d9987dbdf91b octave_struct::subsref: don't resize for simple x(idx) case
John W. Eaton <jwe@octave.org>
parents: 7651
diff changeset
283
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
284 octave_value
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
285 octave_struct::numeric_conv (const octave_value& val,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
286 const std::string& type)
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
287 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
288 octave_value retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
289
23584
7ed6b258db91 maint: Deprecate is_map and replace with isstruct.
Rik <rik@octave.org>
parents: 23577
diff changeset
290 if (type.length () > 0 && type[0] == '.' && ! val.isstruct ())
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
291 retval = octave_map ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
292 else
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
293 retval = val;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
294
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
295 return retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
296 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
297
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
298 octave_value
4247
fc9a075d10fb [project @ 2002-12-30 23:05:27 by jwe]
jwe
parents: 4219
diff changeset
299 octave_struct::subsasgn (const std::string& type,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
300 const std::list<octave_value_list>& idx,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
301 const octave_value& rhs)
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
302 {
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
303 octave_value retval;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
304
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
305 int n = type.length ();
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
306
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
307 octave_value t_rhs = rhs;
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
308
9286
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
309 if (idx.front ().empty ())
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
310 error ("subsasgn: missing index in indexed assignment");
9286
c2248cc4821a don't crash on assignments like a() = 1
Jaroslav Hajek <highegg@gmail.com>
parents: 9190
diff changeset
311
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
312 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.'))
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
313 {
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
314 switch (type[0])
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
315 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
316 case '(':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
317 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
318 if (type.length () > 1 && type[1] == '.')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
319 {
25337
3ff9192b676e use auto keyword to declare iterator variables where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
320 auto p = idx.begin ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
321 octave_value_list t_idx = *p;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
322
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
323 octave_value_list key_idx = *++p;
4513
508238e65af7 [project @ 2003-09-19 21:40:57 by jwe]
jwe
parents: 4499
diff changeset
324
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
325 if (key_idx.length () != 1)
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
326 error ("subsasgn: dynamic structure field names must be strings");
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
327
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
328 std::string key
32220
e039114b8a1a subsasgn: Add function name to error message (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 32215
diff changeset
329 = key_idx(0).xstring_value ("subsasgn: dynamic structure field names must be strings");
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
330
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
331 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
332
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
333 std::list<octave_value_list> next_idx (idx);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
334
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
335 // We handled two index elements, so subsasgn to
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
336 // needs to skip both of them.
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
337
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
338 next_idx.erase (next_idx.begin ());
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
339 next_idx.erase (next_idx.begin ());
8456
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
340
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
341 std::string next_type = type.substr (2);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
342
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
343 Cell tmpc (1, 1);
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
344 auto pkey = m_map.seek (key);
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
345 if (pkey != m_map.end ())
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
346 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
347 m_map.contents (pkey).make_unique ();
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
348 tmpc = m_map.contents (pkey).index (idx.front (), true);
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
349 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
350
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
351 // FIXME: better code reuse?
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
352 // cf. octave_cell::subsasgn and the case below.
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
353 if (tmpc.numel () != 1)
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
354 err_indexed_cs_list ();
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
355
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
356 octave_value& tmp = tmpc(0);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
357
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
358 bool orig_undefined = tmp.is_undefined ();
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
359
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
360 if (orig_undefined || tmp.is_zero_by_zero ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
361 {
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
362 tmp = octave_value::empty_conv (next_type, rhs);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
363 tmp.make_unique (); // probably a no-op.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
364 }
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
365 else
21118
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
366 // optimization: ignore the copy
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
367 // still stored inside our map.
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
368 tmp.make_unique (1);
3ac9f47fb04b Invert pattern if/code/else/err_XXX to if !/err_XXX/code.
Rik <rik@octave.org>
parents: 21102
diff changeset
369
21943
2fc1ce5deae4 fix misleading indentation
John W. Eaton <jwe@octave.org>
parents: 21743
diff changeset
370 t_rhs =(orig_undefined
2fc1ce5deae4 fix misleading indentation
John W. Eaton <jwe@octave.org>
parents: 21743
diff changeset
371 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
2fc1ce5deae4 fix misleading indentation
John W. Eaton <jwe@octave.org>
parents: 21743
diff changeset
372 : tmp.subsasgn (next_type, next_idx, rhs));
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
373 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
374 else
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
375 err_invalid_index_for_assignment ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
376 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
377 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
378
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
379 case '.':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
380 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
381 octave_value_list key_idx = idx.front ();
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
382
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
383 if (key_idx.length () != 1)
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
384 error ("subsasgn: dynamic structure field names must be strings");
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
385
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
386 std::string key
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
387 = key_idx(0).xstring_value ("subsasgn: dynamic structure field names must be strings");
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
388
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
389 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
390
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
391 std::list<octave_value_list> next_idx (idx);
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
392
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
393 next_idx.erase (next_idx.begin ());
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
394
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
395 std::string next_type = type.substr (1);
8456
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
396
8580
188d38a553c7 further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents: 8579
diff changeset
397 Cell tmpc (1, 1);
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
398 auto pkey = m_map.seek (key);
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
399 if (pkey != m_map.end ())
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
400 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
401 m_map.contents (pkey).make_unique ();
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
402 tmpc = m_map.contents (pkey);
9087
961410931a4f fix nested struct assignments
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
403 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
404
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
405 // FIXME: better code reuse?
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
406
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
407 if (tmpc.numel () == 1)
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
408 {
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
409 octave_value& tmp = tmpc(0);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
410
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
411 bool orig_undefined = tmp.is_undefined ();
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
412
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
413 if (orig_undefined || tmp.is_zero_by_zero ())
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
414 {
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
415 tmp = octave_value::empty_conv (next_type, rhs);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
416 tmp.make_unique (); // probably a no-op.
8456
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
417 }
c1709a45b45b optimize structure components access
Jaroslav Hajek <highegg@gmail.com>
parents: 8377
diff changeset
418 else
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
419 // optimization: ignore the copy
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
420 // still stored inside our map.
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
421 tmp.make_unique (1);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
422
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
423 t_rhs = (orig_undefined
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
424 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
425 : tmp.subsasgn (next_type, next_idx, rhs));
8546
3d8a914c580e improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents: 8523
diff changeset
426 }
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
427 else
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
428 err_indexed_cs_list ();
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
429 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
430 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
431
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
432 case '{':
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
433 err_invalid_index_type (type_name (), type[0]);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
434 break;
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
435
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
436 default:
33362
2949b89f9711 replace most uses of panic_impossible with error in octave_value classes
John W. Eaton <jwe@octave.org>
parents: 32633
diff changeset
437 error ("unexpected: index not '(', '{', or '.' in octave_struct::subsasgn - please report this bug");
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
438 }
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
439 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
440
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
441 switch (type[0])
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
442 {
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
443 case '(':
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
444 {
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
445 if (n > 1 && type[1] == '.')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
446 {
25337
3ff9192b676e use auto keyword to declare iterator variables where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
447 auto p = idx.begin ();
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
448 octave_value_list key_idx = *++p;
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
449 octave_value_list idxf = idx.front ();
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
450
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
451 if (key_idx.length () != 1)
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
452 error ("subsasgn: dynamic structure field names must be strings");
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
453
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
454 std::string key
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
455 = key_idx(0).xstring_value ("subsasgn: dynamic structure field names must be strings");
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
456
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
457 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
458
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
459 if (t_rhs.is_cs_list ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
460 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
461 Cell tmp_cell = Cell (t_rhs.list_value ());
9529
8e5009334661 partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents: 9522
diff changeset
462
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
463 // Inquire the proper shape of the RHS.
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
464
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
465 dim_vector didx = dims ().redim (idxf.length ());
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
466 for (octave_idx_type k = 0; k < idxf.length (); k++)
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
467 if (! idxf(k).is_magic_colon ())
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
468 didx(k) = idxf(k).numel ();
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
469
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
470 if (didx.numel () == tmp_cell.numel ())
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
471 tmp_cell = tmp_cell.reshape (didx);
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
472
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
473 m_map.assign (idxf, key, tmp_cell);
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
474
31814
53c10d69a9be maint: use "m_" prefix for member variables in class octave_value.
Rik <rik@octave.org>
parents: 31813
diff changeset
475 m_count++;
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
476 retval = octave_value (this);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
477 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
478 else
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
479 {
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30329
diff changeset
480 const octave_map& cmap = const_cast<const octave_map&> (m_map);
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
481 // cast to const reference, avoid forced key insertion.
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
482 if (idxf.all_scalars ()
21098
99d373870017 Fix assigning to trailing singletons for structs and cell (bug #39789, bug #35841)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21022
diff changeset
483 || cmap.contents (key).index (idxf, true).numel () == 1)
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
484 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
485 m_map.assign (idxf,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30329
diff changeset
486 key, Cell (t_rhs.storable_value ()));
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
487
31814
53c10d69a9be maint: use "m_" prefix for member variables in class octave_value.
Rik <rik@octave.org>
parents: 31813
diff changeset
488 m_count++;
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
489 retval = octave_value (this);
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
490 }
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
491 else
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
492 err_nonbraced_cs_list_assignment ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
493 }
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
494 }
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
495 else
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
496 {
23587
0c468af9dc00 maint: Deprecate is_object and replace with isobject.
Rik <rik@octave.org>
parents: 23584
diff changeset
497 if (t_rhs.isstruct () || t_rhs.isobject ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
498 {
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
499 octave_map rhs_map = t_rhs.xmap_value ("invalid structure assignment");
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
500
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
501 m_map.assign (idx.front (), rhs_map);
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
502
31814
53c10d69a9be maint: use "m_" prefix for member variables in class octave_value.
Rik <rik@octave.org>
parents: 31813
diff changeset
503 m_count++;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
504 retval = octave_value (this);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
505 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
506 else
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
507 {
23589
63950abd2f81 maint: Deprecate is_null_type and replace with isnull.
Rik <rik@octave.org>
parents: 23587
diff changeset
508 if (! t_rhs.isnull ())
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
509 error ("invalid structure assignment");
20962
3aa293be0e8d maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents: 20956
diff changeset
510
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
511 m_map.delete_elements (idx.front ());
20962
3aa293be0e8d maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents: 20956
diff changeset
512
31814
53c10d69a9be maint: use "m_" prefix for member variables in class octave_value.
Rik <rik@octave.org>
parents: 31813
diff changeset
513 m_count++;
20962
3aa293be0e8d maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents: 20956
diff changeset
514 retval = octave_value (this);
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
515 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
516 }
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
517 }
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
518 break;
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
519
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
520 case '.':
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
521 {
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
522 octave_value_list key_idx = idx.front ();
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
523
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
524 if (key_idx.length () != 1)
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
525 error ("subsasgn: dynamic structure field names must be strings");
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
526
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
527 std::string key
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
528 = key_idx(0).xstring_value ("subsasgn: dynamic structure field names must be strings");
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
529
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
530 maybe_warn_invalid_field_name (key, "subsasgn");
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
531
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
532 if (t_rhs.is_cs_list ())
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
533 {
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
534 Cell tmp_cell = Cell (t_rhs.list_value ());
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
535
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
536 // The shape of the RHS is irrelevant, we just want
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
537 // the number of elements to agree and to preserve the
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
538 // shape of the left hand side of the assignment.
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
539
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
540 if (numel () == tmp_cell.numel ())
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
541 tmp_cell = tmp_cell.reshape (dims ());
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
542
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
543 m_map.setfield (key, tmp_cell);
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
544 }
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
545 else
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
546 {
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
547 Cell tmp_cell(1, 1);
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
548 tmp_cell(0) = t_rhs.storable_value ();
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
549 m_map.setfield (key, tmp_cell);
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
550 }
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
551
31814
53c10d69a9be maint: use "m_" prefix for member variables in class octave_value.
Rik <rik@octave.org>
parents: 31813
diff changeset
552 m_count++;
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
553 retval = octave_value (this);
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
554 }
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
555 break;
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
556
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
557 case '{':
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21098
diff changeset
558 err_invalid_index_type (type_name (), type[0]);
20740
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
559 break;
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
560
bba1a5fd4d8c eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20711
diff changeset
561 default:
33362
2949b89f9711 replace most uses of panic_impossible with error in octave_value classes
John W. Eaton <jwe@octave.org>
parents: 32633
diff changeset
562 error ("unexpected: index not '(', '{', or '.' in octave_struct::subsasgn - please report this bug");
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
563 }
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
564
10754
92eb5fb58ebc fix resize with structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10753
diff changeset
565 retval.maybe_mutate ();
92eb5fb58ebc fix resize with structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10753
diff changeset
566
3933
f9ea3dcf58ee [project @ 2002-05-15 03:21:00 by jwe]
jwe
parents: 3932
diff changeset
567 return retval;
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
568 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
569
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
570 /*
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
571 %!test
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
572 %! x(1:2) = struct ();
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
573 %! idx = struct ("type", {"()", ".", "."}, "subs", {{1}, "a", "b"});
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
574 %! x = subsasgn (x, idx, 42);
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
575 %! assert (x(1).a.b, 42);
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
576 %! assert (isempty (x(2).a));
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
577
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
578 %!test <*64213>
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
579 %! x(1:2) = struct ();
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
580 %! idx = struct ("type", {"()", "."}, "subs", {{1}, {"a", "b"}});
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
581 %! fail ("x = subsasgn (x, idx, 42);", ...
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
582 %! "structure field names must be strings");
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
583 */
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
584
7046
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
585 octave_value
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
586 octave_struct::do_index_op (const octave_value_list& idx, bool resize_ok)
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
587 {
23880
1b232c0c19e6 Empty indexing of a struct should return the entire struct (bug #51633).
Rik <rik@octave.org>
parents: 23807
diff changeset
588 if (idx.length () == 0)
27061
d61825e693f2 warn about indexing values with () (bug #56167)
John W. Eaton <jwe@octave.org>
parents: 26419
diff changeset
589 {
d61825e693f2 warn about indexing values with () (bug #56167)
John W. Eaton <jwe@octave.org>
parents: 26419
diff changeset
590 warn_empty_index (type_name ());
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
591 return m_map;
27061
d61825e693f2 warn about indexing values with () (bug #56167)
John W. Eaton <jwe@octave.org>
parents: 26419
diff changeset
592 }
23880
1b232c0c19e6 Empty indexing of a struct should return the entire struct (bug #51633).
Rik <rik@octave.org>
parents: 23807
diff changeset
593 else // octave_map handles indexing itself.
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
594 return m_map.index (idx, resize_ok);
7046
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
595 }
fbf8576cf399 [project @ 2007-10-22 12:12:20 by dbateman]
dbateman
parents: 7040
diff changeset
596
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29476
diff changeset
597 std::size_t
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31718
diff changeset
598 octave_struct::byte_size () const
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
599 {
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
600 // Neglect the size of the fieldnames.
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
601
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29476
diff changeset
602 std::size_t retval = 0;
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
603
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
604 for (auto p = m_map.cbegin (); p != m_map.cend (); p++)
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
605 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
606 std::string key = m_map.key (p);
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
607
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
608 octave_value val = octave_value (m_map.contents (p));
4791
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
609
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
610 retval += val.byte_size ();
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
611 }
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
612
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
613 return retval;
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
614 }
62f2fb593455 [project @ 2004-02-20 18:02:59 by jwe]
jwe
parents: 4780
diff changeset
615
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
616 void
18416
bcd71a2531d3 Support disp/display overloading in classdef
Michael Goffioul <michael.goffioul@gmail.com>
parents: 18384
diff changeset
617 octave_struct::print (std::ostream& os, bool)
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
618 {
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
619 print_raw (os);
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
620 }
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
621
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
622 void
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
623 octave_struct::print_raw (std::ostream& os, bool) const
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
624 {
28823
26cfccfee9a0 Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents: 28695
diff changeset
625 octave::unwind_protect_var<int> restore_var (Vstruct_levels_to_print);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
626
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
627 if (Vstruct_levels_to_print >= 0)
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
628 {
29703
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
629 bool max_depth_reached = (Vstruct_levels_to_print-- == 0);
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
630
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
631 bool print_fieldnames_only = (max_depth_reached
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
632 || ! Vprint_struct_array_contents);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
633
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
634 increment_indent_level ();
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
635
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
636 indent (os);
32598
2f3b54f81947 avoid dim_vector copies in more places where possible (bug #64962)
John W. Eaton <jwe@octave.org>
parents: 32546
diff changeset
637 const dim_vector& dv = dims ();
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
638 os << dv.str () << " struct array containing the fields:";
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
639 newline (os);
3932
2e2e32198722 [project @ 2002-05-07 18:10:44 by jwe]
jwe
parents: 3548
diff changeset
640
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
641 increment_indent_level ();
4604
cba347c642e2 [project @ 2003-11-13 04:38:05 by jwe]
jwe
parents: 4561
diff changeset
642
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
643 string_vector key_list = m_map.fieldnames ();
5880
84ca47e311b3 [project @ 2006-07-06 22:34:48 by jwe]
jwe
parents: 5828
diff changeset
644
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20218
diff changeset
645 for (octave_idx_type i = 0; i < key_list.numel (); i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
646 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
647 std::string key = key_list[i];
5880
84ca47e311b3 [project @ 2006-07-06 22:34:48 by jwe]
jwe
parents: 5828
diff changeset
648
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
649 Cell val = m_map.contents (key);
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
650
23354
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
651 if (i > 0 || ! Vcompact_format)
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
652 newline (os);
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
653
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
654 if (print_fieldnames_only)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
655 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
656 indent (os);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
657 os << key;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
658 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
659 else
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
660 {
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
661 octave_value tmp (val);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
662 tmp.print_with_name (os, key);
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
663 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
664 }
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
665
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
666 if (print_fieldnames_only)
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
667 newline (os);
4604
cba347c642e2 [project @ 2003-11-13 04:38:05 by jwe]
jwe
parents: 4561
diff changeset
668
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
669 decrement_indent_level ();
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
670 decrement_indent_level ();
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
671 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
672 else
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
673 {
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
674 indent (os);
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
675 os << "<structure>";
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
676 newline (os);
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
677 }
2376
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
678 }
2142216bf85a [project @ 1996-10-12 01:39:07 by jwe]
jwe
parents:
diff changeset
679
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
680 bool
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
681 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
682 {
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
683 bool retval = false;
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
684
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
685 indent (os);
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
686
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
687 if (Vstruct_levels_to_print < 0)
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
688 os << name << " = ";
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
689 else
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
690 {
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
691 os << name << " =";
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
692 newline (os);
23354
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
693 if (! Vcompact_format)
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
694 newline (os);
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
695
3961
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
696 retval = true;
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
697 }
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
698
635209a37bf4 [project @ 2002-06-03 18:15:47 by jwe]
jwe
parents: 3933
diff changeset
699 return retval;
2901
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
700 }
e6d25bc478dd [project @ 1997-04-30 03:41:26 by jwe]
jwe
parents: 2847
diff changeset
701
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
702 static bool
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
703 scalar (const dim_vector& dims)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
704 {
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
705 return dims.ndims () == 2 && dims(0) == 1 && dims(1) == 1;
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
706 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
707
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
708 std::string
24700
aaf7bcea71dd Silence build warnings about unused parameters.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 24668
diff changeset
709 octave_struct::edit_display (const float_display_format&,
24668
d4dd741b2794 new octave_value functions for formatting output
John W. Eaton <jwe@octave.org>
parents: 24660
diff changeset
710 octave_idx_type r, octave_idx_type c) const
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
711 {
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
712 octave_value val;
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
713 if (m_map.rows () == 1 || m_map.columns () == 1)
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
714 {
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
715 // Vector struct. Columns are fields, rows are values.
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
716
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
717 Cell cval = m_map.contents (c);
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
718
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
719 val = cval(r);
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
720 }
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
721 else
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
722 {
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
723 // 2-d struct array. Rows and columns index individual
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
724 // scalar structs.
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
725
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30329
diff changeset
726 val = m_map(r, c);
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
727 }
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
728
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
729 std::string tname = val.type_name ();
32598
2f3b54f81947 avoid dim_vector copies in more places where possible (bug #64962)
John W. Eaton <jwe@octave.org>
parents: 32546
diff changeset
730 const dim_vector& dv = val.dims ();
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
731 std::string dimstr = dv.str ();
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
732 return "[" + dimstr + " " + tname + "]";
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
733 }
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
734
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
735
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
736 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
737 octave_struct::save_ascii (std::ostream& os)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
738 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
739 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
740
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
741 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
743 const dim_vector dv = dims ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
744
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
745 os << "# ndims: " << dv.ndims () << "\n";
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
746
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
747 for (int i = 0; i < dv.ndims (); i++)
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23803
diff changeset
748 os << ' ' << dv(i);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
749 os << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
750
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
751 os << "# length: " << nf << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
752
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
753 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
754 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
755 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
756
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
757 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
758 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
759 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
760
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
761 octave_value val = m_map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
762
20447
c6224b4e7774 maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents: 20232
diff changeset
763 bool b = save_text_data (os, val, key, false, 0);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
764
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
765 if (! b)
18384
bd9d34f28b0f Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents: 17787
diff changeset
766 return ! os.fail ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
767 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
768
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
769 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
770 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
771
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
772 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
773 octave_struct::load_ascii (std::istream& is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
774 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
775 octave_idx_type len = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
776 dim_vector dv (1, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
777 bool success = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
778
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
779 // KLUGE: earlier Octave versions did not save extra dimensions with struct,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
780 // and as a result did not preserve dimensions for empty structs.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
781 // The default dimensions were 1x1, which we want to preserve.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
782 string_vector keywords(2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
783
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
784 keywords[0] = "ndims";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
785 keywords[1] = "length";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
786
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
787 std::string kw;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
788
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
789 if (extract_keyword (is, keywords, kw, len, true))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
790 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
791 if (kw == keywords[0])
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
792 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
793 int mdims = std::max (static_cast<int> (len), 2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
794 dv.resize (mdims);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
795 for (int i = 0; i < mdims; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
796 is >> dv(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
797
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
798 success = extract_keyword (is, keywords[1], len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
799 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
800 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
801 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
802 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
803
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
804 if (! success || len < 0)
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
805 error ("load: failed to extract number of elements in structure");
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
806
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
807 if (len > 0)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
808 {
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
809 octave_map m (dv);
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
810
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
811 for (octave_idx_type j = 0; j < len; j++)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
812 {
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
813 octave_value t2;
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
814 bool dummy;
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
815
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
816 // recurse to read cell elements
28179
bea3f8f96f7a Load structures with arbitrary string fieldnames (bug #50831, bug #46645).
Olaf Till <i7tiol@t-online.de>
parents: 27923
diff changeset
817 std::string nm = read_text_data (is, "", dummy, t2, j, false);
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
818
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
819 if (! is)
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
820 break;
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
821
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
822 Cell tcell = (t2.iscell () ? t2.xcell_value ("load: internal error loading struct elements") :
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
823 Cell (t2));
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
824
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
825 m.setfield (nm, tcell);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
826 }
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
827
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
828 if (! is)
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
829 error ("load: failed to load structure");
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
830
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
831 m_map = m;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
832 }
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
833 else if (len == 0)
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
834 m_map = octave_map (dv);
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
835 else
33362
2949b89f9711 replace most uses of panic_impossible with error in octave_value classes
John W. Eaton <jwe@octave.org>
parents: 32633
diff changeset
836 error ("unexpected: len < 0 in octave_struct::load_ascii - please report this bug");
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
837
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
838 return success;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
839 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
840
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
841 bool
26399
586413770c7f pass save_as_floats by value in octave_value save_binary functions
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
842 octave_struct::save_binary (std::ostream& os, bool save_as_floats)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
843 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
844 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
845
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
846 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
847
32598
2f3b54f81947 avoid dim_vector copies in more places where possible (bug #64962)
John W. Eaton <jwe@octave.org>
parents: 32546
diff changeset
848 const dim_vector& dv = dims ();
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
849 if (dv.ndims () < 1)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
850 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
851
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
852 // Use negative value for ndims
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
853 int32_t di = - dv.ndims ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
854 os.write (reinterpret_cast<char *> (&di), 4);
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
855 for (int i = 0; i < dv.ndims (); i++)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
856 {
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
857 di = dv(i);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
858 os.write (reinterpret_cast<char *> (&di), 4);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
859 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
860
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
861 int32_t len = nf;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
862 os.write (reinterpret_cast<char *> (&len), 4);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
863
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
864 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
865 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
866 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
867
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
868 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
869 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
870 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
871
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
872 octave_value val = m_map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
873
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
874 bool b = save_binary_data (os, val, key, "", 0, save_as_floats);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
875
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
876 if (! b)
18384
bd9d34f28b0f Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents: 17787
diff changeset
877 return ! os.fail ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
878 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
879
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
880 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
881 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
882
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
883 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
884 octave_struct::load_binary (std::istream& is, bool swap,
21739
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
885 octave::mach_info::float_format fmt)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
886 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
887 bool success = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
888 int32_t len;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
889 if (! is.read (reinterpret_cast<char *> (&len), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
890 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
891 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
892 swap_bytes<4> (&len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
893
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
894 dim_vector dv (1, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
895
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
896 if (len < 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
897 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
898 // We have explicit dimensions.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
899 int mdims = -len;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
900
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
901 int32_t di;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
902 dv.resize (mdims);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
903
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
904 for (int i = 0; i < mdims; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
905 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
906 if (! is.read (reinterpret_cast<char *> (&di), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
907 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
908 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
909 swap_bytes<4> (&di);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
910 dv(i) = di;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
911 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
912
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
913 if (! is.read (reinterpret_cast<char *> (&len), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
914 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
915 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
916 swap_bytes<4> (&len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
917 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
918
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
919 if (len > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
920 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
921 octave_map m (dv);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
922
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
923 for (octave_idx_type j = 0; j < len; j++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
924 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
925 octave_value t2;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
926 bool dummy;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
927 std::string doc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
928
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
929 // recurse to read cell elements
21017
93748bcaec17 maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents: 20979
diff changeset
930 std::string nm = read_binary_data (is, swap, fmt, "",
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
931 dummy, t2, doc);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
932
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20952
diff changeset
933 if (! is)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
934 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
935
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
936 Cell tcell = (t2.iscell () ? t2.xcell_value ("load: internal error loading struct elements") :
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
937 Cell (t2));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
938
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
939 m.setfield (nm, tcell);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
940 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
941
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
942 if (! is)
20952
072559bd31f2 maint: Eliminate useless statements after error().
Rik <rik@octave.org>
parents: 20939
diff changeset
943 error ("load: failed to load structure");
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
944
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
945 m_map = m;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
946 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
947 else if (len == 0)
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
948 m_map = octave_map (dv);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
949 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
950 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
951
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
952 return success;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
953 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
954
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
955 bool
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
956 octave_struct::save_hdf5 (octave_hdf5_id loc_id, const char *name,
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
957 bool save_as_floats)
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
958 {
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
959 #if defined (HAVE_HDF5)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
960
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
961 hid_t data_hid = -1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
962
21211
2cf8bc5c7017 use "#if defined (HAVE_FOO)" instead of "#if HAVE_FOO" for feature tests
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
963 #if defined (HAVE_HDF5_18)
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
964 data_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT, octave_H5P_DEFAULT,
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
965 octave_H5P_DEFAULT);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
966 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
967 data_hid = H5Gcreate (loc_id, name, 0);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
968 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
969 if (data_hid < 0) return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
970
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
971 // recursively add each element of the structure to this group
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
972 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
973
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
974 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
975
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
976 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
977 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
978 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
979
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
980 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
981 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
982 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
983
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
984 octave_value val = m_map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
985
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
986 bool retval2 = add_hdf5_data (data_hid, val, key, "", false,
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
987 save_as_floats);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
988
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
989 if (! retval2)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
990 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
991 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
992
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
993 H5Gclose (data_hid);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
994
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
995 return true;
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
996
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
997 #else
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
998 octave_unused_parameter (loc_id);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
999 octave_unused_parameter (name);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1000 octave_unused_parameter (save_as_floats);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1001
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
1002 warn_save ("hdf5");
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1003
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1004 return false;
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1005 #endif
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1006 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1007
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1008 bool
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1009 octave_struct::load_hdf5 (octave_hdf5_id loc_id, const char *name)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1010 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1011 bool retval = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1012
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1013 #if defined (HAVE_HDF5)
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1014
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1015 hdf5_callback_data dsub;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1016
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1017 herr_t retval2 = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1018 octave_map m (dim_vector (1, 1));
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1019 int current_item = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1020 hsize_t num_obj = 0;
21211
2cf8bc5c7017 use "#if defined (HAVE_FOO)" instead of "#if HAVE_FOO" for feature tests
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
1021 #if defined (HAVE_HDF5_18)
21022
ebc439187d29 avoid old-style cast warnings from HDF5 macros used in C++ sources
John W. Eaton <jwe@octave.org>
parents: 21017
diff changeset
1022 hid_t group_id = H5Gopen (loc_id, name, octave_H5P_DEFAULT);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1023 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1024 hid_t group_id = H5Gopen (loc_id, name);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1025 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1026 H5Gget_num_objs (group_id, &num_obj);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1027 H5Gclose (group_id);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1028
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1029 // FIXME: fields appear to be sorted alphabetically on loading.
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1030 // Why is that happening?
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1031
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1032 while (current_item < static_cast<int> (num_obj)
22028
5c949eecb6dd use int64_t for octave_hdf5_id (bug #47858)
Stefan Miereis <stefan.miereis@gmx.de>
parents: 22022
diff changeset
1033 && (retval2 = hdf5_h5g_iterate (loc_id, name, &current_item,
5c949eecb6dd use int64_t for octave_hdf5_id (bug #47858)
Stefan Miereis <stefan.miereis@gmx.de>
parents: 22022
diff changeset
1034 &dsub)) > 0)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1035 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1036 octave_value t2 = dsub.tc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1037
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
1038 Cell tcell = (t2.iscell () ? t2.xcell_value ("load: internal error loading struct elements") :
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
1039 Cell (t2));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1040
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1041 m.setfield (dsub.name, tcell);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1042
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1043 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1044
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1045 if (retval2 >= 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1046 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1047 m_map = m;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1048 retval = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1049 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1050
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1051 #else
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1052 octave_unused_parameter (loc_id);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1053 octave_unused_parameter (name);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1054
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
1055 warn_load ("hdf5");
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1056 #endif
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1057
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1058 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1059 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1060
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1061 mxArray *
28126
4c21f99b4ad5 handle interleaved complex data and new typed data access functions for mex
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
1062 octave_struct::as_mxArray (bool interleaved) const
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1063 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1064 int nf = nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1065 string_vector kv = map_keys ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1066
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1067 OCTAVE_LOCAL_BUFFER (const char *, f, nf);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1068
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1069 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1070 f[i] = kv[i].c_str ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1071
28126
4c21f99b4ad5 handle interleaved complex data and new typed data access functions for mex
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
1072 mxArray *retval = new mxArray (interleaved, dims (), nf, f);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1073
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1074 mxArray **elts = static_cast<mxArray **> (retval->get_data ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1075
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1076 mwSize nel = numel ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1077
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1078 mwSize ntot = nf * nel;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1079
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1080 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1081 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1082 Cell c = m_map.contents (kv[i]);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1083
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1084 const octave_value *p = c.data ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1085
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1086 mwIndex k = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1087 for (mwIndex j = i; j < ntot; j += nf)
28126
4c21f99b4ad5 handle interleaved complex data and new typed data access functions for mex
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
1088 elts[j] = new mxArray (interleaved, p[k++]);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1089 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1090
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1091 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1092 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1093
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1094 octave_value
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1095 octave_struct::fast_elem_extract (octave_idx_type n) const
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1096 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1097 if (n < m_map.numel ())
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1098 return m_map.checkelem (n);
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1099 else
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1100 return octave_value ();
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1101 }
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1102
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1103 bool
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1104 octave_struct::fast_elem_insert (octave_idx_type n,
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1105 const octave_value& x)
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1106 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1107 bool retval = false;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1108
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1109 if (n < m_map.numel ())
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1110 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1111 // To avoid copying the scalar struct, it just stores a pointer to
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1112 // itself.
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1113 const octave_scalar_map *sm_ptr;
32513
1de97b475564 maint: Add space " " between C++ cast and argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 32495
diff changeset
1114 void *here = reinterpret_cast<void *> (&sm_ptr);
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14625
diff changeset
1115 return (x.get_rep ().fast_elem_insert_self (here, btyp_struct)
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1116 && m_map.fast_elem_insert (n, *sm_ptr));
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1117 }
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1118
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1119 return retval;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1120 }
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1121
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1122 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_scalar_struct, "scalar struct",
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1123 "struct");
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1124
29476
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
1125 void
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
1126 octave_scalar_struct::break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame)
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
1127 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1128 for (octave_idx_type i = 0; i < m_map.nfields (); i++)
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1129 m_map.contents(i).break_closure_cycles (frame);
29476
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
1130 }
c74ff452e2bb avoid memory leaks when returning handles to nested functions
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
1131
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1132 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1133 octave_scalar_struct::dotref (const octave_value_list& idx, bool auto_add)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1134 {
15613
126285fce876 check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15534
diff changeset
1135 octave_value retval;
126285fce876 check for allowed struct field names in subsref and subsasgn
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 15534
diff changeset
1136
30723
08b08b7f05b2 Replace direct calls to C library assert() with Octave specialty functions in libinterp/ (bug #61753)
Arun Giridhar <arungiridhar@gmail.com> and Rik <rik@octave.org>
parents: 30588
diff changeset
1137 panic_if (idx.length () != 1);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1138
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1139 std::string nm = idx(0).string_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1140
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1141 maybe_warn_invalid_field_name (nm, "subsref");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1142
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1143 retval = m_map.getfield (nm);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1144
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1145 if (! auto_add && retval.is_undefined ())
15731
18f168880226 error_ids: Adding ids and documentation
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 15614
diff changeset
1146 error_with_id ("Octave:invalid-indexing",
18f168880226 error_ids: Adding ids and documentation
Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
parents: 15614
diff changeset
1147 "structure has no member '%s'", nm.c_str ());
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1148
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1149 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1150 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1151
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1152 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1153 octave_scalar_struct::subsref (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1154 const std::list<octave_value_list>& idx)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1155 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1156 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1157
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1158 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1159 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1160 int skip = 1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1161
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1162 retval = dotref (idx.front ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1163
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1164 if (idx.size () > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1165 retval = retval.next_subsref (type, idx, skip);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1166 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1167 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1168 retval = to_array ().subsref (type, idx);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1169
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1170 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1171 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1172
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1173 octave_value_list
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1174 octave_scalar_struct::subsref (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1175 const std::list<octave_value_list>& idx,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1176 int nargout)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1177 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1178 octave_value_list retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1179
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1180 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1181 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1182 int skip = 1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1183
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1184 retval(0) = dotref (idx.front ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1185
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1186 if (idx.size () > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1187 retval = retval(0).next_subsref (nargout, type, idx, skip);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1188 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1189 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1190 retval = to_array ().subsref (type, idx, nargout);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1191
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1192 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1193 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1194
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1195 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1196 octave_scalar_struct::subsref (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1197 const std::list<octave_value_list>& idx,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1198 bool auto_add)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1199 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1200 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1201
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1202 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1203 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1204 int skip = 1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1205
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1206 retval = dotref (idx.front (), auto_add);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1207
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1208 if (idx.size () > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1209 retval = retval.next_subsref (auto_add, type, idx, skip);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1210 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1211 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1212 retval = to_array ().subsref (type, idx, auto_add);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1213
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1214 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1215 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1216
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1217 /*
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1218 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1219 %! x(1).a.a = 1;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1220 %! x(2).a.a = 2;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1221 %! assert (size (x), [1, 2]);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1222 %! assert (x(1).a.a, 1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1223 %! assert (x(2).a.a, 2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1224 */
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1225
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1226 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1227 octave_scalar_struct::numeric_conv (const octave_value& val,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1228 const std::string& type)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1229 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1230 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1231
23584
7ed6b258db91 maint: Deprecate is_map and replace with isstruct.
Rik <rik@octave.org>
parents: 23577
diff changeset
1232 if (type.length () > 0 && type[0] == '.' && ! val.isstruct ())
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1233 retval = octave_map ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1234 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1235 retval = val;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1236
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1237 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1238 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1239
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1240 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1241 octave_scalar_struct::subsasgn (const std::string& type,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1242 const std::list<octave_value_list>& idx,
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1243 const octave_value& rhs)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1244 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1245 octave_value retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1246
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1247 if (idx.front ().empty ())
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1248 error ("subsasgn: missing index in indexed assignment");
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1249
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1250 if (type[0] == '.')
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1251 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1252 int n = type.length ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1253
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1254 octave_value t_rhs = rhs;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1255
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1256 octave_value_list key_idx = idx.front ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1257
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1258 if (key_idx.length () != 1)
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1259 error ("subsasgn: structure field names must be strings");
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1260
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1261 std::string key
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1262 = key_idx(0).xstring_value ("subsasgn: structure field names must be strings");
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1263
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1264 maybe_warn_invalid_field_name (key, "subsasgn");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1265
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1266 if (n > 1)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1267 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1268 std::list<octave_value_list> next_idx (idx);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1269
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1270 next_idx.erase (next_idx.begin ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1271
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1272 std::string next_type = type.substr (1);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1273
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1274 octave_value tmp;
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1275 auto pkey = m_map.seek (key);
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1276 if (pkey != m_map.end ())
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1277 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1278 m_map.contents (pkey).make_unique ();
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1279 tmp = m_map.contents (pkey);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1280 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1281
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1282 bool orig_undefined = tmp.is_undefined ();
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1283
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1284 if (orig_undefined || tmp.is_zero_by_zero ())
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1285 {
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1286 tmp = octave_value::empty_conv (next_type, rhs);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1287 tmp.make_unique (); // probably a no-op.
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1288 }
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1289 else
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1290 // optimization: ignore the copy still stored inside our m_map.
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1291 tmp.make_unique (1);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1292
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1293 t_rhs = (orig_undefined
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1294 ? tmp.undef_subsasgn (next_type, next_idx, rhs)
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20447
diff changeset
1295 : tmp.subsasgn (next_type, next_idx, rhs));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1296 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1297
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1298 m_map.setfield (key, t_rhs.storable_value ());
10749
df1a3e0ebbff important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents: 10747
diff changeset
1299
31814
53c10d69a9be maint: use "m_" prefix for member variables in class octave_value.
Rik <rik@octave.org>
parents: 31813
diff changeset
1300 m_count++;
10749
df1a3e0ebbff important fixes for struct rewrite(1)
Jaroslav Hajek <highegg@gmail.com>
parents: 10747
diff changeset
1301 retval = this;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1302 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1303 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1304 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1305 // Forward this case to octave_struct.
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1306 octave_value tmp (new octave_struct (octave_map (m_map)));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1307 retval = tmp.subsasgn (type, idx, rhs);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1308 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1309
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1310 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1311 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1312
32215
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1313 /*
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1314 %!test
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1315 %! x = struct ();
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1316 %! idx = struct ("type", ".", "subs", {"a", "b"});
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1317 %! x = subsasgn (x, idx, 42);
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1318 %! assert (x.a.b, 42);
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1319
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1320 %!test <*64213>
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1321 %! x = struct ();
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1322 %! idx = struct ("type", ".", "subs", {{"a", "b"}});
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1323 %! fail ("x = subsasgn (x, idx, 42)", ...
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1324 %! "structure field names must be strings");
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1325 */
ce1ae40de0f0 subsasgn: Avoid panic on invalid field names (bug #64213).
Markus Mützel <markus.muetzel@gmx.de>
parents: 31706
diff changeset
1326
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1327 octave_value
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1328 octave_scalar_struct::do_index_op (const octave_value_list& idx, bool resize_ok)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1329 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1330 // octave_map handles indexing itself.
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1331 return octave_map (m_map).index (idx, resize_ok);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1332 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1333
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29476
diff changeset
1334 std::size_t
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31718
diff changeset
1335 octave_scalar_struct::byte_size () const
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1336 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1337 // Neglect the size of the fieldnames.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1338
29654
d13d090cb03a use std::size_t and std::ptrdiff_t in C++ code (bug #60471)
John W. Eaton <jwe@octave.org>
parents: 29476
diff changeset
1339 std::size_t retval = 0;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1340
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1341 for (auto p = m_map.cbegin (); p != m_map.cend (); p++)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1342 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1343 std::string key = m_map.key (p);
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1344
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1345 octave_value val = octave_value (m_map.contents (p));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1346
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1347 retval += val.byte_size ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1348 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1349
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1350 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1351 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1352
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1353 void
18416
bcd71a2531d3 Support disp/display overloading in classdef
Michael Goffioul <michael.goffioul@gmail.com>
parents: 18384
diff changeset
1354 octave_scalar_struct::print (std::ostream& os, bool)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1355 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1356 print_raw (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1357 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1358
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1359 void
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1360 octave_scalar_struct::print_raw (std::ostream& os, bool) const
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1361 {
28823
26cfccfee9a0 Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents: 28695
diff changeset
1362 octave::unwind_protect_var<int> restore_var (Vstruct_levels_to_print);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1363
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1364 if (Vstruct_levels_to_print >= 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1365 {
29703
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1366 bool max_depth_reached = (Vstruct_levels_to_print-- == 0);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1367
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1368 bool print_fieldnames_only = max_depth_reached;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1369
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1370 increment_indent_level ();
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1371 increment_indent_level ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1372
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1373 string_vector key_list = m_map.fieldnames ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1374
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20218
diff changeset
1375 for (octave_idx_type i = 0; i < key_list.numel (); i++)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1376 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1377 std::string key = key_list[i];
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1378
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1379 octave_value val = m_map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1380
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1381 if (print_fieldnames_only)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1382 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1383 indent (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1384 os << key;
32598
2f3b54f81947 avoid dim_vector copies in more places where possible (bug #64962)
John W. Eaton <jwe@octave.org>
parents: 32546
diff changeset
1385 const dim_vector& dv = val.dims ();
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23803
diff changeset
1386 os << ": " << dv.str () << ' ' << val.type_name ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1387 newline (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1388 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1389 else
12143
ed129a03f93c octave_scalar_struct::print_raw: avoid unnecessary conversion of map contents to Cell
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
1390 val.print_with_name (os, key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1391 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1392
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1393 decrement_indent_level ();
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1394 decrement_indent_level ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1395 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1396 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1397 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1398 indent (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1399 os << "<structure>";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1400 newline (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1401 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1402 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1403
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1404 bool
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1405 octave_scalar_struct::print_name_tag (std::ostream& os,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1406 const std::string& name) const
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1407 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1408 bool retval = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1409
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1410 indent (os);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1411
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1412 if (Vstruct_levels_to_print < 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1413 os << name << " = ";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1414 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1415 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1416 os << name << " =";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1417 newline (os);
23354
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
1418 if (! Vcompact_format)
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
1419 newline (os);
1a2941fb8ffd Change display of structs to respect compact format.
Rik <rik@octave.org>
parents: 23220
diff changeset
1420
29703
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1421 increment_indent_level ();
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1422
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1423 indent (os);
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1424 os << "scalar structure containing the fields:";
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1425 newline (os);
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1426 if (! Vcompact_format)
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1427 newline (os);
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1428
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1429 decrement_indent_level ();
777c16616c40 Make disp() Matlab-compatible for scalar struct variables (bug #60643).
Rik <rik@octave.org>
parents: 29655
diff changeset
1430
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1431 retval = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1432 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1433
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1434 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1435 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1436
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1437 std::string
24700
aaf7bcea71dd Silence build warnings about unused parameters.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 24668
diff changeset
1438 octave_scalar_struct::edit_display (const float_display_format&,
24668
d4dd741b2794 new octave_value functions for formatting output
John W. Eaton <jwe@octave.org>
parents: 24660
diff changeset
1439 octave_idx_type r, octave_idx_type) const
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1440 {
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1441 // Scalar struct. Rows are fields, single column for values.
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1442
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1443 octave_value val = m_map.contents (r);
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1444
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1445 std::string tname = val.type_name ();
32598
2f3b54f81947 avoid dim_vector copies in more places where possible (bug #64962)
John W. Eaton <jwe@octave.org>
parents: 32546
diff changeset
1446 const dim_vector& dv = val.dims ();
24660
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1447 std::string dimstr = dv.str ();
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1448 return "[" + dimstr + " " + tname + "]";
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1449 }
a4ea36915e38 handle structure arrays in the variable editor
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
1450
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1451 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1452 octave_scalar_struct::save_ascii (std::ostream& os)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1453 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1454 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1455
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1456 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1457
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1458 const dim_vector dv = dims ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1459
21660
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
1460 os << "# ndims: " << dv.ndims () << "\n";
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
1461
53728df3e4c9 maint: for clarity, call ndims() rather than length() on dimension vectors.
Rik <rik@octave.org>
parents: 21568
diff changeset
1462 for (int i = 0; i < dv.ndims (); i++)
23807
336f89b6208b Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents: 23803
diff changeset
1463 os << ' ' << dv(i);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1464 os << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1465
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1466 os << "# length: " << nf << "\n";
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1467
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1468 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1469 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1470 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1471
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1472 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1473 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1474 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1475
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1476 octave_value val = m_map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1477
20447
c6224b4e7774 maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents: 20232
diff changeset
1478 bool b = save_text_data (os, val, key, false, 0);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1479
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1480 if (! b)
18384
bd9d34f28b0f Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents: 17787
diff changeset
1481 return ! os.fail ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1482 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1483
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1484 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1485 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1486
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1487 bool
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1488 octave_scalar_struct::load_ascii (std::istream& is)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1489 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1490 octave_idx_type len = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1491
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1492 if (! extract_keyword (is, "length", len) || len < 0)
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1493 error ("load: failed to extract number of elements in structure");
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1494
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1495 if (len > 0)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1496 {
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1497 octave_scalar_map m;
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1498
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1499 for (octave_idx_type j = 0; j < len; j++)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1500 {
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1501 octave_value t2;
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1502 bool dummy;
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1503
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1504 // recurse to read cell elements
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1505 std::string nm
28179
bea3f8f96f7a Load structures with arbitrary string fieldnames (bug #50831, bug #46645).
Olaf Till <i7tiol@t-online.de>
parents: 27923
diff changeset
1506 = read_text_data (is, "", dummy, t2, j, false);
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1507
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1508 if (! is)
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1509 break;
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1510
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1511 m.setfield (nm, t2);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1512 }
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1513
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1514 if (! is)
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1515 error ("load: failed to load structure");
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1516
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1517 m_map = m;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1518 }
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1519 else if (len == 0)
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1520 m_map = octave_scalar_map ();
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1521 else
33362
2949b89f9711 replace most uses of panic_impossible with error in octave_value classes
John W. Eaton <jwe@octave.org>
parents: 32633
diff changeset
1522 error ("unexpected: len < 0 in octave_scalar_struct::load_ascii - please report this bug");
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1523
21124
95f8c8cdbffe maint: Eliminate 'bool success' variable where possible.
Rik <rik@octave.org>
parents: 21121
diff changeset
1524 return true;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1525 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1526
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1527 bool
26399
586413770c7f pass save_as_floats by value in octave_value save_binary functions
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
1528 octave_scalar_struct::save_binary (std::ostream& os, bool save_as_floats)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1529 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1530 octave_map m = map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1531
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1532 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1533
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1534 int32_t len = nf;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1535 os.write (reinterpret_cast<char *> (&len), 4);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1536
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1537 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1538 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1539 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1540
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1541 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1542 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1543 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1544
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1545 octave_value val = m_map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1546
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1547 bool b = save_binary_data (os, val, key, "", 0, save_as_floats);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1548
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1549 if (! b)
18384
bd9d34f28b0f Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents: 17787
diff changeset
1550 return ! os.fail ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1551 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1552
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1553 return true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1554 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1555
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1556 bool
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1557 octave_scalar_struct::load_binary (std::istream& is, bool swap,
21739
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
1558 octave::mach_info::float_format fmt)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1559 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1560 bool success = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1561 int32_t len;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1562 if (! is.read (reinterpret_cast<char *> (&len), 4))
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1563 return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1564 if (swap)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1565 swap_bytes<4> (&len);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1566
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1567 if (len > 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1568 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1569 octave_scalar_map m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1570
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1571 for (octave_idx_type j = 0; j < len; j++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1572 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1573 octave_value t2;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1574 bool dummy;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1575 std::string doc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1576
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1577 // recurse to read cell elements
21017
93748bcaec17 maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents: 20979
diff changeset
1578 std::string nm = read_binary_data (is, swap, fmt, "",
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1579 dummy, t2, doc);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1580
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20952
diff changeset
1581 if (! is)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1582 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1583
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1584 m.setfield (nm, t2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1585 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1586
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1587 if (! is)
20952
072559bd31f2 maint: Eliminate useless statements after error().
Rik <rik@octave.org>
parents: 20939
diff changeset
1588 error ("load: failed to load structure");
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
1589
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1590 m_map = m;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1591 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1592 else if (len == 0)
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1593 m_map = octave_scalar_map ();
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1594 else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1595 success = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1596
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1597 return success;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1598 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1599
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1600 bool
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1601 octave_scalar_struct::save_hdf5 (octave_hdf5_id loc_id, const char *name,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1602 bool save_as_floats)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1603 {
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1604 #if defined (HAVE_HDF5)
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1605
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1606 hid_t data_hid = -1;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1607
21211
2cf8bc5c7017 use "#if defined (HAVE_FOO)" instead of "#if HAVE_FOO" for feature tests
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
1608 #if defined (HAVE_HDF5_18)
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
1609 data_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT, octave_H5P_DEFAULT,
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
1610 octave_H5P_DEFAULT);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1611 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1612 data_hid = H5Gcreate (loc_id, name, 0);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1613 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1614 if (data_hid < 0) return false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1615
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1616 // recursively add each element of the structure to this group
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1617 octave_scalar_map m = scalar_map_value ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1618
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1619 octave_idx_type nf = m.nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1620
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1621 // Iterating over the list of keys will preserve the order of the
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1622 // fields.
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1623 string_vector keys = m.fieldnames ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1624
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1625 for (octave_idx_type i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1626 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1627 std::string key = keys(i);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1628
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1629 octave_value val = m_map.contents (key);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1630
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1631 bool retval2 = add_hdf5_data (data_hid, val, key, "", false,
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1632 save_as_floats);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1633
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1634 if (! retval2)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1635 break;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1636 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1637
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1638 H5Gclose (data_hid);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1639
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1640 return true;
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1641
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1642 #else
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1643 octave_unused_parameter (loc_id);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1644 octave_unused_parameter (name);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1645 octave_unused_parameter (save_as_floats);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1646
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
1647 warn_save ("hdf5");
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1648
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1649 return false;
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1650 #endif
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1651 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1652
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1653 bool
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1654 octave_scalar_struct::load_hdf5 (octave_hdf5_id loc_id, const char *name)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1655 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1656 bool retval = false;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1657
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1658 #if defined (HAVE_HDF5)
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1659
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1660 hdf5_callback_data dsub;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1661
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1662 herr_t retval2 = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1663 octave_scalar_map m;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1664 int current_item = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1665 hsize_t num_obj = 0;
21211
2cf8bc5c7017 use "#if defined (HAVE_FOO)" instead of "#if HAVE_FOO" for feature tests
John W. Eaton <jwe@octave.org>
parents: 21200
diff changeset
1666 #if defined (HAVE_HDF5_18)
21022
ebc439187d29 avoid old-style cast warnings from HDF5 macros used in C++ sources
John W. Eaton <jwe@octave.org>
parents: 21017
diff changeset
1667 hid_t group_id = H5Gopen (loc_id, name, octave_H5P_DEFAULT);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1668 #else
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1669 hid_t group_id = H5Gopen (loc_id, name);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1670 #endif
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1671 H5Gget_num_objs (group_id, &num_obj);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1672 H5Gclose (group_id);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1673
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1674 // FIXME: fields appear to be sorted alphabetically on loading.
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1675 // Why is that happening?
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1676
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1677 while (current_item < static_cast<int> (num_obj)
22028
5c949eecb6dd use int64_t for octave_hdf5_id (bug #47858)
Stefan Miereis <stefan.miereis@gmx.de>
parents: 22022
diff changeset
1678 && (retval2 = hdf5_h5g_iterate (loc_id, name, &current_item,
5c949eecb6dd use int64_t for octave_hdf5_id (bug #47858)
Stefan Miereis <stefan.miereis@gmx.de>
parents: 22022
diff changeset
1679 &dsub)) > 0)
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1680 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1681 octave_value t2 = dsub.tc;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1682
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1683 m.setfield (dsub.name, t2);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1684
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1685 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1686
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1687 if (retval2 >= 0)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1688 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1689 m_map = m;
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1690 retval = true;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1691 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1692
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1693 #else
21691
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1694 octave_unused_parameter (loc_id);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1695 octave_unused_parameter (name);
263d18409fdf Eliminate unused variable warnings for conditionally compiled code.
John W. Eaton <jwe@octave.org>
parents: 21660
diff changeset
1696
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
1697 warn_load ("hdf5");
19863
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1698 #endif
09ed6f7538dd avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents: 19852
diff changeset
1699
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1700 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1701 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1702
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1703 mxArray *
28126
4c21f99b4ad5 handle interleaved complex data and new typed data access functions for mex
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
1704 octave_scalar_struct::as_mxArray (bool interleaved) const
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1705 {
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1706 int nf = nfields ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1707 string_vector kv = map_keys ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1708
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1709 OCTAVE_LOCAL_BUFFER (const char *, f, nf);
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1710
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1711 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1712 f[i] = kv[i].c_str ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1713
28126
4c21f99b4ad5 handle interleaved complex data and new typed data access functions for mex
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
1714 mxArray *retval = new mxArray (interleaved, dims (), nf, f);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1715
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1716 mxArray **elts = static_cast<mxArray **> (retval->get_data ());
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1717
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1718 mwSize nel = numel ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1719
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1720 mwSize ntot = nf * nel;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1721
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1722 for (int i = 0; i < nf; i++)
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1723 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1724 Cell c = m_map.contents (kv[i]);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1725
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1726 const octave_value *p = c.data ();
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1727
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1728 mwIndex k = 0;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1729 for (mwIndex j = i; j < ntot; j += nf)
28126
4c21f99b4ad5 handle interleaved complex data and new typed data access functions for mex
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
1730 elts[j] = new mxArray (interleaved, p[k++]);
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1731 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1732
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1733 return retval;
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1734 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1735
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1736 octave_value
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31718
diff changeset
1737 octave_scalar_struct::to_array ()
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1738 {
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1739 return new octave_struct (octave_map (m_map));
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1740 }
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1741
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1742 bool
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1743 octave_scalar_struct::fast_elem_insert_self (void *where,
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
1744 builtin_type_t btyp) const
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1745 {
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1746
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1747 if (btyp == btyp_struct)
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1748 {
32513
1de97b475564 maint: Add space " " between C++ cast and argument.
Markus Mützel <markus.muetzel@gmx.de>
parents: 32495
diff changeset
1749 *(reinterpret_cast<const octave_scalar_map **> (where)) = &m_map;
10760
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1750 return true;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1751 }
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1752 else
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1753 return false;
76079e505f9d optimize cellfun with uniform struct output
Jaroslav Hajek <highegg@gmail.com>
parents: 10754
diff changeset
1754 }
10742
604e13a89c7f initial code for structs rewrite
Jaroslav Hajek <highegg@gmail.com>
parents: 10691
diff changeset
1755
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 31260
diff changeset
1756 OCTAVE_BEGIN_NAMESPACE(octave)
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29703
diff changeset
1757
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1758 DEFUN (struct, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1759 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1760 @deftypefn {} {@var{s} =} struct ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1761 @deftypefnx {} {@var{s} =} struct (@var{field1}, @var{value1}, @var{field2}, @var{value2}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1762 @deftypefnx {} {@var{s} =} struct (@var{obj})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1763
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1764 Create a scalar or array structure and initialize its values.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1765
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1766 The @var{field1}, @var{field2}, @dots{} variables are strings specifying the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1767 names of the fields and the @var{value1}, @var{value2}, @dots{} variables
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1768 can be of any type.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1769
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1770 If the values are cell arrays, create a structure array and initialize its
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1771 values. The dimensions of each cell array of values must match. Singleton
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1772 cells and non-cell values are repeated so that they fill the entire array.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1773 If the cells are empty, create an empty structure array with the specified
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1774 field names.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1775
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1776 If the argument is an object, return the underlying struct.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1777
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1778 Observe that the syntax is optimized for struct @strong{arrays}. Consider
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1779 the following examples:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1780
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1781 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1782 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1783 struct ("foo", 1)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1784 @result{} scalar structure containing the fields:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1785 foo = 1
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1786
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1787 struct ("foo", @{@})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1788 @result{} 0x0 struct array containing the fields:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1789 foo
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1790
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1791 struct ("foo", @{ @{@} @})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1792 @result{} scalar structure containing the fields:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1793 foo = @{@}(0x0)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1794
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1795 struct ("foo", @{1, 2, 3@})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1796 @result{} 1x3 struct array containing the fields:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1797 foo
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1798
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1799 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1800 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1801
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1802 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1803 The first case is an ordinary scalar struct---one field, one value. The
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1804 second produces an empty struct array with one field and no values, since
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1805 being passed an empty cell array of struct array values. When the value is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1806 a cell array containing a single entry, this becomes a scalar struct with
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1807 that single entry as the value of the field. That single entry happens
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1808 to be an empty cell array.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1809
32495
386d05bbae5a doc: Spellcheck documentation ahead of 9.1 release.
Rik <rik@octave.org>
parents: 32221
diff changeset
1810 Finally, if the value is a nonscalar cell array, then @code{struct}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1811 produces a struct @strong{array}.
30329
81d26e8481a6 maint: Shorten @seealso lines to less than 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30139
diff changeset
1812 @seealso{cell2struct, fieldnames, getfield, setfield, rmfield, isfield,
81d26e8481a6 maint: Shorten @seealso lines to less than 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30139
diff changeset
1813 orderfields, isstruct, structfun}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1814 @end deftypefn */)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1815 {
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1816 int nargin = args.length ();
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1817
5444
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1818 // struct ([]) returns an empty struct.
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1819
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1820 // struct (empty_matrix) returns an empty struct with the same
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1821 // dimensions as the empty matrix.
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1822
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1823 // Note that struct () creates a 1x1 struct with no fields for
781c9e7dbb73 [project @ 2005-09-08 02:55:21 by jwe]
jwe
parents: 5433
diff changeset
1824 // compatibility with Matlab.
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1825
23584
7ed6b258db91 maint: Deprecate is_map and replace with isstruct.
Rik <rik@octave.org>
parents: 23577
diff changeset
1826 if (nargin == 1 && args(0).isstruct ())
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21124
diff changeset
1827 return ovl (args(0));
10747
58c1b5402588 fix a showstopping bug in octave_fields reference counting
Jaroslav Hajek <highegg@gmail.com>
parents: 10742
diff changeset
1828
23587
0c468af9dc00 maint: Deprecate is_object and replace with isobject.
Rik <rik@octave.org>
parents: 23584
diff changeset
1829 if (nargin == 1 && args(0).isobject ())
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21124
diff changeset
1830 return ovl (args(0).map_value ());
9190
7a10410db2c6 [mq]: x
Robert T. Short <rtshort@ieee.org>
parents: 9087
diff changeset
1831
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1832 if ((nargin == 1 || nargin == 2)
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23576
diff changeset
1833 && args(0).isempty () && args(0).is_real_matrix ())
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1834 {
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1835 if (nargin == 2)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1836 {
31718
52a5bec0fc74 style fixes: avoid breaking lines after "(" and prefer "" over QString ("")
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
1837 Array<std::string> cstr = args(1).xcellstr_value ("struct: second argument should be a cell array of field names");
20681
b0b37f0d7e6d new cellstr_value function and elimination of error_state
John W. Eaton <jwe@octave.org>
parents: 20619
diff changeset
1838
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21124
diff changeset
1839 return ovl (octave_map (args(0).dims (), cstr));
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1840 }
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1841 else
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21124
diff changeset
1842 return ovl (octave_map (args(0).dims ()));
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1843 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1844
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1845 // Check for "field", VALUE pairs.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1846
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1847 for (int i = 0; i < nargin; i += 2)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1848 {
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1849 if (! args(i).is_string () || i + 1 >= nargin)
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23589
diff changeset
1850 error (R"(struct: additional arguments must occur as "field", VALUE pairs)");
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1851 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1852
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1853 // Check that the dimensions of the values correspond.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1854
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1855 dim_vector dims (1, 1);
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1856
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1857 int first_dimensioned_value = 0;
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1858
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1859 for (int i = 1; i < nargin; i += 2)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1860 {
23576
00e518162fda maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents: 23575
diff changeset
1861 if (args(i).iscell ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1862 {
32599
60c1b3ffd859 maint: Avoid dim_vector copies in a few more places (bug #64962)
Rik <rik@octave.org>
parents: 32598
diff changeset
1863 const dim_vector& argdims = args(i).dims ();
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1864
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1865 if (! scalar (argdims))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1866 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1867 if (! first_dimensioned_value)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1868 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1869 dims = argdims;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1870 first_dimensioned_value = i + 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1871 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1872 else if (dims != argdims)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1873 {
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21124
diff changeset
1874 error ("struct: dimensions of parameter %d "
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21124
diff changeset
1875 "do not match those of parameter %d",
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1876 first_dimensioned_value, i+1);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1877 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1878 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1879 }
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1880 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1881
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1882 // Create the return value.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1883
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1884 octave_map m_map (dims);
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1885
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1886 for (int i = 0; i < nargin; i+= 2)
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1887 {
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1888 // Get key.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1889
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1890 std::string key (args(i).string_value ());
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1891
15614
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1892 maybe_warn_invalid_field_name (key, "struct");
f2b8f90052fd warn instead of throwing an error for invalid structure field names
John W. Eaton <jwe@octave.org>
parents: 15613
diff changeset
1893
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1894 // Value may be v, { v }, or { v1, v2, ... }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1895 // In the first two cases, we need to create a cell array of
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1896 // the appropriate dimensions filled with v. In the last case,
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1897 // the cell array has already been determined to be of the
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1898 // correct dimensions.
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1899
23576
00e518162fda maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents: 23575
diff changeset
1900 if (args(i+1).iscell ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1901 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1902 const Cell c (args(i+1).cell_value ());
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1903
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1904 if (scalar (c.dims ()))
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1905 m_map.setfield (key, Cell (dims, c(0)));
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1906 else
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1907 m_map.setfield (key, c);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
1908 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1909 else
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1910 m_map.setfield (key, Cell (dims, args(i+1)));
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1911 }
6946
10c7a803b409 [project @ 2007-10-03 14:38:48 by jwe]
jwe
parents: 6833
diff changeset
1912
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
1913 return ovl (m_map);
4744
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1914 }
7dcb696159ac [project @ 2004-02-06 05:46:22 by jwe]
jwe
parents: 4696
diff changeset
1915
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1916 /*
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1917 %!shared x
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1918 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1919 %!assert (struct ("a",1, "b",3), x(1))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1920 %!assert (isempty (x([])))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1921 %!assert (isempty (struct ("a",{}, "b",{})))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1922 %!assert (struct ("a",{1,2}, "b",{3,3}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1923 %!assert (struct ("a",{1,2}, "b",3), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1924 %!assert (struct ("a",{1,2}, "b",{3}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1925 %!assert (struct ("b",3, "a",{1,2}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1926 %!assert (struct ("b",{3}, "a",{1,2}), x)
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1927 %!test x = struct ([]);
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1928 %!assert (size (x), [0,0])
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1929 %!assert (isstruct (x))
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1930 %!assert (isempty (fieldnames (x)))
30346
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30329
diff changeset
1931 %!fail ('struct ("a",{1,2},"b",{1,2,3})',
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30329
diff changeset
1932 %! 'dimensions of parameter 2 do not match those of parameter 4')
20711
7b608fadc663 Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents: 20704
diff changeset
1933 %!error <arguments must occur as "field", VALUE pairs> struct (1,2,3,4)
30346
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30329
diff changeset
1934 %!fail ('struct ("1",2,"3")',
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30329
diff changeset
1935 %! 'struct: additional arguments must occur as "field", VALUE pairs')
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1936 */
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
1937
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1938 DEFUN (isstruct, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1939 doc: /* -*- texinfo -*-
30559
841a10208c38 doc: Use TF for output variable in documentation for isXXX functions in libinterp/ directory.
Rik <rik@octave.org>
parents: 30391
diff changeset
1940 @deftypefn {} {@var{tf} =} isstruct (@var{x})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1941 Return true if @var{x} is a structure or a structure array.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1942 @seealso{ismatrix, iscell, isa}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1943 @end deftypefn */)
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1944 {
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
1945 if (args.length () != 1)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
1946 print_usage ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1947
23584
7ed6b258db91 maint: Deprecate is_map and replace with isstruct.
Rik <rik@octave.org>
parents: 23577
diff changeset
1948 return ovl (args(0).isstruct ());
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1949 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1950
15781
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1951 DEFUN (__fieldnames__, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1952 doc: /* -*- texinfo -*-
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
1953 @deftypefn {} {@var{names} =} __fieldnames__ (@var{struct})
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
1954 @deftypefnx {} {@var{names} =} __fieldnames__ (@var{obj})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1955 Internal function.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1956
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1957 Implements @code{fieldnames()} for structures and Octave objects.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1958 @seealso{fieldnames}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1959 @end deftypefn */)
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1960 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1961 octave_value retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1962
15781
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1963 // Input validation has already been done in fieldnames.m.
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1964 octave_value arg = args(0);
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1965
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1966 octave_map m = arg.map_value ();
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1967
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1968 string_vector keys = m.fieldnames ();
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1969
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23576
diff changeset
1970 if (keys.isempty ())
15781
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1971 retval = Cell (0, 1);
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1972 else
15781
c33594eefda7 Add fieldnames.m which extensds fieldnames() to work on Java objects.
Rik <rik@octave.org>
parents: 15731
diff changeset
1973 retval = Cell (keys);
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1974
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1975 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1976 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1977
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1978 DEFUN (isfield, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1979 doc: /* -*- texinfo -*-
30559
841a10208c38 doc: Use TF for output variable in documentation for isXXX functions in libinterp/ directory.
Rik <rik@octave.org>
parents: 30391
diff changeset
1980 @deftypefn {} {@var{tf} =} isfield (@var{x}, "@var{name}")
841a10208c38 doc: Use TF for output variable in documentation for isXXX functions in libinterp/ directory.
Rik <rik@octave.org>
parents: 30391
diff changeset
1981 @deftypefnx {} {@var{tf} =} isfield (@var{x}, @var{name})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1982 Return true if the @var{x} is a structure and it includes an element named
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1983 @var{name}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1984
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1985 If @var{name} is a cell array of strings then a logical array of equal
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1986 dimension is returned.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1987 @seealso{fieldnames}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
1988 @end deftypefn */)
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1989 {
20818
cef0448a6ed2 eliminate unnecessary uses of nargin
John W. Eaton <jwe@octave.org>
parents: 20807
diff changeset
1990 if (args.length () != 2)
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
1991 print_usage ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
1992
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20893
diff changeset
1993 octave_value retval = false;
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
1994
23584
7ed6b258db91 maint: Deprecate is_map and replace with isstruct.
Rik <rik@octave.org>
parents: 23577
diff changeset
1995 if (args(0).isstruct ())
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
1996 {
31260
c5c8bf50449c Improve performance of isfield from linear time to constant time (bug #58105)
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
1997 octave_value m = args(0);
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
1998
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
1999 // FIXME: should this work for all types that can do
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2000 // structure reference operations?
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2001 if (args(1).is_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2002 {
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2003 std::string key = args(1).string_value ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2004
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2005 retval = m.isfield (key);
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2006 }
23576
00e518162fda maint: Deprecate is_cell and replace with iscell.
Rik <rik@octave.org>
parents: 23575
diff changeset
2007 else if (args(1).iscell ())
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2008 {
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2009 Cell c = args(1).cell_value ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2010 boolNDArray bm (c.dims ());
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2011 octave_idx_type n = bm.numel ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2012
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2013 for (octave_idx_type i = 0; i < n; i++)
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2014 {
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2015 if (c(i).is_string ())
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2016 {
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2017 std::string key = c(i).string_value ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2018
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2019 bm(i) = m.isfield (key);
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2020 }
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2021 else
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2022 bm(i) = false;
10691
e0ba186b242b Fisfield: Accept cell arrays as name argument.
David Grundberg <davidg@cs.umu.se>
parents: 10370
diff changeset
2023 }
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2024
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2025 retval = bm;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10258
diff changeset
2026 }
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2027 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2028
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2029 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2030 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4247
diff changeset
2031
18538
fcd87f68af4f Deprecate nfields and replace with numfields.
Rik <rik@octave.org>
parents: 18537
diff changeset
2032 DEFUN (numfields, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2033 doc: /* -*- texinfo -*-
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
2034 @deftypefn {} {@var{n} =} numfields (@var{s})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2035 Return the number of fields of the structure @var{s}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2036 @seealso{fieldnames}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2037 @end deftypefn */)
9784
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2038 {
20818
cef0448a6ed2 eliminate unnecessary uses of nargin
John W. Eaton <jwe@octave.org>
parents: 20807
diff changeset
2039 if (args.length () != 1)
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2040 print_usage ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2041
23584
7ed6b258db91 maint: Deprecate is_map and replace with isstruct.
Rik <rik@octave.org>
parents: 23577
diff changeset
2042 if (! args(0).isstruct ())
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2043 error ("numfields: argument must be a struct");
9784
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2044
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20893
diff changeset
2045 return ovl (static_cast<double> (args(0).nfields ()));
9784
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2046 }
f786dca09f79 implement nfields
Jaroslav Hajek <highegg@gmail.com>
parents: 9529
diff changeset
2047
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2048 /*
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2049 ## test isfield
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2050 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2051 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3;
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2052 %! assert (isfield (x, "b"));
14861
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2053 %!assert (isfield (struct ("a", "1"), "a"))
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2054 %!assert (isfield ({1}, "c"), false)
14861
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2055 %!assert (isfield (struct ("a", "1"), 10), false)
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2056 %!assert (isfield (struct ("a", "b"), "a "), false)
f7afecdd87ef maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
2057 %!assert (isfield (struct ("a", 1, "b", 2), {"a", "c"}), [true, false])
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2058 */
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2059
28695
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2060 OCTAVE_NORETURN
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2061 static void
31771
21f9b34eb893 maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents: 31718
diff changeset
2062 invalid_cell2struct_fields_error ()
28695
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2063 {
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2064 error ("cell2struct: FIELDS must be a cell array of strings or a scalar string");
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2065 }
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2066
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2067 static Array<std::string>
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2068 get_cell2struct_fields (const octave_value& arg)
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2069 {
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2070 if (arg.is_string ())
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2071 {
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2072 if (arg.rows () != 1)
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2073 invalid_cell2struct_fields_error ();
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2074
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2075 return Array<std::string> (dim_vector (1, 1), arg.string_value ());
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2076 }
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2077
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2078 if (arg.iscell ())
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2079 {
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2080 const Cell c = arg.cell_value ();
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2081
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2082 Array<std::string> retval (c.dims ());
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2083
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2084 for (octave_idx_type i = 0; i < c.numel (); i++)
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2085 {
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2086 const octave_value val = c(i);
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2087
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2088 if (! val.is_string () || val.rows () != 1)
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2089 invalid_cell2struct_fields_error ();
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2090
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2091 retval(i) = c(i).string_value ();
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2092 }
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2093
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2094 return retval;
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2095 }
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2096
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2097 invalid_cell2struct_fields_error ();
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2098 }
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2099
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2100 DEFUN (cell2struct, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2101 doc: /* -*- texinfo -*-
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
2102 @deftypefn {} {@var{S} =} cell2struct (@var{cell}, @var{fields})
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
2103 @deftypefnx {} {@var{S} =} cell2struct (@var{cell}, @var{fields}, @var{dim})
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2104 Convert @var{cell} to a structure.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2105
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2106 The number of fields in @var{fields} must match the number of elements in
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2107 @var{cell} along dimension @var{dim}, that is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2108 @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}. If @var{dim}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2109 is omitted, a value of 1 is assumed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2110
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2111 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2112 @group
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
2113 S = cell2struct (@{"Peter", "Hannah", "Robert";
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2114 185, 170, 168@},
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2115 @{"Name","Height"@}, 1);
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30723
diff changeset
2116 S(1)
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2117 @result{}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2118 @{
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2119 Name = Peter
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2120 Height = 185
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2121 @}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2122
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2123 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2124 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2125 @seealso{struct2cell, cell2mat, struct}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2126 @end deftypefn */)
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2127 {
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2128 int nargin = args.length ();
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2129
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2130 if (nargin < 2 || nargin > 3)
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2131 print_usage ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2132
31718
52a5bec0fc74 style fixes: avoid breaking lines after "(" and prefer "" over QString ("")
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
2133 const Cell vals = args(0).xcell_value ("cell2struct: argument CELL must be of type cell");
28695
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2134
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2135 const Array<std::string> fields = get_cell2struct_fields (args(1));
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2136
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2137 int dim = 0;
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2138
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2139 if (nargin == 3)
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2140 {
20893
384ff5aa9437 2015 Code Sprint: Eliminate useless return statements after error ().
Rik <rik@octave.org>
parents: 20853
diff changeset
2141 if (! args(2).is_real_scalar ())
384ff5aa9437 2015 Code Sprint: Eliminate useless return statements after error ().
Rik <rik@octave.org>
parents: 20853
diff changeset
2142 error ("cell2struct: DIM must be a real scalar");
384ff5aa9437 2015 Code Sprint: Eliminate useless return statements after error ().
Rik <rik@octave.org>
parents: 20853
diff changeset
2143
26414
8e39c5a22bc2 ov-struct.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents: 26376
diff changeset
2144 dim = args(2).int_value () - 1;
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2145 }
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2146
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2147 if (dim < 0)
20893
384ff5aa9437 2015 Code Sprint: Eliminate useless return statements after error ().
Rik <rik@octave.org>
parents: 20853
diff changeset
2148 error ("cell2struct: DIM must be a valid dimension");
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2149
23450
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23354
diff changeset
2150 octave_idx_type ext = (vals.ndims () > dim ? vals.dims ()(dim) : 1);
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2151
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2152 if (ext != fields.numel ())
20893
384ff5aa9437 2015 Code Sprint: Eliminate useless return statements after error ().
Rik <rik@octave.org>
parents: 20853
diff changeset
2153 error ("cell2struct: number of FIELDS does not match dimension");
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2154
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2155 int nd = std::max (dim+1, vals.ndims ());
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2156 // result dimensions.
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2157 dim_vector rdv = vals.dims ().redim (nd);
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2158
30723
08b08b7f05b2 Replace direct calls to C library assert() with Octave specialty functions in libinterp/ (bug #61753)
Arun Giridhar <arungiridhar@gmail.com> and Rik <rik@octave.org>
parents: 30588
diff changeset
2159 panic_unless (ext == rdv(dim));
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2160 if (nd == 2)
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2161 {
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2162 rdv(0) = rdv(1-dim);
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2163 rdv(1) = 1;
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2164 }
4751
7cb3b220d0f8 [project @ 2004-02-07 22:42:04 by jwe]
jwe
parents: 4750
diff changeset
2165 else
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2166 {
21568
3d60ed163b70 maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents: 21547
diff changeset
2167 for (int i = dim + 1; i < nd; i++)
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2168 rdv(i-1) = rdv(i);
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2169
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2170 rdv.resize (nd-1);
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2171 }
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2172
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
2173 octave_map m_map (rdv);
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
2174 Array<idx_vector> ia (dim_vector (nd, 1), idx_vector::colon);
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2175
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2176 for (octave_idx_type i = 0; i < ext; i++)
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2177 {
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2178 ia(dim) = i;
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
2179 m_map.setfield (fields(i), vals.index (ia).reshape (rdv));
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2180 }
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2181
30077
b16e0d357437 maint: use "m_" prefix for member variables in some octave-value classes.
John W. Eaton <jwe@octave.org>
parents: 30001
diff changeset
2182 return ovl (m_map);
4750
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2183 }
95661d5713ce [project @ 2004-02-07 22:30:03 by jwe]
jwe
parents: 4744
diff changeset
2184
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2185 /*
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2186 ## test cell2struct versus struct2cell
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2187 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2188 %! keys = cellstr (char (floor (rand (100,10)*24+65)))';
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2189 %! vals = mat2cell (rand (100,1), ones (100,1), 1)';
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2190 %! s = struct ([keys; vals]{:});
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2191 %! t = cell2struct (vals, keys, 2);
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2192 %! assert (s, t);
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2193 %! assert (struct2cell (s), vals');
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2194 %! assert (fieldnames (s), keys');
13868
87f78c11d725 cell2struct: if DIM is omitted, use a default value of 1.
John W. Eaton <jwe@octave.org>
parents: 13706
diff changeset
2195
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
2196 %!assert (cell2struct ({1; 2}, {"a"; "b"}), struct ("a", 1, "b", 2))
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
2197
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
2198 %!assert (cell2struct ({}, {"f"}, 3), struct ("f", {}))
28695
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2199
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2200 %!assert (cell2struct ({1; 2; 3; 4}, {'a', 'b'; 'c', 'd'}),
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2201 %! struct ('a', 1, 'c', 2, 'b', 3, 'd', 4));
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2202 %!assert (cell2struct ({1, 2, 3, 4}, {'a', 'b'; 'c', 'd'}, 2),
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2203 %! struct ('a', 1, 'c', 2, 'b', 3, 'd', 4));
b3770a5f210d improve validation of cell2struct fields argument (bug #58617)
John W. Eaton <jwe@octave.org>
parents: 28548
diff changeset
2204 %!error cell2struct ({1, 2, 3, 4}, {'a', 'b'; 'c', 'd'})
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2205 */
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2206
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2207 DEFUN (rmfield, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2208 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2209 @deftypefn {} {@var{sout} =} rmfield (@var{s}, "@var{f}")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2210 @deftypefnx {} {@var{sout} =} rmfield (@var{s}, @var{f})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2211 Return a @emph{copy} of the structure (array) @var{s} with the field @var{f}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2212 removed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2213
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2214 If @var{f} is a cell array of strings or a character array, remove each of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2215 the named fields.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2216 @seealso{orderfields, fieldnames, isfield}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2217 @end deftypefn */)
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2218 {
20818
cef0448a6ed2 eliminate unnecessary uses of nargin
John W. Eaton <jwe@octave.org>
parents: 20807
diff changeset
2219 if (args.length () != 2)
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2220 print_usage ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2221
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2222 octave_map m = args(0).xmap_value ("rmfield: first argument must be a struct");
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2223
31813
73fdf7f8381b maint: Use consistent coding style when calling Ffunction().
Rik <rik@octave.org>
parents: 31771
diff changeset
2224 octave_value_list fval = Fcellstr (ovl (args(1)), 1);
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2225
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2226 Cell fcell = fval(0).cell_value ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2227
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2228 for (int i = 0; i < fcell.numel (); i++)
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2229 {
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2230 std::string key = fcell(i).string_value ();
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2231
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
2232 if (! m.isfield (key))
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2233 error ("rmfield: structure does not contain field %s", key.c_str ());
20979
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
2234
0963ed389012 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20974
diff changeset
2235 m.rmfield (key);
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2236 }
20797
492738d32c60 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2237
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20893
diff changeset
2238 return ovl (m);
4817
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2239 }
6430596f2238 [project @ 2004-03-04 21:13:33 by jwe]
jwe
parents: 4792
diff changeset
2240
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2241 /*
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2242 ## test rmfield
15534
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2243 %!shared x
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2244 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; x(6).f="abc123";
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2245 %!
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2246 %!test
15534
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2247 %! y = rmfield (x, "c");
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2248 %! assert (fieldnames (y), {"d"; "a"; "b"; "f"});
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2249 %! assert (size (y), [1, 6]);
360adb4a3136 doc: Update docstring form rmfield() and add another %!test.
Rik <rik@octave.org>
parents: 15467
diff changeset
2250 %!test
14429
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2251 %! y = rmfield (x, {"a", "f"});
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2252 %! assert (fieldnames (y), {"d"; "b"; "c"});
eff4a5933e28 Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
2253 %! assert (size (y), [1, 6]);
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2254 */
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10066
diff changeset
2255
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2256 DEFUN (struct_levels_to_print, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2257 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2258 @deftypefn {} {@var{val} =} struct_levels_to_print ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2259 @deftypefnx {} {@var{old_val} =} struct_levels_to_print (@var{new_val})
30588
ed17822e7662 doc: Always have a return value for functions which set internal variables (bug #61681)
Rik <rik@octave.org>
parents: 30565
diff changeset
2260 @deftypefnx {} {@var{old_val} =} struct_levels_to_print (@var{new_val}, "local")
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2261 Query or set the internal variable that specifies the number of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2262 structure levels to display.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2263
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2264 When called from inside a function with the @qcode{"local"} option, the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2265 variable is changed locally for the function and any subroutines it calls.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2266 The original variable value is restored when exiting the function.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2267 @seealso{print_struct_array_contents}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2268 @end deftypefn */)
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2269 {
30001
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
2270 return set_internal_variable (Vstruct_levels_to_print, args, nargout,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
2271 "struct_levels_to_print", -1,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
2272 std::numeric_limits<int>::max ());
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2273 }
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2274
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2275 DEFUN (print_struct_array_contents, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2276 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2277 @deftypefn {} {@var{val} =} print_struct_array_contents ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2278 @deftypefnx {} {@var{old_val} =} print_struct_array_contents (@var{new_val})
30588
ed17822e7662 doc: Always have a return value for functions which set internal variables (bug #61681)
Rik <rik@octave.org>
parents: 30565
diff changeset
2279 @deftypefnx {} {@var{old_val} =} print_struct_array_contents (@var{new_val}, "local")
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2280 Query or set the internal variable that specifies whether to print struct
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2281 array contents.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2282
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2283 If true, values of struct array elements are printed. This variable does
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2284 not affect scalar structures whose elements are always printed. In both
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2285 cases, however, printing will be limited to the number of levels specified
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2286 by @var{struct_levels_to_print}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2287
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2288 When called from inside a function with the @qcode{"local"} option, the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2289 variable is changed locally for the function and any subroutines it calls.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2290 The original variable value is restored when exiting the function.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2291 @seealso{struct_levels_to_print}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21943
diff changeset
2292 @end deftypefn */)
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2293 {
30001
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
2294 return set_internal_variable (Vprint_struct_array_contents, args, nargout,
428cccc75cc9 move most functions in variables.cc to octave namespace
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
2295 "print_struct_array_contents");
11474
8a40037533e2 struct printing changes
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
2296 }
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29703
diff changeset
2297
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 31260
diff changeset
2298 OCTAVE_END_NAMESPACE(octave)