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 |
4750
|
43 #include "Array-util.h" |
|
44 |
4687
|
45 #include "byte-swap.h" |
|
46 #include "ls-oct-ascii.h" |
|
47 #include "ls-oct-binary.h" |
|
48 #include "ls-hdf5.h" |
|
49 #include "ls-utils.h" |
|
50 |
3219
|
51 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
2376
|
52 |
4612
|
53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct"); |
2376
|
54 |
4513
|
55 Cell |
3933
|
56 octave_struct::dotref (const octave_value_list& idx) |
2962
|
57 { |
4513
|
58 Cell retval; |
3933
|
59 |
|
60 assert (idx.length () == 1); |
2962
|
61 |
3933
|
62 std::string nm = idx(0).string_value (); |
|
63 |
4219
|
64 Octave_map::const_iterator p = map.seek (nm); |
2376
|
65 |
4219
|
66 if (p != map.end ()) |
3933
|
67 retval = map.contents (p); |
|
68 else |
2376
|
69 error ("structure has no member `%s'", nm.c_str ()); |
|
70 |
|
71 return retval; |
|
72 } |
|
73 |
4513
|
74 #if 0 |
3933
|
75 static void |
|
76 gripe_invalid_index (void) |
|
77 { |
|
78 error ("invalid index for structure array"); |
|
79 } |
4513
|
80 #endif |
3933
|
81 |
|
82 static void |
|
83 gripe_invalid_index_for_assignment (void) |
|
84 { |
|
85 error ("invalid index for structure array assignment"); |
|
86 } |
|
87 |
|
88 static void |
|
89 gripe_invalid_index_type (const std::string& nm, char t) |
|
90 { |
|
91 error ("%s cannot be indexed with %c", nm.c_str (), t); |
|
92 } |
|
93 |
|
94 static void |
|
95 gripe_failed_assignment (void) |
|
96 { |
|
97 error ("assignment to structure element failed"); |
|
98 } |
|
99 |
|
100 octave_value |
4247
|
101 octave_struct::subsref (const std::string& type, |
4219
|
102 const std::list<octave_value_list>& idx) |
3933
|
103 { |
|
104 octave_value retval; |
|
105 |
|
106 int skip = 1; |
|
107 |
|
108 switch (type[0]) |
|
109 { |
|
110 case '(': |
|
111 { |
|
112 if (type.length () > 1 && type[1] == '.') |
|
113 { |
4219
|
114 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
115 octave_value_list key_idx = *++p; |
3933
|
116 |
4513
|
117 Cell tmp = dotref (key_idx); |
3933
|
118 |
|
119 if (! error_state) |
|
120 { |
4513
|
121 Cell t = tmp.index (idx.front ()); |
3933
|
122 |
4513
|
123 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933
|
124 |
4513
|
125 // We handled two index elements, so tell |
|
126 // next_subsref to skip both of them. |
3933
|
127 |
4513
|
128 skip++; |
3933
|
129 } |
|
130 } |
|
131 else |
4513
|
132 retval = map.index (idx.front ()); |
3933
|
133 } |
|
134 break; |
|
135 |
|
136 case '.': |
|
137 { |
4513
|
138 Cell t = dotref (idx.front ()); |
3933
|
139 |
4499
|
140 retval = (t.length () == 1) ? t(0) : octave_value (t, true); |
3933
|
141 } |
|
142 break; |
|
143 |
|
144 case '{': |
|
145 gripe_invalid_index_type (type_name (), type[0]); |
|
146 break; |
|
147 |
|
148 default: |
|
149 panic_impossible (); |
|
150 } |
|
151 |
|
152 if (! error_state) |
|
153 retval = retval.next_subsref (type, idx, skip); |
|
154 |
|
155 return retval; |
|
156 } |
|
157 |
|
158 octave_value |
4513
|
159 octave_struct::numeric_conv (const Cell& val, |
3933
|
160 const std::string& type) |
|
161 { |
|
162 octave_value retval; |
|
163 |
|
164 if (val.length () == 1) |
|
165 { |
|
166 retval = val(0); |
|
167 |
|
168 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) |
|
169 retval = Octave_map (); |
|
170 } |
|
171 else |
|
172 gripe_invalid_index_for_assignment (); |
|
173 |
|
174 return retval; |
|
175 } |
|
176 |
|
177 octave_value |
4247
|
178 octave_struct::subsasgn (const std::string& type, |
4219
|
179 const std::list<octave_value_list>& idx, |
3933
|
180 const octave_value& rhs) |
2376
|
181 { |
3933
|
182 octave_value retval; |
|
183 |
|
184 int n = type.length (); |
|
185 |
|
186 octave_value t_rhs = rhs; |
|
187 |
|
188 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
|
189 { |
|
190 switch (type[0]) |
|
191 { |
|
192 case '(': |
|
193 { |
|
194 if (type.length () > 1 && type[1] == '.') |
|
195 { |
4219
|
196 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
197 octave_value_list t_idx = *p; |
3933
|
198 |
4513
|
199 octave_value_list key_idx = *++p; |
|
200 |
|
201 assert (key_idx.length () == 1); |
3933
|
202 |
4513
|
203 std::string key = key_idx(0).string_value (); |
3933
|
204 |
4513
|
205 octave_value u; |
3933
|
206 |
4513
|
207 if (! map.contains (key)) |
|
208 u = octave_value::empty_conv (type.substr (2), rhs); |
|
209 else |
|
210 { |
4675
|
211 Cell map_val = map.contents (key); |
3933
|
212 |
4513
|
213 Cell map_elt = map_val.index (idx.front (), true); |
3933
|
214 |
4513
|
215 u = numeric_conv (map_elt, type.substr (2)); |
|
216 } |
3933
|
217 |
4513
|
218 if (! error_state) |
|
219 { |
|
220 std::list<octave_value_list> next_idx (idx); |
3933
|
221 |
4513
|
222 // We handled two index elements, so subsasgn to |
|
223 // needs to skip both of them. |
3933
|
224 |
4513
|
225 next_idx.erase (next_idx.begin ()); |
|
226 next_idx.erase (next_idx.begin ()); |
4059
|
227 |
4513
|
228 u.make_unique (); |
|
229 |
|
230 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); |
3933
|
231 } |
|
232 } |
|
233 else |
|
234 gripe_invalid_index_for_assignment (); |
|
235 } |
|
236 break; |
|
237 |
|
238 case '.': |
|
239 { |
|
240 octave_value_list key_idx = idx.front (); |
|
241 |
|
242 assert (key_idx.length () == 1); |
|
243 |
|
244 std::string key = key_idx(0).string_value (); |
|
245 |
|
246 octave_value u; |
|
247 |
|
248 if (! map.contains (key)) |
|
249 u = octave_value::empty_conv (type.substr (1), rhs); |
|
250 else |
|
251 { |
4675
|
252 Cell map_val = map.contents (key); |
3933
|
253 |
|
254 u = numeric_conv (map_val, type.substr (1)); |
|
255 } |
|
256 |
|
257 if (! error_state) |
|
258 { |
4219
|
259 std::list<octave_value_list> next_idx (idx); |
3933
|
260 |
4219
|
261 next_idx.erase (next_idx.begin ()); |
3933
|
262 |
4059
|
263 u.make_unique (); |
|
264 |
3933
|
265 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); |
|
266 } |
|
267 } |
|
268 break; |
|
269 |
|
270 case '{': |
|
271 gripe_invalid_index_type (type_name (), type[0]); |
|
272 break; |
|
273 |
|
274 default: |
|
275 panic_impossible (); |
|
276 } |
|
277 } |
|
278 |
|
279 if (! error_state) |
|
280 { |
|
281 switch (type[0]) |
|
282 { |
|
283 case '(': |
|
284 { |
|
285 if (n > 1 && type[1] == '.') |
|
286 { |
4219
|
287 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
288 octave_value_list key_idx = *++p; |
3933
|
289 |
|
290 assert (key_idx.length () == 1); |
|
291 |
|
292 std::string key = key_idx(0).string_value (); |
|
293 |
|
294 if (! error_state) |
|
295 { |
4513
|
296 map.assign (idx.front (), key, t_rhs); |
3933
|
297 |
4513
|
298 if (! error_state) |
|
299 retval = octave_value (this, count + 1); |
3933
|
300 else |
4513
|
301 gripe_failed_assignment (); |
3933
|
302 } |
|
303 else |
|
304 gripe_failed_assignment (); |
|
305 } |
|
306 else |
4197
|
307 { |
4513
|
308 Octave_map rhs_map = t_rhs.map_value (); |
4197
|
309 |
4513
|
310 if (! error_state) |
4197
|
311 { |
4513
|
312 map.assign (idx.front (), rhs_map); |
4197
|
313 |
|
314 if (! error_state) |
4513
|
315 retval = octave_value (this, count + 1); |
4197
|
316 else |
4513
|
317 gripe_failed_assignment (); |
4197
|
318 } |
4513
|
319 else |
|
320 error ("invalid structure assignment"); |
4197
|
321 } |
3933
|
322 } |
|
323 break; |
|
324 |
|
325 case '.': |
|
326 { |
|
327 octave_value_list key_idx = idx.front (); |
|
328 |
|
329 assert (key_idx.length () == 1); |
|
330 |
|
331 std::string key = key_idx(0).string_value (); |
|
332 |
|
333 map.assign (key, t_rhs); |
|
334 |
|
335 if (! error_state) |
|
336 retval = octave_value (this, count + 1); |
|
337 else |
|
338 gripe_failed_assignment (); |
|
339 } |
|
340 break; |
|
341 |
|
342 case '{': |
|
343 gripe_invalid_index_type (type_name (), type[0]); |
|
344 break; |
|
345 |
|
346 default: |
|
347 panic_impossible (); |
|
348 } |
|
349 } |
|
350 else |
|
351 gripe_failed_assignment (); |
|
352 |
|
353 return retval; |
2376
|
354 } |
|
355 |
|
356 void |
3523
|
357 octave_struct::print (std::ostream& os, bool) const |
2901
|
358 { |
|
359 print_raw (os); |
|
360 } |
|
361 |
|
362 void |
3523
|
363 octave_struct::print_raw (std::ostream& os, bool) const |
2376
|
364 { |
|
365 // XXX FIXME XXX -- would be nice to print the output in some |
|
366 // standard order. Maybe all substructures first, maybe |
|
367 // alphabetize entries, etc. |
|
368 |
2985
|
369 unwind_protect::begin_frame ("octave_struct_print"); |
2376
|
370 |
|
371 unwind_protect_int (Vstruct_levels_to_print); |
|
372 |
3961
|
373 if (Vstruct_levels_to_print >= 0) |
2376
|
374 { |
3961
|
375 bool print_keys_only = (Vstruct_levels_to_print == 0); |
|
376 |
|
377 Vstruct_levels_to_print--; |
|
378 |
2901
|
379 indent (os); |
|
380 os << "{"; |
|
381 newline (os); |
2376
|
382 |
2901
|
383 increment_indent_level (); |
2376
|
384 |
4561
|
385 int n = map.numel (); |
3932
|
386 |
4604
|
387 if (n > 1 && print_keys_only) |
|
388 { |
|
389 indent (os); |
|
390 dim_vector dv = dims (); |
|
391 os << dv.str () << " struct array containing the fields:"; |
|
392 newline (os); |
|
393 newline (os); |
|
394 |
|
395 increment_indent_level (); |
|
396 } |
|
397 |
4219
|
398 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) |
2376
|
399 { |
3523
|
400 std::string key = map.key (p); |
4513
|
401 Cell val = map.contents (p); |
2376
|
402 |
4499
|
403 octave_value tmp = (n == 1) ? val(0) : octave_value (val, true); |
3961
|
404 |
|
405 if (print_keys_only) |
3932
|
406 { |
3961
|
407 indent (os); |
4604
|
408 os << key; |
|
409 if (n == 1) |
|
410 { |
|
411 dim_vector dv = tmp.dims (); |
|
412 os << ": " << dv.str () << " " << tmp.type_name (); |
|
413 } |
3961
|
414 newline (os); |
3932
|
415 } |
3961
|
416 else |
4121
|
417 tmp.print_with_name (os, key); |
2376
|
418 } |
|
419 |
4604
|
420 if (n > 1 && print_keys_only) |
|
421 decrement_indent_level (); |
|
422 |
2901
|
423 decrement_indent_level (); |
2376
|
424 |
2901
|
425 indent (os); |
|
426 os << "}"; |
|
427 newline (os); |
2376
|
428 } |
|
429 else |
2901
|
430 { |
3961
|
431 indent (os); |
|
432 os << "<structure>"; |
2901
|
433 newline (os); |
|
434 } |
2376
|
435 |
2985
|
436 unwind_protect::run_frame ("octave_struct_print"); |
2376
|
437 } |
|
438 |
2901
|
439 bool |
3523
|
440 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
441 { |
3961
|
442 bool retval = false; |
|
443 |
2901
|
444 indent (os); |
3961
|
445 |
|
446 if (Vstruct_levels_to_print < 0) |
|
447 os << name << " = "; |
|
448 else |
|
449 { |
|
450 os << name << " ="; |
|
451 newline (os); |
|
452 retval = true; |
|
453 } |
|
454 |
|
455 return retval; |
2901
|
456 } |
|
457 |
4744
|
458 static bool |
|
459 scalar (const dim_vector& dims) |
|
460 { |
|
461 return dims.length () == 2 && dims (0) == 1 && dims (1) == 1; |
|
462 } |
|
463 |
|
464 // XXX FIXME XXX -- move these tests to the test directory? |
|
465 /* |
|
466 %!shared x |
|
467 %! x(1).a=1; x(2).a=2; x(1).b=3; x(2).b=3; |
|
468 %!assert(struct('a',1,'b',3),x(1)) |
|
469 %!assert(struct('a',{},'b',{}),x([])) |
|
470 %!assert(struct('a',{1,2},'b',{3,3}),x) |
|
471 %!assert(struct('a',{1,2},'b',3),x) |
|
472 %!assert(struct('a',{1,2},'b',{3}),x) |
|
473 %!assert(struct('b',3,'a',{1,2}),x) |
|
474 %!assert(struct('b',{3},'a',{1,2}),x) |
|
475 %!test x=struct([]); |
|
476 %!assert(size(x),[0,0]); |
|
477 %!assert(isstruct(x)); |
|
478 %!assert(isempty(fieldnames(x))); |
|
479 %!fail("struct('a',{1,2},'b',{1,2,3})","dimensions of parameter 2 do not match those of parameter 4") |
|
480 %!fail("struct(1,2,3,4)","struct expects alternating 'field',value pairs"); |
|
481 %!fail("struct('1',2,'3')","struct expects alternating 'field',value pairs"); |
|
482 */ |
|
483 |
|
484 DEFUN (struct, args, , |
|
485 "-*- texinfo -*-\n\ |
|
486 @deftypefn {Built-in Function} {} struct (\"field\", @var{value}, \"field\", @var{value}, @dots{})\n\ |
|
487 \n\ |
|
488 Create a structure and initialize its value.\n\ |
|
489 \n\ |
|
490 If the values are cell arrays, create a structure array and initialize\n\ |
|
491 its values. The dimensions of each cell array of values must match.\n\ |
|
492 Singleton cells and non-cell values are repeated so that they fill\n\ |
|
493 the entire array. If the cells are empty, create an empty structure\n\ |
|
494 array with the specified field names.") |
|
495 { |
|
496 octave_value_list retval; |
|
497 |
|
498 int nargin = args.length (); |
|
499 |
|
500 // struct([]) returns an empty struct. |
|
501 // XXX FIXME XXX should struct() also create an empty struct? |
|
502 |
|
503 if (nargin == 1 && args(0).is_empty () && args(0).is_real_matrix ()) |
|
504 return octave_value (Octave_map ()); |
|
505 |
|
506 // Check for "field", VALUE pairs. |
|
507 |
|
508 for (int i = 0; i < nargin; i += 2) |
|
509 { |
|
510 if (! args(i).is_string () || i + 1 >= nargin) |
|
511 { |
|
512 error ("struct expects alternating \"field\", VALUE pairs"); |
|
513 return retval; |
|
514 } |
|
515 } |
|
516 |
|
517 // Check that the dimensions of the values correspond. |
|
518 |
|
519 dim_vector dims (1, 1); |
|
520 |
|
521 int first_dimensioned_value = 0; |
|
522 |
|
523 for (int i = 1; i < nargin; i += 2) |
|
524 { |
|
525 if (args(i).is_cell ()) |
|
526 { |
|
527 dim_vector argdims (args(i).dims ()); |
|
528 |
|
529 if (! scalar (argdims)) |
|
530 { |
|
531 if (! first_dimensioned_value) |
|
532 { |
|
533 dims = argdims; |
|
534 first_dimensioned_value = i + 1; |
|
535 } |
|
536 else if (dims != argdims) |
|
537 { |
|
538 error ("struct: dimensions of parameter %d do not match those of parameter %d", |
|
539 first_dimensioned_value, i+1); |
|
540 return retval; |
|
541 } |
|
542 } |
|
543 } |
|
544 } |
|
545 |
|
546 // Create the return value. |
|
547 |
|
548 Octave_map map (dims); |
|
549 |
|
550 for (int i = 0; i < nargin; i+= 2) |
|
551 { |
|
552 // Get key. |
|
553 |
|
554 std::string key (args(i).string_value ()); |
|
555 |
|
556 if (error_state) |
|
557 return retval; |
|
558 |
|
559 // Value may be v, { v }, or { v1, v2, ... } |
|
560 // In the first two cases, we need to create a cell array of |
|
561 // the appropriate dimensions filled with v. In the last case, |
|
562 // the cell array has already been determined to be of the |
|
563 // correct dimensions. |
|
564 |
|
565 if (args(i+1).is_cell ()) |
|
566 { |
|
567 const Cell c (args(i+1).cell_value ()); |
|
568 |
|
569 if (error_state) |
|
570 return retval; |
|
571 |
|
572 if (scalar (c.dims ())) |
|
573 map.assign (key, Cell (dims, c(0))); |
|
574 else |
|
575 map.assign (key, c); |
|
576 } |
|
577 else |
|
578 map.assign (key, Cell (dims, args(i+1))); |
|
579 |
|
580 if (error_state) |
|
581 return retval; |
|
582 } |
|
583 |
|
584 return octave_value (map); |
|
585 } |
|
586 |
4358
|
587 DEFUN (isstruct, args, , |
|
588 "-*- texinfo -*-\n\ |
|
589 @deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ |
|
590 Return 1 if the value of the expression @var{expr} is a structure.\n\ |
|
591 @end deftypefn") |
|
592 { |
|
593 octave_value retval; |
|
594 |
|
595 if (args.length () == 1) |
|
596 retval = args(0).is_map (); |
|
597 else |
|
598 print_usage ("isstruct"); |
|
599 |
|
600 return retval; |
|
601 } |
|
602 |
|
603 DEFUN (fieldnames, args, , |
|
604 "-*- texinfo -*-\n\ |
|
605 @deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ |
|
606 Return a cell array of strings naming the elements of the structure\n\ |
|
607 @var{struct}. It is an error to call @code{fieldnames} with an\n\ |
|
608 argument that is not a structure.\n\ |
|
609 @end deftypefn") |
|
610 { |
|
611 octave_value retval; |
|
612 |
|
613 int nargin = args.length (); |
|
614 |
|
615 if (nargin == 1) |
|
616 { |
|
617 if (args(0).is_map ()) |
|
618 { |
|
619 Octave_map m = args(0).map_value (); |
4744
|
620 string_vector keys = m.keys (); |
|
621 if (keys.length () == 0) |
|
622 retval = Cell (0, 1); |
|
623 else |
|
624 retval = Cell (m.keys ()); |
4358
|
625 } |
|
626 else |
|
627 gripe_wrong_type_arg ("fieldnames", args(0)); |
|
628 } |
|
629 else |
|
630 print_usage ("fieldnames"); |
|
631 |
|
632 return retval; |
|
633 } |
|
634 |
|
635 DEFUN (isfield, args, , |
|
636 "-*- texinfo -*-\n\ |
|
637 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ |
|
638 Return true if the expression @var{expr} is a structure and it includes an\n\ |
|
639 element named @var{name}. The first argument must be a structure and\n\ |
|
640 the second must be a string.\n\ |
|
641 @end deftypefn") |
|
642 { |
|
643 octave_value retval; |
|
644 |
|
645 int nargin = args.length (); |
|
646 |
|
647 if (nargin == 2) |
|
648 { |
|
649 retval = false; |
|
650 |
|
651 // XXX FIXME XXX -- should this work for all types that can do |
|
652 // structure reference operations? |
|
653 |
|
654 if (args(0).is_map () && args(1).is_string ()) |
|
655 { |
|
656 std::string key = args(1).string_value (); |
|
657 |
|
658 Octave_map m = args(0).map_value (); |
|
659 |
|
660 retval = m.contains (key) != 0; |
|
661 } |
|
662 else |
|
663 print_usage ("isfield"); |
|
664 } |
|
665 else |
|
666 print_usage ("isfield"); |
|
667 |
|
668 return retval; |
|
669 } |
|
670 |
4687
|
671 bool |
|
672 octave_struct::save_ascii (std::ostream& os, bool& infnan_warned, |
|
673 bool strip_nan_and_inf) |
|
674 { |
|
675 Octave_map m = map_value (); |
|
676 os << "# length: " << m.length () << "\n"; |
|
677 |
|
678 Octave_map::iterator i = m.begin (); |
|
679 while (i != m.end ()) |
|
680 { |
|
681 Cell val = map.contents (i); |
|
682 octave_value tmp = (map.numel () == 1) ? val(0) : |
|
683 octave_value (val, true); |
|
684 |
|
685 bool b = save_ascii_data (os, tmp, m.key (i), infnan_warned, |
|
686 strip_nan_and_inf, 0, 0); |
|
687 |
|
688 if (! b) |
|
689 return os; |
|
690 |
|
691 i++; |
|
692 } |
|
693 |
|
694 return true; |
|
695 } |
|
696 |
|
697 bool |
|
698 octave_struct::load_ascii (std::istream& is) |
|
699 { |
|
700 int len = 0; |
|
701 bool success = true; |
|
702 |
|
703 if (extract_keyword (is, "length", len) && len >= 0) |
|
704 { |
|
705 if (len > 0) |
|
706 { |
|
707 Octave_map m (map); |
|
708 |
|
709 for (int j = 0; j < len; j++) |
|
710 { |
|
711 octave_value t2; |
|
712 bool dummy; |
|
713 |
|
714 // recurse to read cell elements |
|
715 std::string nm |
|
716 = read_ascii_data (is, std::string (), dummy, t2, count); |
|
717 |
|
718 if (!is) |
|
719 break; |
|
720 |
|
721 m.assign (nm, t2); |
|
722 } |
|
723 |
|
724 if (is) |
|
725 map = m; |
|
726 else |
|
727 { |
|
728 error ("load: failed to load structure"); |
|
729 success = false; |
|
730 } |
|
731 } |
|
732 else if (len == 0 ) |
|
733 map = Octave_map (); |
|
734 else |
|
735 panic_impossible (); |
|
736 } |
|
737 else { |
|
738 error ("load: failed to extract number of elements in structure"); |
|
739 success = false; |
|
740 } |
|
741 |
|
742 return success; |
|
743 } |
|
744 |
|
745 bool |
|
746 octave_struct::save_binary (std::ostream& os, bool& save_as_floats) |
|
747 { |
|
748 Octave_map m = map_value (); |
|
749 |
|
750 FOUR_BYTE_INT len = m.length(); |
|
751 os.write (X_CAST (char *, &len), 4); |
|
752 |
|
753 Octave_map::iterator i = m.begin (); |
|
754 while (i != m.end ()) |
|
755 { |
|
756 Cell val = map.contents (i); |
|
757 octave_value tmp = (map.numel () == 1) ? val(0) : |
|
758 octave_value (val, true); |
|
759 |
|
760 bool b = save_binary_data (os, tmp, m.key (i), "", 0, save_as_floats); |
|
761 |
|
762 if (! b) |
|
763 return os; |
|
764 |
|
765 i++; |
|
766 } |
|
767 |
|
768 return true; |
|
769 } |
|
770 |
|
771 bool |
|
772 octave_struct::load_binary (std::istream& is, bool swap, |
|
773 oct_mach_info::float_format fmt) |
|
774 { |
|
775 bool success = true; |
|
776 FOUR_BYTE_INT len; |
|
777 if (! is.read (X_CAST (char *, &len), 4)) |
|
778 return false; |
|
779 if (swap) |
|
780 swap_4_bytes (X_CAST (char *, &len)); |
|
781 |
|
782 if (len > 0) |
|
783 { |
|
784 Octave_map m (map); |
|
785 |
|
786 for (int j = 0; j < len; j++) |
|
787 { |
|
788 octave_value t2; |
|
789 bool dummy; |
|
790 std::string doc; |
|
791 |
|
792 // recurse to read cell elements |
|
793 std::string nm = read_binary_data (is, swap, fmt, std::string (), |
|
794 dummy, t2, doc); |
|
795 |
|
796 if (!is) |
|
797 break; |
|
798 |
|
799 m.assign (nm, t2); |
|
800 } |
|
801 |
|
802 if (is) |
|
803 map = m; |
|
804 else |
|
805 { |
|
806 error ("load: failed to load structure"); |
|
807 success = false; |
|
808 } |
|
809 } |
|
810 else if (len == 0 ) |
|
811 map = Octave_map (); |
|
812 else |
|
813 panic_impossible (); |
|
814 |
|
815 return success; |
|
816 } |
|
817 |
|
818 #if defined (HAVE_HDF5) |
|
819 bool |
|
820 octave_struct::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) |
|
821 { |
|
822 hid_t data_hid = -1; |
|
823 |
|
824 data_hid = H5Gcreate (loc_id, name, 0); |
|
825 if (data_hid < 0) return false; |
|
826 |
|
827 // recursively add each element of the structure to this group |
|
828 Octave_map m = map_value (); |
|
829 Octave_map::iterator i = m.begin (); |
|
830 while (i != m.end ()) |
|
831 { |
|
832 Cell val = map.contents (i); |
|
833 octave_value tmp = (map.numel () == 1) ? val(0) : |
|
834 octave_value (val, true); |
|
835 |
|
836 bool retval2 = add_hdf5_data (data_hid, tmp, m.key (i), "", false, |
|
837 save_as_floats); |
|
838 |
|
839 if (! retval2) |
|
840 break; |
|
841 |
|
842 i++; |
|
843 } |
|
844 |
|
845 H5Gclose (data_hid); |
|
846 return true; |
|
847 } |
|
848 |
|
849 bool |
|
850 octave_struct::load_hdf5 (hid_t loc_id, const char *name, |
|
851 bool have_h5giterate_bug) |
|
852 { |
|
853 bool retval = false; |
|
854 |
|
855 hdf5_callback_data dsub; |
|
856 |
|
857 herr_t retval2; |
|
858 Octave_map m; |
|
859 int current_item = 0; |
4696
|
860 #ifdef HAVE_H5GGET_NUM_OBJS |
|
861 hsize_t num_obj = 0; |
|
862 H5Gget_num_objs (loc_id, &num_obj); |
|
863 |
|
864 while (current_item < static_cast<int> (num_obj) |
|
865 && (retval2 = H5Giterate (loc_id, name, ¤t_item, |
|
866 hdf5_read_next_data, &dsub)) > 0) |
|
867 #else |
4687
|
868 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, |
|
869 hdf5_read_next_data, &dsub)) > 0) |
4696
|
870 #endif |
4687
|
871 { |
|
872 m.assign (dsub.name, dsub.tc); |
|
873 |
|
874 if (have_h5giterate_bug) |
|
875 current_item++; // H5Giterate returned the last index processed |
|
876 } |
|
877 |
|
878 if (retval2 >= 0) |
|
879 { |
|
880 map = m; |
|
881 retval = true; |
|
882 } |
|
883 |
|
884 return retval; |
|
885 } |
4750
|
886 |
|
887 // Check that the dimensions of the input arguments are correct. |
|
888 |
|
889 static bool |
|
890 cell2struct_check_args (const dim_vector& c_dv, const dim_vector& f_dv, |
|
891 bool is_cell, int dim) |
|
892 { |
|
893 bool retval (true); |
|
894 |
|
895 if (dim >= 0 && dim < c_dv.length ()) |
|
896 { |
|
897 if (is_cell) |
|
898 { |
|
899 int f_el = f_dv.numel (); |
|
900 |
|
901 if (f_el != c_dv(dim)) |
|
902 { |
|
903 error ("cell2struct: number of fields must match size (CELL, DIM)"); |
|
904 |
|
905 retval = false; |
|
906 } |
|
907 } |
|
908 else |
|
909 { |
|
910 if (f_dv.length () > 2) |
|
911 { |
|
912 error ("cell2struct: field array must be a 2 dimensional matrix"); |
|
913 |
|
914 retval = false; |
|
915 } |
|
916 else if (f_dv(0) != c_dv(dim)) |
|
917 { |
|
918 error ("cell2struct: number of fields in character array must " |
|
919 "match the number of elements in CELL along the specified " |
|
920 "dimension: size (FIELD, 1) == length (C, DIM)"); |
|
921 |
|
922 retval = false; |
|
923 } |
|
924 } |
|
925 } |
|
926 else |
|
927 { |
|
928 error ("cell2struct: DIM out of range"); |
|
929 |
|
930 retval = false; |
|
931 } |
|
932 |
|
933 return retval; |
|
934 } |
|
935 |
|
936 static void |
|
937 cell2struct_construct_idx (Array<int>& ra_idx1, const Array<int>& ra_idx2, |
|
938 int dim, int fill_value) |
|
939 { |
|
940 int iidx = 0; |
|
941 |
|
942 for (int idx = 0; idx < ra_idx1.length (); idx++) |
|
943 { |
|
944 if (idx == dim) |
|
945 ra_idx1.elem (idx) = fill_value; |
|
946 else |
|
947 ra_idx1.elem (idx) = ra_idx2(iidx++); |
|
948 } |
|
949 } |
|
950 |
|
951 DEFUN (cell2struct, args, , |
|
952 "-*- texinfo -*-\n\ |
|
953 @deftypefn {Built-in Function} {} cell2struct (@var{CELL}, @var{FIELDS}, @var{DIM})\n\ |
|
954 Convert @var{CELL} to a structure. The number of fields in @var{FIELDS}\n\ |
|
955 must match the number of elements in @var{CELL} along dimension @var{DIM},\n\ |
|
956 that is @code{numel (@var{FIELDS}) == size (@var{CELL}, @var{DIM})}.\n\ |
|
957 \n\ |
|
958 @example\n\ |
|
959 @group\n\ |
|
960 A = cell2struct(@{'Peter', 'Hannah', 'Robert'; 185, 170, 168@}, @{'Name','Height'@}, 1);\n\ |
|
961 A(1)\n\ |
|
962 @result{} ans =\n\ |
|
963 @{\n\ |
|
964 Height = 185\n\ |
|
965 Name = Peter\n\ |
|
966 @}\n\ |
|
967 \n\ |
|
968 @end group\n\ |
|
969 @end example\n\ |
|
970 @end deftypefn") |
|
971 { |
|
972 octave_value retval; |
|
973 |
|
974 if (args.length () != 3) |
|
975 { |
|
976 print_usage ("cell2struct"); |
|
977 return retval; |
|
978 } |
|
979 |
|
980 Cell c = args(0).cell_value (); |
|
981 |
|
982 if (error_state) |
|
983 { |
|
984 error ("cell2struct: expecting first argument to be a cell array"); |
|
985 return retval; |
|
986 } |
|
987 |
|
988 octave_value field = args(1); |
|
989 |
|
990 // Field is either cell or character matrix. |
|
991 |
|
992 bool field_is_cell = field.is_cell (); |
|
993 |
|
994 Cell field_cell; |
|
995 charMatrix field_char; |
|
996 |
|
997 if (field_is_cell) |
|
998 field_cell = field.cell_value (); |
|
999 else |
|
1000 field_char = field.char_matrix_value (); |
|
1001 |
|
1002 if (error_state) |
|
1003 { |
|
1004 error ("cell2struct: expecting second argument to be a cell or character array"); |
|
1005 return retval; |
|
1006 } |
|
1007 |
|
1008 // Retrieve the dimension value. |
|
1009 |
|
1010 // XXX FIX ME XXX -- int_value () should print out the conversions |
|
1011 // it does to be Matlab compatible. |
|
1012 |
|
1013 int dim = args(2).int_value () - 1; |
|
1014 |
|
1015 if (error_state) |
|
1016 { |
|
1017 error ("cell2struct: expecting third argument to be an integer"); |
|
1018 return retval; |
|
1019 } |
|
1020 |
|
1021 dim_vector c_dv = c.dims (); |
|
1022 dim_vector field_dv = field.dims (); |
|
1023 |
|
1024 if (cell2struct_check_args (c_dv, field_dv, field_is_cell, dim)) |
|
1025 { |
|
1026 int c_dv_length = c_dv.length (); |
|
1027 |
|
1028 // Dimension vector for the Cell arrays to be put into the structure. |
|
1029 |
|
1030 dim_vector value_dv; |
|
1031 |
|
1032 // Initialize c_value_dv. |
|
1033 |
|
1034 if (c_dv_length == 2) |
|
1035 value_dv = dim_vector (1, 1); |
|
1036 else |
|
1037 value_dv.resize (c_dv_length - 1); |
|
1038 |
|
1039 int idx_tmp = 0; |
|
1040 |
|
1041 for (int i = 0; i < c_dv_length; i++) |
|
1042 { |
|
1043 if (i != dim) |
|
1044 value_dv.elem (idx_tmp++) = c_dv.elem (i); |
|
1045 } |
|
1046 |
|
1047 // All initializing is done, we can start moving values. |
|
1048 |
|
1049 Octave_map map; |
|
1050 |
|
1051 // If field is a cell array then we use all elements in array, |
|
1052 // on the other hand when field is a character array the number |
|
1053 // of elements is equals the number of rows. |
|
1054 |
|
1055 int field_numel = field_is_cell ? field_dv.numel (): field_dv(0); |
|
1056 |
|
1057 // For matlab compatibility. |
|
1058 |
|
1059 if (field_numel == 0) |
|
1060 map.reshape (dim_vector (0, 1)); |
|
1061 |
|
1062 for (int i = 0; i < field_numel; i++) |
|
1063 { |
|
1064 // Construct cell array which goes into the structure together |
|
1065 // with the appropriate field name. |
|
1066 |
|
1067 Cell c_value (value_dv); |
|
1068 |
|
1069 Array<int> value_idx (value_dv.length (), 0); |
|
1070 Array<int> c_idx (c_dv_length, 0); |
|
1071 |
|
1072 for (int j = 0; j < value_dv.numel (); j++) |
|
1073 { |
|
1074 // Need to do this to construct the appropriate |
|
1075 // idx for getting elements from the original cell array. |
|
1076 |
|
1077 cell2struct_construct_idx (c_idx, value_idx, dim, i); |
|
1078 |
|
1079 c_value.elem (value_idx) = c.elem (c_idx); |
|
1080 |
|
1081 increment_index (value_idx, value_dv); |
|
1082 } |
|
1083 |
|
1084 std::string field_str; |
|
1085 |
|
1086 if (field_is_cell) |
|
1087 { |
|
1088 // Matlab retrieves the field values column by column. |
|
1089 |
|
1090 octave_value field_tmp = field_cell.elem (i); |
|
1091 |
|
1092 field_str = field_tmp.string_value (); |
|
1093 |
|
1094 if (error_state) |
|
1095 { |
|
1096 error ("cell2struct: fields have to be of type string"); |
|
1097 return retval; |
|
1098 } |
|
1099 } |
|
1100 else |
|
1101 { |
|
1102 field_str = field_char.row_as_string (i); |
|
1103 |
|
1104 if (error_state) |
|
1105 return retval; |
|
1106 } |
|
1107 |
|
1108 map.reshape (value_dv); |
|
1109 |
|
1110 map.assign (field_str, c_value); |
|
1111 } |
|
1112 |
|
1113 retval = map; |
|
1114 } |
|
1115 |
|
1116 return retval; |
|
1117 } |
|
1118 |
4687
|
1119 #endif |
|
1120 |
2376
|
1121 /* |
|
1122 ;;; Local Variables: *** |
|
1123 ;;; mode: C++ *** |
|
1124 ;;; End: *** |
|
1125 */ |