2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2376
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
2376
|
32 |
3933
|
33 #include "Cell.h" |
4358
|
34 #include "defun.h" |
2376
|
35 #include "error.h" |
4358
|
36 #include "gripes.h" |
2979
|
37 #include "oct-lvalue.h" |
3932
|
38 #include "ov-list.h" |
2376
|
39 #include "ov-struct.h" |
|
40 #include "unwind-prot.h" |
2948
|
41 #include "variables.h" |
2376
|
42 |
3219
|
43 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
2376
|
44 |
3219
|
45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct"); |
2376
|
46 |
4513
|
47 Cell |
3933
|
48 octave_struct::dotref (const octave_value_list& idx) |
2962
|
49 { |
4513
|
50 Cell retval; |
3933
|
51 |
|
52 assert (idx.length () == 1); |
2962
|
53 |
3933
|
54 std::string nm = idx(0).string_value (); |
|
55 |
4219
|
56 Octave_map::const_iterator p = map.seek (nm); |
2376
|
57 |
4219
|
58 if (p != map.end ()) |
3933
|
59 retval = map.contents (p); |
|
60 else |
2376
|
61 error ("structure has no member `%s'", nm.c_str ()); |
|
62 |
|
63 return retval; |
|
64 } |
|
65 |
4513
|
66 #if 0 |
3933
|
67 static void |
|
68 gripe_invalid_index (void) |
|
69 { |
|
70 error ("invalid index for structure array"); |
|
71 } |
4513
|
72 #endif |
3933
|
73 |
|
74 static void |
|
75 gripe_invalid_index_for_assignment (void) |
|
76 { |
|
77 error ("invalid index for structure array assignment"); |
|
78 } |
|
79 |
|
80 static void |
|
81 gripe_invalid_index_type (const std::string& nm, char t) |
|
82 { |
|
83 error ("%s cannot be indexed with %c", nm.c_str (), t); |
|
84 } |
|
85 |
|
86 static void |
|
87 gripe_failed_assignment (void) |
|
88 { |
|
89 error ("assignment to structure element failed"); |
|
90 } |
|
91 |
|
92 octave_value |
4247
|
93 octave_struct::subsref (const std::string& type, |
4219
|
94 const std::list<octave_value_list>& idx) |
3933
|
95 { |
|
96 octave_value retval; |
|
97 |
|
98 int skip = 1; |
|
99 |
|
100 switch (type[0]) |
|
101 { |
|
102 case '(': |
|
103 { |
|
104 if (type.length () > 1 && type[1] == '.') |
|
105 { |
4219
|
106 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
107 octave_value_list key_idx = *++p; |
3933
|
108 |
4513
|
109 Cell tmp = dotref (key_idx); |
3933
|
110 |
|
111 if (! error_state) |
|
112 { |
4513
|
113 Cell t = tmp.index (idx.front ()); |
3933
|
114 |
4513
|
115 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933
|
116 |
4513
|
117 // We handled two index elements, so tell |
|
118 // next_subsref to skip both of them. |
3933
|
119 |
4513
|
120 skip++; |
3933
|
121 } |
|
122 } |
|
123 else |
4513
|
124 retval = map.index (idx.front ()); |
3933
|
125 } |
|
126 break; |
|
127 |
|
128 case '.': |
|
129 { |
4513
|
130 Cell t = dotref (idx.front ()); |
3933
|
131 |
4499
|
132 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933
|
133 } |
|
134 break; |
|
135 |
|
136 case '{': |
|
137 gripe_invalid_index_type (type_name (), type[0]); |
|
138 break; |
|
139 |
|
140 default: |
|
141 panic_impossible (); |
|
142 } |
|
143 |
|
144 if (! error_state) |
|
145 retval = retval.next_subsref (type, idx, skip); |
|
146 |
|
147 return retval; |
|
148 } |
|
149 |
|
150 octave_value |
4513
|
151 octave_struct::numeric_conv (const Cell& val, |
3933
|
152 const std::string& type) |
|
153 { |
|
154 octave_value retval; |
|
155 |
|
156 if (val.length () == 1) |
|
157 { |
|
158 retval = val(0); |
|
159 |
|
160 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) |
|
161 retval = Octave_map (); |
|
162 } |
|
163 else |
|
164 gripe_invalid_index_for_assignment (); |
|
165 |
|
166 return retval; |
|
167 } |
|
168 |
|
169 octave_value |
4247
|
170 octave_struct::subsasgn (const std::string& type, |
4219
|
171 const std::list<octave_value_list>& idx, |
3933
|
172 const octave_value& rhs) |
2376
|
173 { |
3933
|
174 octave_value retval; |
|
175 |
|
176 int n = type.length (); |
|
177 |
|
178 octave_value t_rhs = rhs; |
|
179 |
|
180 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
|
181 { |
|
182 switch (type[0]) |
|
183 { |
|
184 case '(': |
|
185 { |
|
186 if (type.length () > 1 && type[1] == '.') |
|
187 { |
4219
|
188 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
189 octave_value_list t_idx = *p; |
3933
|
190 |
4513
|
191 octave_value_list key_idx = *++p; |
|
192 |
|
193 assert (key_idx.length () == 1); |
3933
|
194 |
4513
|
195 std::string key = key_idx(0).string_value (); |
3933
|
196 |
4513
|
197 octave_value u; |
3933
|
198 |
4513
|
199 if (! map.contains (key)) |
|
200 u = octave_value::empty_conv (type.substr (2), rhs); |
|
201 else |
|
202 { |
|
203 Cell map_val = map[key]; |
3933
|
204 |
4513
|
205 Cell map_elt = map_val.index (idx.front (), true); |
3933
|
206 |
4513
|
207 u = numeric_conv (map_elt, type.substr (2)); |
|
208 } |
3933
|
209 |
4513
|
210 if (! error_state) |
|
211 { |
|
212 std::list<octave_value_list> next_idx (idx); |
3933
|
213 |
4513
|
214 // We handled two index elements, so subsasgn to |
|
215 // needs to skip both of them. |
3933
|
216 |
4513
|
217 next_idx.erase (next_idx.begin ()); |
|
218 next_idx.erase (next_idx.begin ()); |
4059
|
219 |
4513
|
220 u.make_unique (); |
|
221 |
|
222 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); |
3933
|
223 } |
|
224 } |
|
225 else |
|
226 gripe_invalid_index_for_assignment (); |
|
227 } |
|
228 break; |
|
229 |
|
230 case '.': |
|
231 { |
|
232 octave_value_list key_idx = idx.front (); |
|
233 |
|
234 assert (key_idx.length () == 1); |
|
235 |
|
236 std::string key = key_idx(0).string_value (); |
|
237 |
|
238 octave_value u; |
|
239 |
|
240 if (! map.contains (key)) |
|
241 u = octave_value::empty_conv (type.substr (1), rhs); |
|
242 else |
|
243 { |
4513
|
244 Cell map_val = map[key]; |
3933
|
245 |
|
246 u = numeric_conv (map_val, type.substr (1)); |
|
247 } |
|
248 |
|
249 if (! error_state) |
|
250 { |
4219
|
251 std::list<octave_value_list> next_idx (idx); |
3933
|
252 |
4219
|
253 next_idx.erase (next_idx.begin ()); |
3933
|
254 |
4059
|
255 u.make_unique (); |
|
256 |
3933
|
257 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); |
|
258 } |
|
259 } |
|
260 break; |
|
261 |
|
262 case '{': |
|
263 gripe_invalid_index_type (type_name (), type[0]); |
|
264 break; |
|
265 |
|
266 default: |
|
267 panic_impossible (); |
|
268 } |
|
269 } |
|
270 |
|
271 if (! error_state) |
|
272 { |
|
273 switch (type[0]) |
|
274 { |
|
275 case '(': |
|
276 { |
|
277 if (n > 1 && type[1] == '.') |
|
278 { |
4219
|
279 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
280 octave_value_list key_idx = *++p; |
3933
|
281 |
|
282 assert (key_idx.length () == 1); |
|
283 |
|
284 std::string key = key_idx(0).string_value (); |
|
285 |
|
286 if (! error_state) |
|
287 { |
4513
|
288 map.assign (idx.front (), key, t_rhs); |
3933
|
289 |
4513
|
290 if (! error_state) |
|
291 retval = octave_value (this, count + 1); |
3933
|
292 else |
4513
|
293 gripe_failed_assignment (); |
3933
|
294 } |
|
295 else |
|
296 gripe_failed_assignment (); |
|
297 } |
|
298 else |
4197
|
299 { |
4513
|
300 Octave_map rhs_map = t_rhs.map_value (); |
4197
|
301 |
4513
|
302 if (! error_state) |
4197
|
303 { |
4513
|
304 map.assign (idx.front (), rhs_map); |
4197
|
305 |
|
306 if (! error_state) |
4513
|
307 retval = octave_value (this, count + 1); |
4197
|
308 else |
4513
|
309 gripe_failed_assignment (); |
4197
|
310 } |
4513
|
311 else |
|
312 error ("invalid structure assignment"); |
4197
|
313 } |
3933
|
314 } |
|
315 break; |
|
316 |
|
317 case '.': |
|
318 { |
|
319 octave_value_list key_idx = idx.front (); |
|
320 |
|
321 assert (key_idx.length () == 1); |
|
322 |
|
323 std::string key = key_idx(0).string_value (); |
|
324 |
|
325 map.assign (key, t_rhs); |
|
326 |
|
327 if (! error_state) |
|
328 retval = octave_value (this, count + 1); |
|
329 else |
|
330 gripe_failed_assignment (); |
|
331 } |
|
332 break; |
|
333 |
|
334 case '{': |
|
335 gripe_invalid_index_type (type_name (), type[0]); |
|
336 break; |
|
337 |
|
338 default: |
|
339 panic_impossible (); |
|
340 } |
|
341 } |
|
342 else |
|
343 gripe_failed_assignment (); |
|
344 |
|
345 return retval; |
2376
|
346 } |
|
347 |
|
348 void |
3523
|
349 octave_struct::print (std::ostream& os, bool) const |
2901
|
350 { |
|
351 print_raw (os); |
|
352 } |
|
353 |
|
354 void |
3523
|
355 octave_struct::print_raw (std::ostream& os, bool) const |
2376
|
356 { |
|
357 // XXX FIXME XXX -- would be nice to print the output in some |
|
358 // standard order. Maybe all substructures first, maybe |
|
359 // alphabetize entries, etc. |
|
360 |
2985
|
361 unwind_protect::begin_frame ("octave_struct_print"); |
2376
|
362 |
|
363 unwind_protect_int (Vstruct_levels_to_print); |
|
364 |
3961
|
365 if (Vstruct_levels_to_print >= 0) |
2376
|
366 { |
3961
|
367 bool print_keys_only = (Vstruct_levels_to_print == 0); |
|
368 |
|
369 Vstruct_levels_to_print--; |
|
370 |
2901
|
371 indent (os); |
|
372 os << "{"; |
|
373 newline (os); |
2376
|
374 |
2901
|
375 increment_indent_level (); |
2376
|
376 |
3932
|
377 int n = map.array_length (); |
|
378 |
4219
|
379 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) |
2376
|
380 { |
3523
|
381 std::string key = map.key (p); |
4513
|
382 Cell val = map.contents (p); |
2376
|
383 |
4499
|
384 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
3961
|
385 |
|
386 if (print_keys_only) |
3932
|
387 { |
3961
|
388 indent (os); |
|
389 os << key << ": " << tmp.type_name (); |
|
390 newline (os); |
3932
|
391 } |
3961
|
392 else |
4121
|
393 tmp.print_with_name (os, key); |
2376
|
394 } |
|
395 |
2901
|
396 decrement_indent_level (); |
2376
|
397 |
2901
|
398 indent (os); |
|
399 os << "}"; |
|
400 newline (os); |
2376
|
401 } |
|
402 else |
2901
|
403 { |
3961
|
404 indent (os); |
|
405 os << "<structure>"; |
2901
|
406 newline (os); |
|
407 } |
2376
|
408 |
2985
|
409 unwind_protect::run_frame ("octave_struct_print"); |
2376
|
410 } |
|
411 |
2901
|
412 bool |
3523
|
413 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
414 { |
3961
|
415 bool retval = false; |
|
416 |
2901
|
417 indent (os); |
3961
|
418 |
|
419 if (Vstruct_levels_to_print < 0) |
|
420 os << name << " = "; |
|
421 else |
|
422 { |
|
423 os << name << " ="; |
|
424 newline (os); |
|
425 retval = true; |
|
426 } |
|
427 |
|
428 return retval; |
2901
|
429 } |
|
430 |
4358
|
431 DEFUN (isstruct, args, , |
|
432 "-*- texinfo -*-\n\ |
|
433 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ |
|
434 Return 1 if the value of the expression @var{expr} is a structure.\n\ |
|
435 @end deftypefn") |
|
436 { |
|
437 octave_value retval; |
|
438 |
|
439 if (args.length () == 1) |
|
440 retval = args(0).is_map (); |
|
441 else |
|
442 print_usage ("isstruct"); |
|
443 |
|
444 return retval; |
|
445 } |
|
446 |
|
447 DEFUN (fieldnames, args, , |
|
448 "-*- texinfo -*-\n\ |
|
449 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ |
|
450 Return a cell array of strings naming the elements of the structure\n\ |
|
451 @var{struct}. It is an error to call @code{fieldnames} with an\n\ |
|
452 argument that is not a structure.\n\ |
|
453 @end deftypefn") |
|
454 { |
|
455 octave_value retval; |
|
456 |
|
457 int nargin = args.length (); |
|
458 |
|
459 if (nargin == 1) |
|
460 { |
|
461 if (args(0).is_map ()) |
|
462 { |
|
463 Octave_map m = args(0).map_value (); |
|
464 retval = Cell (m.keys ()); |
|
465 } |
|
466 else |
|
467 gripe_wrong_type_arg ("fieldnames", args(0)); |
|
468 } |
|
469 else |
|
470 print_usage ("fieldnames"); |
|
471 |
|
472 return retval; |
|
473 } |
|
474 |
|
475 DEFUN (isfield, args, , |
|
476 "-*- texinfo -*-\n\ |
|
477 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ |
|
478 Return true if the expression @var{expr} is a structure and it includes an\n\ |
|
479 element named @var{name}. The first argument must be a structure and\n\ |
|
480 the second must be a string.\n\ |
|
481 @end deftypefn") |
|
482 { |
|
483 octave_value retval; |
|
484 |
|
485 int nargin = args.length (); |
|
486 |
|
487 if (nargin == 2) |
|
488 { |
|
489 retval = false; |
|
490 |
|
491 // XXX FIXME XXX -- should this work for all types that can do |
|
492 // structure reference operations? |
|
493 |
|
494 if (args(0).is_map () && args(1).is_string ()) |
|
495 { |
|
496 std::string key = args(1).string_value (); |
|
497 |
|
498 Octave_map m = args(0).map_value (); |
|
499 |
|
500 retval = m.contains (key) != 0; |
|
501 } |
|
502 else |
|
503 print_usage ("isfield"); |
|
504 } |
|
505 else |
|
506 print_usage ("isfield"); |
|
507 |
|
508 return retval; |
|
509 } |
|
510 |
2376
|
511 /* |
|
512 ;;; Local Variables: *** |
|
513 ;;; mode: C++ *** |
|
514 ;;; End: *** |
|
515 */ |