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 |
3933
|
47 octave_value_list |
|
48 octave_struct::dotref (const octave_value_list& idx) |
2962
|
49 { |
3933
|
50 octave_value_list retval; |
|
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 |
3933
|
66 static void |
|
67 gripe_invalid_index (void) |
|
68 { |
|
69 error ("invalid index for structure array"); |
|
70 } |
|
71 |
|
72 static void |
|
73 gripe_invalid_index_for_assignment (void) |
|
74 { |
|
75 error ("invalid index for structure array assignment"); |
|
76 } |
|
77 |
|
78 static void |
|
79 gripe_invalid_index_type (const std::string& nm, char t) |
|
80 { |
|
81 error ("%s cannot be indexed with %c", nm.c_str (), t); |
|
82 } |
|
83 |
|
84 static void |
|
85 gripe_failed_assignment (void) |
|
86 { |
|
87 error ("assignment to structure element failed"); |
|
88 } |
|
89 |
|
90 octave_value |
4247
|
91 octave_struct::subsref (const std::string& type, |
4219
|
92 const std::list<octave_value_list>& idx) |
3933
|
93 { |
|
94 octave_value retval; |
|
95 |
|
96 int skip = 1; |
|
97 |
|
98 switch (type[0]) |
|
99 { |
|
100 case '(': |
|
101 { |
|
102 if (type.length () > 1 && type[1] == '.') |
|
103 { |
4219
|
104 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
105 octave_value_list key_idx = *++p; |
3933
|
106 |
|
107 octave_value_list tmp = dotref (key_idx); |
|
108 |
|
109 if (! error_state) |
|
110 { |
|
111 octave_value_list t_idx = idx.front (); |
|
112 |
|
113 if (t_idx.length () == 1) |
|
114 { |
|
115 idx_vector i = t_idx(0).index_vector (); |
|
116 octave_value_list t = tmp.index (i); |
|
117 |
|
118 retval = (t.length () == 1) ? t(0) : octave_value (t); |
|
119 |
|
120 // We handled two index elements, so tell |
|
121 // next_subsref to skip both of them. |
|
122 |
|
123 skip++; |
|
124 } |
|
125 else |
|
126 gripe_invalid_index (); |
|
127 } |
|
128 } |
|
129 else |
|
130 { |
|
131 octave_value_list t_idx = idx.front (); |
|
132 |
|
133 if (t_idx.length () == 1) |
|
134 { |
|
135 idx_vector i = t_idx(0).index_vector (); |
|
136 retval = map.index (i); |
|
137 } |
|
138 else |
|
139 gripe_invalid_index (); |
|
140 } |
|
141 } |
|
142 break; |
|
143 |
|
144 case '.': |
|
145 { |
|
146 octave_value_list t = dotref (idx.front ()); |
|
147 |
|
148 retval = (t.length () == 1) ? t(0) : octave_value (t); |
|
149 } |
|
150 break; |
|
151 |
|
152 case '{': |
|
153 gripe_invalid_index_type (type_name (), type[0]); |
|
154 break; |
|
155 |
|
156 default: |
|
157 panic_impossible (); |
|
158 } |
|
159 |
|
160 if (! error_state) |
|
161 retval = retval.next_subsref (type, idx, skip); |
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
|
166 octave_value |
|
167 octave_struct::numeric_conv (const octave_value_list& val, |
|
168 const std::string& type) |
|
169 { |
|
170 octave_value retval; |
|
171 |
|
172 if (val.length () == 1) |
|
173 { |
|
174 retval = val(0); |
|
175 |
|
176 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) |
|
177 retval = Octave_map (); |
|
178 } |
|
179 else |
|
180 gripe_invalid_index_for_assignment (); |
|
181 |
|
182 return retval; |
|
183 } |
|
184 |
|
185 octave_value |
4247
|
186 octave_struct::subsasgn (const std::string& type, |
4219
|
187 const std::list<octave_value_list>& idx, |
3933
|
188 const octave_value& rhs) |
2376
|
189 { |
3933
|
190 octave_value retval; |
|
191 |
|
192 int n = type.length (); |
|
193 |
|
194 octave_value t_rhs = rhs; |
|
195 |
|
196 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
|
197 { |
|
198 switch (type[0]) |
|
199 { |
|
200 case '(': |
|
201 { |
|
202 if (type.length () > 1 && type[1] == '.') |
|
203 { |
4219
|
204 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
205 octave_value_list t_idx = *p; |
3933
|
206 |
|
207 if (t_idx.length () == 1) |
|
208 { |
4219
|
209 octave_value_list key_idx = *++p; |
3933
|
210 |
|
211 assert (key_idx.length () == 1); |
|
212 |
|
213 std::string key = key_idx(0).string_value (); |
|
214 |
|
215 octave_value u; |
|
216 |
|
217 if (! map.contains (key)) |
|
218 u = octave_value::empty_conv (type.substr (2), rhs); |
|
219 else |
|
220 { |
|
221 octave_value_list map_val = map[key]; |
|
222 |
|
223 octave_value_list t_idx = idx.front (); |
|
224 |
|
225 idx_vector i = t_idx(0).index_vector (); |
|
226 |
|
227 octave_value_list map_elt = map_val.index (i, true); |
|
228 |
|
229 u = numeric_conv (map_elt, type.substr (2)); |
|
230 } |
|
231 |
|
232 if (! error_state) |
|
233 { |
4219
|
234 std::list<octave_value_list> next_idx (idx); |
3933
|
235 |
|
236 // We handled two index elements, so subsasgn to |
|
237 // needs to skip both of them. |
|
238 |
4219
|
239 next_idx.erase (next_idx.begin ()); |
|
240 next_idx.erase (next_idx.begin ()); |
3933
|
241 |
4059
|
242 u.make_unique (); |
|
243 |
3933
|
244 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); |
|
245 } |
|
246 } |
|
247 else |
|
248 gripe_invalid_index_for_assignment (); |
|
249 } |
|
250 else |
|
251 gripe_invalid_index_for_assignment (); |
|
252 } |
|
253 break; |
|
254 |
|
255 case '.': |
|
256 { |
|
257 octave_value_list key_idx = idx.front (); |
|
258 |
|
259 assert (key_idx.length () == 1); |
|
260 |
|
261 std::string key = key_idx(0).string_value (); |
|
262 |
|
263 octave_value u; |
|
264 |
|
265 if (! map.contains (key)) |
|
266 u = octave_value::empty_conv (type.substr (1), rhs); |
|
267 else |
|
268 { |
|
269 octave_value_list map_val = map[key]; |
|
270 |
|
271 u = numeric_conv (map_val, type.substr (1)); |
|
272 } |
|
273 |
|
274 if (! error_state) |
|
275 { |
4219
|
276 std::list<octave_value_list> next_idx (idx); |
3933
|
277 |
4219
|
278 next_idx.erase (next_idx.begin ()); |
3933
|
279 |
4059
|
280 u.make_unique (); |
|
281 |
3933
|
282 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); |
|
283 } |
|
284 } |
|
285 break; |
|
286 |
|
287 case '{': |
|
288 gripe_invalid_index_type (type_name (), type[0]); |
|
289 break; |
|
290 |
|
291 default: |
|
292 panic_impossible (); |
|
293 } |
|
294 } |
|
295 |
|
296 if (! error_state) |
|
297 { |
|
298 switch (type[0]) |
|
299 { |
|
300 case '(': |
|
301 { |
|
302 if (n > 1 && type[1] == '.') |
|
303 { |
4219
|
304 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
305 octave_value_list key_idx = *++p; |
3933
|
306 |
|
307 assert (key_idx.length () == 1); |
|
308 |
|
309 std::string key = key_idx(0).string_value (); |
|
310 |
|
311 if (! error_state) |
|
312 { |
|
313 octave_value_list t_idx = idx.front (); |
|
314 |
|
315 if (t_idx.length () == 1) |
|
316 { |
|
317 idx_vector i = t_idx(0).index_vector (); |
|
318 |
|
319 map.assign (i, key, t_rhs); |
|
320 |
|
321 if (! error_state) |
|
322 retval = octave_value (this, count + 1); |
|
323 else |
|
324 gripe_failed_assignment (); |
|
325 } |
|
326 else |
|
327 gripe_invalid_index_for_assignment (); |
|
328 } |
|
329 else |
|
330 gripe_failed_assignment (); |
|
331 } |
|
332 else |
4197
|
333 { |
|
334 octave_value_list t_idx = idx.front (); |
|
335 |
|
336 if (t_idx.length () == 1) |
|
337 { |
|
338 idx_vector i = t_idx(0).index_vector (); |
|
339 |
|
340 Octave_map rhs_map = t_rhs.map_value (); |
|
341 |
|
342 if (! error_state) |
|
343 { |
|
344 map.assign (i, rhs_map); |
|
345 |
|
346 if (! error_state) |
|
347 retval = octave_value (this, count + 1); |
|
348 else |
|
349 gripe_failed_assignment (); |
|
350 } |
|
351 else |
|
352 error ("invalid structure assignment"); |
|
353 } |
|
354 } |
3933
|
355 } |
|
356 break; |
|
357 |
|
358 case '.': |
|
359 { |
|
360 octave_value_list key_idx = idx.front (); |
|
361 |
|
362 assert (key_idx.length () == 1); |
|
363 |
|
364 std::string key = key_idx(0).string_value (); |
|
365 |
|
366 map.assign (key, t_rhs); |
|
367 |
|
368 if (! error_state) |
|
369 retval = octave_value (this, count + 1); |
|
370 else |
|
371 gripe_failed_assignment (); |
|
372 } |
|
373 break; |
|
374 |
|
375 case '{': |
|
376 gripe_invalid_index_type (type_name (), type[0]); |
|
377 break; |
|
378 |
|
379 default: |
|
380 panic_impossible (); |
|
381 } |
|
382 } |
|
383 else |
|
384 gripe_failed_assignment (); |
|
385 |
|
386 return retval; |
2376
|
387 } |
|
388 |
|
389 void |
3523
|
390 octave_struct::print (std::ostream& os, bool) const |
2901
|
391 { |
|
392 print_raw (os); |
|
393 } |
|
394 |
|
395 void |
3523
|
396 octave_struct::print_raw (std::ostream& os, bool) const |
2376
|
397 { |
|
398 // XXX FIXME XXX -- would be nice to print the output in some |
|
399 // standard order. Maybe all substructures first, maybe |
|
400 // alphabetize entries, etc. |
|
401 |
2985
|
402 unwind_protect::begin_frame ("octave_struct_print"); |
2376
|
403 |
|
404 unwind_protect_int (Vstruct_levels_to_print); |
|
405 |
3961
|
406 if (Vstruct_levels_to_print >= 0) |
2376
|
407 { |
3961
|
408 bool print_keys_only = (Vstruct_levels_to_print == 0); |
|
409 |
|
410 Vstruct_levels_to_print--; |
|
411 |
2901
|
412 indent (os); |
|
413 os << "{"; |
|
414 newline (os); |
2376
|
415 |
2901
|
416 increment_indent_level (); |
2376
|
417 |
3932
|
418 int n = map.array_length (); |
|
419 |
4219
|
420 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) |
2376
|
421 { |
3523
|
422 std::string key = map.key (p); |
3932
|
423 octave_value_list val = map.contents (p); |
2376
|
424 |
4121
|
425 octave_value tmp = (n == 1) ? val(0) : octave_value (val); |
3961
|
426 |
|
427 if (print_keys_only) |
3932
|
428 { |
3961
|
429 indent (os); |
|
430 os << key << ": " << tmp.type_name (); |
|
431 newline (os); |
3932
|
432 } |
3961
|
433 else |
4121
|
434 tmp.print_with_name (os, key); |
2376
|
435 } |
|
436 |
2901
|
437 decrement_indent_level (); |
2376
|
438 |
2901
|
439 indent (os); |
|
440 os << "}"; |
|
441 newline (os); |
2376
|
442 } |
|
443 else |
2901
|
444 { |
3961
|
445 indent (os); |
|
446 os << "<structure>"; |
2901
|
447 newline (os); |
|
448 } |
2376
|
449 |
2985
|
450 unwind_protect::run_frame ("octave_struct_print"); |
2376
|
451 } |
|
452 |
2901
|
453 bool |
3523
|
454 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
455 { |
3961
|
456 bool retval = false; |
|
457 |
2901
|
458 indent (os); |
3961
|
459 |
|
460 if (Vstruct_levels_to_print < 0) |
|
461 os << name << " = "; |
|
462 else |
|
463 { |
|
464 os << name << " ="; |
|
465 newline (os); |
|
466 retval = true; |
|
467 } |
|
468 |
|
469 return retval; |
2901
|
470 } |
|
471 |
4358
|
472 DEFUN (isstruct, args, , |
|
473 "-*- texinfo -*-\n\ |
|
474 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ |
|
475 Return 1 if the value of the expression @var{expr} is a structure.\n\ |
|
476 @end deftypefn") |
|
477 { |
|
478 octave_value retval; |
|
479 |
|
480 if (args.length () == 1) |
|
481 retval = args(0).is_map (); |
|
482 else |
|
483 print_usage ("isstruct"); |
|
484 |
|
485 return retval; |
|
486 } |
|
487 |
|
488 DEFUN (fieldnames, args, , |
|
489 "-*- texinfo -*-\n\ |
|
490 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ |
|
491 Return a cell array of strings naming the elements of the structure\n\ |
|
492 @var{struct}. It is an error to call @code{fieldnames} with an\n\ |
|
493 argument that is not a structure.\n\ |
|
494 @end deftypefn") |
|
495 { |
|
496 octave_value retval; |
|
497 |
|
498 int nargin = args.length (); |
|
499 |
|
500 if (nargin == 1) |
|
501 { |
|
502 if (args(0).is_map ()) |
|
503 { |
|
504 Octave_map m = args(0).map_value (); |
|
505 retval = Cell (m.keys ()); |
|
506 } |
|
507 else |
|
508 gripe_wrong_type_arg ("fieldnames", args(0)); |
|
509 } |
|
510 else |
|
511 print_usage ("fieldnames"); |
|
512 |
|
513 return retval; |
|
514 } |
|
515 |
|
516 DEFUN (isfield, args, , |
|
517 "-*- texinfo -*-\n\ |
|
518 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ |
|
519 Return true if the expression @var{expr} is a structure and it includes an\n\ |
|
520 element named @var{name}. The first argument must be a structure and\n\ |
|
521 the second must be a string.\n\ |
|
522 @end deftypefn") |
|
523 { |
|
524 octave_value retval; |
|
525 |
|
526 int nargin = args.length (); |
|
527 |
|
528 if (nargin == 2) |
|
529 { |
|
530 retval = false; |
|
531 |
|
532 // XXX FIXME XXX -- should this work for all types that can do |
|
533 // structure reference operations? |
|
534 |
|
535 if (args(0).is_map () && args(1).is_string ()) |
|
536 { |
|
537 std::string key = args(1).string_value (); |
|
538 |
|
539 Octave_map m = args(0).map_value (); |
|
540 |
|
541 retval = m.contains (key) != 0; |
|
542 } |
|
543 else |
|
544 print_usage ("isfield"); |
|
545 } |
|
546 else |
|
547 print_usage ("isfield"); |
|
548 |
|
549 return retval; |
|
550 } |
|
551 |
2376
|
552 /* |
|
553 ;;; Local Variables: *** |
|
554 ;;; mode: C++ *** |
|
555 ;;; End: *** |
|
556 */ |