Mercurial > hg > octave-nkf
annotate src/ov-cell.cc @ 11935:8bc4e0ae758c release-3-0-x
handle CRLF and CR in fgetl/fgets
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 23 Feb 2009 21:04:10 +0100 |
parents | 65e41465c46b |
children |
rev | line source |
---|---|
3353 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007 |
4 John W. Eaton | |
3353 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3353 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3353 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
5850 | 28 #include <iomanip> |
3503 | 29 #include <iostream> |
5765 | 30 #include <sstream> |
5164 | 31 #include <vector> |
3353 | 32 |
5360 | 33 #include "Array-util.h" |
34 #include "byte-swap.h" | |
3353 | 35 #include "lo-utils.h" |
4153 | 36 #include "quit.h" |
3353 | 37 |
38 #include "defun.h" | |
39 #include "error.h" | |
40 #include "ov-cell.h" | |
3354 | 41 #include "oct-obj.h" |
3353 | 42 #include "unwind-prot.h" |
3354 | 43 #include "utils.h" |
3928 | 44 #include "ov-base-mat.h" |
45 #include "ov-base-mat.cc" | |
46 #include "ov-re-mat.h" | |
47 #include "ov-scalar.h" | |
5360 | 48 #include "pr-output.h" |
49 #include "ov-scalar.h" | |
3928 | 50 |
4687 | 51 #include "ls-oct-ascii.h" |
52 #include "ls-oct-binary.h" | |
53 #include "ls-hdf5.h" | |
54 #include "ls-utils.h" | |
55 | |
3928 | 56 template class octave_base_matrix<Cell>; |
3353 | 57 |
58 DEFINE_OCTAVE_ALLOCATOR (octave_cell); | |
59 | |
4612 | 60 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell", "cell"); |
3353 | 61 |
6833 | 62 static void |
63 gripe_failed_assignment (void) | |
64 { | |
65 error ("assignment to cell array failed"); | |
66 } | |
67 | |
4994 | 68 octave_value_list |
4247 | 69 octave_cell::subsref (const std::string& type, |
4994 | 70 const std::list<octave_value_list>& idx, int nargout) |
3933 | 71 { |
4994 | 72 octave_value_list retval; |
3933 | 73 |
74 switch (type[0]) | |
75 { | |
76 case '(': | |
4994 | 77 retval(0) = do_index_op (idx.front ()); |
3933 | 78 break; |
79 | |
80 case '{': | |
81 { | |
82 octave_value tmp = do_index_op (idx.front ()); | |
83 | |
4582 | 84 if (! error_state) |
3933 | 85 { |
4582 | 86 Cell tcell = tmp.cell_value (); |
87 | |
88 if (tcell.length () == 1) | |
4994 | 89 retval(0) = tcell(0,0); |
4582 | 90 else |
91 { | |
5275 | 92 octave_idx_type n = tcell.numel (); |
4705 | 93 |
94 octave_value_list lst (n, octave_value ()); | |
95 | |
5275 | 96 for (octave_idx_type i = 0; i < n; i++) |
4705 | 97 { |
98 OCTAVE_QUIT; | |
99 lst(i) = tcell(i); | |
100 } | |
101 | |
4994 | 102 retval(0) = octave_value (lst, true); |
4582 | 103 } |
3933 | 104 } |
105 } | |
106 break; | |
107 | |
108 case '.': | |
109 { | |
110 std::string nm = type_name (); | |
111 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
112 } | |
113 break; | |
114 | |
115 default: | |
116 panic_impossible (); | |
117 } | |
118 | |
5775 | 119 // FIXME -- perhaps there should be an |
4994 | 120 // octave_value_list::next_subsref member function? See also |
121 // octave_user_function::subsref. | |
122 | |
123 if (idx.size () > 1) | |
124 retval = retval(0).next_subsref (nargout, type, idx); | |
125 | |
126 return retval; | |
3933 | 127 } |
128 | |
129 octave_value | |
4247 | 130 octave_cell::subsasgn (const std::string& type, |
4219 | 131 const std::list<octave_value_list>& idx, |
3933 | 132 const octave_value& rhs) |
133 { | |
134 octave_value retval; | |
135 | |
136 int n = type.length (); | |
137 | |
138 octave_value t_rhs = rhs; | |
139 | |
140 if (n > 1) | |
141 { | |
142 switch (type[0]) | |
143 { | |
144 case '(': | |
145 { | |
6767 | 146 if (is_empty () && type[1] == '.') |
147 { | |
148 // Allow conversion of empty cell array to some other | |
149 // type in cases like | |
150 // | |
151 // x = []; x(i).f = rhs | |
3933 | 152 |
6767 | 153 octave_value tmp = octave_value::empty_conv (type, rhs); |
3933 | 154 |
6767 | 155 return tmp.subsasgn (type, idx, rhs); |
156 } | |
157 else | |
3933 | 158 { |
6767 | 159 octave_value tmp = do_index_op (idx.front (), true); |
3933 | 160 |
6767 | 161 if (! tmp.is_defined ()) |
162 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
3933 | 163 |
6767 | 164 if (! error_state) |
165 { | |
166 std::list<octave_value_list> next_idx (idx); | |
4362 | 167 |
6767 | 168 next_idx.erase (next_idx.begin ()); |
169 | |
170 tmp.make_unique (); | |
171 | |
172 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
173 } | |
3933 | 174 } |
175 } | |
176 break; | |
177 | |
178 case '{': | |
179 { | |
180 octave_value tmp = do_index_op (idx.front (), true); | |
181 | |
182 if (! tmp.is_defined ()) | |
183 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
184 | |
4362 | 185 if (! error_state) |
186 { | |
5992 | 187 const Cell tcell = tmp.cell_value (); |
3933 | 188 |
4362 | 189 if (! error_state && tcell.length () == 1) |
190 { | |
191 tmp = tcell(0,0); | |
3933 | 192 |
4362 | 193 std::list<octave_value_list> next_idx (idx); |
194 | |
195 next_idx.erase (next_idx.begin ()); | |
3933 | 196 |
4362 | 197 tmp.make_unique (); |
3933 | 198 |
5927 | 199 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
4519 | 200 tmp = octave_value::empty_conv (type.substr (1), rhs); |
201 | |
202 if (! error_state) | |
203 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
4362 | 204 } |
3933 | 205 } |
206 } | |
207 break; | |
208 | |
209 case '.': | |
210 { | |
211 std::string nm = type_name (); | |
212 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
213 } | |
214 break; | |
215 | |
216 default: | |
217 panic_impossible (); | |
218 } | |
219 } | |
220 | |
3940 | 221 if (! error_state) |
3933 | 222 { |
3940 | 223 switch (type[0]) |
224 { | |
225 case '(': | |
226 { | |
227 octave_value_list i = idx.front (); | |
3933 | 228 |
3940 | 229 if (t_rhs.is_cell ()) |
230 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); | |
231 else | |
4941 | 232 if (t_rhs.is_empty ()) |
233 octave_base_matrix<Cell>::assign (i, Cell()); | |
234 else | |
235 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | |
3933 | 236 |
6833 | 237 if (! error_state) |
238 { | |
239 count++; | |
240 retval = octave_value (this); | |
241 } | |
242 else | |
243 gripe_failed_assignment (); | |
3940 | 244 } |
245 break; | |
3933 | 246 |
3940 | 247 case '{': |
248 { | |
249 octave_value_list i = idx.front (); | |
3933 | 250 |
5846 | 251 if (t_rhs.is_cs_list ()) |
252 { | |
253 Cell tmp_cell = Cell (t_rhs.list_value ()); | |
254 | |
7040 | 255 // The shape of the RHS is irrelevant, we just want |
256 // the number of elements to agree and to preserve the | |
257 // shape of the left hand side of the assignment. | |
258 | |
259 if (numel () == tmp_cell.numel ()) | |
260 tmp_cell = tmp_cell.reshape (dims ()); | |
5846 | 261 |
262 octave_base_matrix<Cell>::assign (i, tmp_cell); | |
263 } | |
264 else | |
265 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | |
3933 | 266 |
6833 | 267 if (! error_state) |
268 { | |
269 count++; | |
270 retval = octave_value (this); | |
271 } | |
272 else | |
273 gripe_failed_assignment (); | |
3940 | 274 } |
275 break; | |
3933 | 276 |
3940 | 277 case '.': |
278 { | |
279 std::string nm = type_name (); | |
280 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
281 } | |
282 break; | |
3933 | 283 |
3940 | 284 default: |
285 panic_impossible (); | |
286 } | |
3933 | 287 } |
288 | |
289 return retval; | |
290 } | |
291 | |
3353 | 292 void |
293 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) | |
294 { | |
3928 | 295 if (rhs.is_cell ()) |
296 octave_base_matrix<Cell>::assign (idx, rhs.cell_value ()); | |
3353 | 297 else |
3928 | 298 octave_base_matrix<Cell>::assign (idx, Cell (rhs)); |
3353 | 299 } |
300 | |
4791 | 301 size_t |
302 octave_cell::byte_size (void) const | |
303 { | |
304 size_t retval = 0; | |
305 | |
5275 | 306 for (octave_idx_type i = 0; i < numel (); i++) |
4791 | 307 retval += matrix(i).byte_size (); |
308 | |
309 return retval; | |
310 } | |
311 | |
3933 | 312 octave_value_list |
313 octave_cell::list_value (void) const | |
314 { | |
315 octave_value_list retval; | |
316 | |
5275 | 317 octave_idx_type nr = rows (); |
318 octave_idx_type nc = columns (); | |
3933 | 319 |
320 if (nr == 1 && nc > 0) | |
321 { | |
322 retval.resize (nc); | |
323 | |
5275 | 324 for (octave_idx_type i = 0; i < nc; i++) |
3933 | 325 retval(i) = matrix(0,i); |
326 } | |
327 else if (nc == 1 && nr > 0) | |
328 { | |
329 retval.resize (nr); | |
330 | |
5275 | 331 for (octave_idx_type i = 0; i < nr; i++) |
3933 | 332 retval(i) = matrix(i,0); |
333 } | |
334 else | |
335 error ("invalid conversion from cell array to list"); | |
336 | |
337 return retval; | |
338 } | |
339 | |
4243 | 340 string_vector |
5715 | 341 octave_cell::all_strings (bool pad) const |
4243 | 342 { |
4358 | 343 string_vector retval; |
344 | |
7285 | 345 octave_idx_type nel = numel (); |
4243 | 346 |
4358 | 347 int n_elts = 0; |
348 | |
5275 | 349 octave_idx_type max_len = 0; |
4358 | 350 |
7285 | 351 for (octave_idx_type i = 0; i < nel; i++) |
4358 | 352 { |
7285 | 353 string_vector s = matrix(i).all_strings (); |
354 | |
355 if (error_state) | |
356 return retval; | |
4358 | 357 |
7285 | 358 octave_idx_type s_len = s.length (); |
4358 | 359 |
7285 | 360 n_elts += s_len ? s_len : 1; |
4358 | 361 |
7285 | 362 octave_idx_type s_max_len = s.max_length (); |
4358 | 363 |
7285 | 364 if (s_max_len > max_len) |
365 max_len = s_max_len; | |
4358 | 366 } |
367 | |
368 retval.resize (n_elts); | |
4243 | 369 |
5275 | 370 octave_idx_type k = 0; |
4243 | 371 |
7285 | 372 for (octave_idx_type i = 0; i < nel; i++) |
4243 | 373 { |
7285 | 374 string_vector s = matrix(i).all_strings (); |
375 | |
376 octave_idx_type s_len = s.length (); | |
4358 | 377 |
7285 | 378 if (s_len) |
379 { | |
380 for (octave_idx_type j = 0; j < s_len; j++) | |
5715 | 381 { |
7285 | 382 std::string t = s[j]; |
5715 | 383 int t_len = t.length (); |
384 | |
385 if (pad && max_len > t_len) | |
386 t += std::string (max_len - t_len, ' '); | |
387 | |
388 retval[k++] = t; | |
389 } | |
4243 | 390 } |
7285 | 391 else if (pad) |
392 retval[k++] = std::string (max_len, ' '); | |
393 else | |
394 retval[k++] = std::string (); | |
4243 | 395 } |
396 | |
397 return retval; | |
398 } | |
399 | |
4604 | 400 bool |
401 octave_cell::print_as_scalar (void) const | |
402 { | |
403 return (ndims () > 2 || numel () == 0); | |
404 } | |
405 | |
3933 | 406 void |
407 octave_cell::print (std::ostream& os, bool) const | |
408 { | |
409 print_raw (os); | |
410 } | |
411 | |
412 void | |
413 octave_cell::print_raw (std::ostream& os, bool) const | |
414 { | |
4587 | 415 int nd = matrix.ndims (); |
4513 | 416 |
4587 | 417 if (nd == 2) |
4513 | 418 { |
5275 | 419 octave_idx_type nr = rows (); |
420 octave_idx_type nc = columns (); | |
4513 | 421 |
422 if (nr > 0 && nc > 0) | |
423 { | |
424 indent (os); | |
425 os << "{"; | |
426 newline (os); | |
427 | |
428 increment_indent_level (); | |
429 | |
5275 | 430 for (octave_idx_type j = 0; j < nc; j++) |
4513 | 431 { |
5275 | 432 for (octave_idx_type i = 0; i < nr; i++) |
4513 | 433 { |
434 OCTAVE_QUIT; | |
435 | |
5765 | 436 std::ostringstream buf; |
437 buf << "[" << i+1 << "," << j+1 << "]"; | |
3933 | 438 |
4513 | 439 octave_value val = matrix(i,j); |
440 | |
5765 | 441 val.print_with_name (os, buf.str ()); |
4513 | 442 } |
443 } | |
444 | |
445 decrement_indent_level (); | |
446 | |
447 indent (os); | |
448 os << "}"; | |
449 newline (os); | |
450 } | |
451 else | |
452 { | |
453 os << "{}"; | |
5360 | 454 if (Vprint_empty_dimensions) |
4513 | 455 os << "(" << nr << "x" << nc << ")"; |
456 os << "\n"; | |
457 } | |
458 } | |
459 else | |
3933 | 460 { |
461 indent (os); | |
4513 | 462 dim_vector dv = matrix.dims (); |
4587 | 463 os << "{" << dv.str () << " Cell Array}"; |
3933 | 464 newline (os); |
465 } | |
466 } | |
467 | |
4687 | 468 #define CELL_ELT_TAG "<cell-element>" |
469 | |
470 bool | |
6974 | 471 octave_cell::save_ascii (std::ostream& os) |
4687 | 472 { |
473 dim_vector d = dims (); | |
474 if (d.length () > 2) | |
475 { | |
476 os << "# ndims: " << d.length () << "\n"; | |
477 | |
5275 | 478 for (int i = 0; i < d.length (); i++) |
4687 | 479 os << " " << d (i); |
480 os << "\n"; | |
481 | |
482 Cell tmp = cell_value (); | |
483 | |
5275 | 484 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 485 { |
486 octave_value o_val = tmp.elem (i); | |
487 | |
488 // Recurse to print sub-value. | |
6974 | 489 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 490 |
491 if (! b) | |
492 return os; | |
493 } | |
494 } | |
495 else | |
496 { | |
497 // Keep this case, rather than use generic code above for backward | |
498 // compatiability. Makes load_ascii much more complex!! | |
499 os << "# rows: " << rows () << "\n" | |
500 << "# columns: " << columns () << "\n"; | |
501 | |
502 Cell tmp = cell_value (); | |
503 | |
5275 | 504 for (octave_idx_type j = 0; j < tmp.cols (); j++) |
4687 | 505 { |
5275 | 506 for (octave_idx_type i = 0; i < tmp.rows (); i++) |
4687 | 507 { |
508 octave_value o_val = tmp.elem (i, j); | |
509 | |
510 // Recurse to print sub-value. | |
6974 | 511 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 512 |
513 if (! b) | |
514 return os; | |
515 } | |
516 | |
517 os << "\n"; | |
518 } | |
519 } | |
520 | |
521 return true; | |
522 } | |
523 | |
524 bool | |
525 octave_cell::load_ascii (std::istream& is) | |
526 { | |
527 bool success = true; | |
5099 | 528 |
529 string_vector keywords(2); | |
4687 | 530 |
5099 | 531 keywords[0] = "ndims"; |
532 keywords[1] = "rows"; | |
533 | |
534 std::string kw; | |
5275 | 535 octave_idx_type val = 0; |
5099 | 536 |
537 if (extract_keyword (is, keywords, kw, val, true)) | |
4687 | 538 { |
5099 | 539 if (kw == "ndims") |
4687 | 540 { |
5275 | 541 int mdims = static_cast<int> (val); |
4687 | 542 |
5099 | 543 if (mdims >= 0) |
544 { | |
545 dim_vector dv; | |
546 dv.resize (mdims); | |
547 | |
548 for (int i = 0; i < mdims; i++) | |
549 is >> dv(i); | |
4687 | 550 |
5099 | 551 Cell tmp(dv); |
4687 | 552 |
5275 | 553 for (octave_idx_type i = 0; i < dv.numel (); i++) |
5099 | 554 { |
555 octave_value t2; | |
556 bool dummy; | |
557 | |
558 // recurse to read cell elements | |
559 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 560 dummy, t2, i); |
4687 | 561 |
5099 | 562 if (nm == CELL_ELT_TAG) |
563 { | |
564 if (is) | |
565 tmp.elem (i) = t2; | |
566 } | |
567 else | |
568 { | |
569 error ("load: cell array element had unexpected name"); | |
570 success = false; | |
571 break; | |
572 } | |
573 } | |
4687 | 574 |
5099 | 575 if (is) |
576 matrix = tmp; | |
4687 | 577 else |
578 { | |
5099 | 579 error ("load: failed to load matrix constant"); |
4687 | 580 success = false; |
581 } | |
582 } | |
583 else | |
584 { | |
5099 | 585 error ("load: failed to extract number of rows and columns"); |
586 success = false; | |
587 } | |
588 } | |
589 else if (kw == "rows") | |
590 { | |
5275 | 591 octave_idx_type nr = val; |
592 octave_idx_type nc = 0; | |
5099 | 593 |
594 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) | |
595 { | |
596 if (nr > 0 && nc > 0) | |
597 { | |
598 Cell tmp (nr, nc); | |
599 | |
5275 | 600 for (octave_idx_type j = 0; j < nc; j++) |
5099 | 601 { |
5275 | 602 for (octave_idx_type i = 0; i < nr; i++) |
5099 | 603 { |
604 octave_value t2; | |
605 bool dummy; | |
606 | |
607 // recurse to read cell elements | |
608 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 609 dummy, t2, i); |
5099 | 610 |
611 if (nm == CELL_ELT_TAG) | |
612 { | |
613 if (is) | |
614 tmp.elem (i, j) = t2; | |
615 } | |
616 else | |
617 { | |
618 error ("load: cell array element had unexpected name"); | |
619 success = false; | |
620 goto cell_read_error; | |
621 } | |
622 } | |
623 } | |
624 | |
625 cell_read_error: | |
626 | |
627 if (is) | |
628 matrix = tmp; | |
629 else | |
630 { | |
631 error ("load: failed to load cell element"); | |
632 success = false; | |
633 } | |
634 } | |
635 else if (nr == 0 || nc == 0) | |
636 matrix = Cell (nr, nc); | |
637 else | |
638 panic_impossible (); | |
639 } | |
640 else | |
641 { | |
642 error ("load: failed to extract number of rows and columns for cell array"); | |
4687 | 643 success = false; |
644 } | |
645 } | |
646 else | |
5099 | 647 panic_impossible (); |
4687 | 648 } |
649 else | |
650 { | |
5099 | 651 error ("load: failed to extract number of rows and columns"); |
652 success = false; | |
4687 | 653 } |
654 | |
655 return success; | |
656 } | |
657 | |
658 bool | |
659 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) | |
660 { | |
661 dim_vector d = dims (); | |
662 if (d.length () < 1) | |
663 return false; | |
664 | |
665 // Use negative value for ndims | |
5828 | 666 int32_t di = - d.length(); |
5760 | 667 os.write (reinterpret_cast<char *> (&di), 4); |
5275 | 668 for (int i = 0; i < d.length (); i++) |
4687 | 669 { |
670 di = d(i); | |
5760 | 671 os.write (reinterpret_cast<char *> (&di), 4); |
4687 | 672 } |
673 | |
674 Cell tmp = cell_value (); | |
675 | |
5275 | 676 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 677 { |
678 octave_value o_val = tmp.elem (i); | |
679 | |
680 // Recurse to print sub-value. | |
681 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, | |
682 save_as_floats); | |
683 | |
684 if (! b) | |
685 return false; | |
686 } | |
687 | |
688 return true; | |
689 } | |
690 | |
691 bool | |
692 octave_cell::load_binary (std::istream& is, bool swap, | |
693 oct_mach_info::float_format fmt) | |
694 { | |
695 bool success = true; | |
5828 | 696 int32_t mdims; |
5760 | 697 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687 | 698 return false; |
699 if (swap) | |
4944 | 700 swap_bytes<4> (&mdims); |
4687 | 701 if (mdims >= 0) |
702 return false; | |
703 | |
704 mdims = -mdims; | |
5828 | 705 int32_t di; |
4687 | 706 dim_vector dv; |
707 dv.resize (mdims); | |
708 | |
709 for (int i = 0; i < mdims; i++) | |
710 { | |
5760 | 711 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687 | 712 return false; |
713 if (swap) | |
4944 | 714 swap_bytes<4> (&di); |
4687 | 715 dv(i) = di; |
716 } | |
717 | |
5157 | 718 // Convert an array with a single dimension to be a row vector. |
719 // Octave should never write files like this, other software | |
720 // might. | |
721 | |
722 if (mdims == 1) | |
723 { | |
724 mdims = 2; | |
725 dv.resize (mdims); | |
726 dv(1) = dv(0); | |
727 dv(0) = 1; | |
728 } | |
729 | |
5275 | 730 octave_idx_type nel = dv.numel (); |
4687 | 731 Cell tmp(dv); |
732 | |
5275 | 733 for (octave_idx_type i = 0; i < nel; i++) |
4687 | 734 { |
735 octave_value t2; | |
736 bool dummy; | |
737 std::string doc; | |
738 | |
739 // recurse to read cell elements | |
740 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
741 dummy, t2, doc); | |
742 | |
743 if (nm == CELL_ELT_TAG) | |
744 { | |
745 if (is) | |
746 tmp.elem (i) = t2; | |
747 } | |
748 else | |
749 { | |
750 error ("load: cell array element had unexpected name"); | |
751 success = false; | |
752 break; | |
753 } | |
754 } | |
755 | |
756 if (is) | |
757 matrix = tmp; | |
758 else | |
759 { | |
760 error ("load: failed to load matrix constant"); | |
761 success = false; | |
762 } | |
763 | |
764 return success; | |
765 } | |
766 | |
767 #if defined (HAVE_HDF5) | |
4815 | 768 |
4687 | 769 bool |
770 octave_cell::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
771 { | |
4814 | 772 dim_vector dv = dims (); |
4837 | 773 int empty = save_hdf5_empty (loc_id, name, dv); |
774 if (empty) | |
775 return (empty > 0); | |
776 | |
4815 | 777 hsize_t rank = dv.length (); |
4687 | 778 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
779 | |
780 data_hid = H5Gcreate (loc_id, name, 0); | |
4815 | 781 |
782 if (data_hid < 0) | |
783 return false; | |
4687 | 784 |
4814 | 785 // Have to save cell array shape, since can't have a |
786 // dataset of groups.... | |
4815 | 787 |
788 space_hid = H5Screate_simple (1, &rank, 0); | |
789 | |
4687 | 790 if (space_hid < 0) |
791 { | |
792 H5Gclose (data_hid); | |
793 return false; | |
794 } | |
795 | |
5351 | 796 OCTAVE_LOCAL_BUFFER (octave_idx_type, hdims, rank); |
4814 | 797 |
798 // Octave uses column-major, while HDF5 uses row-major ordering | |
4933 | 799 for (hsize_t i = 0; i < rank; i++) |
4815 | 800 hdims[i] = dv(rank-i-1); |
4814 | 801 |
5351 | 802 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
4687 | 803 H5P_DEFAULT); |
804 if (size_hid < 0) | |
805 { | |
806 H5Sclose (space_hid); | |
807 H5Gclose (data_hid); | |
808 return false; | |
809 } | |
810 | |
6276 | 811 if (H5Dwrite (size_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
812 H5P_DEFAULT, hdims) < 0) | |
4687 | 813 { |
814 H5Dclose (size_hid); | |
815 H5Sclose (space_hid); | |
816 H5Gclose (data_hid); | |
817 return false; | |
818 } | |
4815 | 819 |
4687 | 820 H5Dclose (size_hid); |
821 H5Sclose (space_hid); | |
822 | |
4815 | 823 // Recursively add each element of the cell to this group. |
824 | |
4687 | 825 Cell tmp = cell_value (); |
5850 | 826 |
827 octave_idx_type nel = dv.numel (); | |
828 | |
829 for (octave_idx_type i = 0; i < nel; i++) | |
4687 | 830 { |
5765 | 831 std::ostringstream buf; |
6276 | 832 int digits = static_cast<int> (floor (log10 (static_cast<double> (nel)) + 1.0)); |
5850 | 833 buf << "_" << std::setw (digits) << std::setfill ('0') << i; |
5765 | 834 std::string s = buf.str (); |
4687 | 835 |
5850 | 836 if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, |
837 save_as_floats)) | |
4814 | 838 { |
839 H5Gclose (data_hid); | |
840 return false; | |
4687 | 841 } |
842 } | |
843 | |
844 H5Gclose (data_hid); | |
4815 | 845 |
4687 | 846 return true; |
847 } | |
848 | |
849 bool | |
850 octave_cell::load_hdf5 (hid_t loc_id, const char *name, | |
851 bool have_h5giterate_bug) | |
852 { | |
853 bool retval = false; | |
4837 | 854 |
855 dim_vector dv; | |
856 int empty = load_hdf5_empty (loc_id, name, dv); | |
857 if (empty > 0) | |
858 matrix.resize(dv); | |
859 if (empty) | |
860 return (empty > 0); | |
861 | |
4687 | 862 hid_t group_id = H5Gopen (loc_id, name); |
863 | |
864 if (group_id < 0) | |
865 return false; | |
866 | |
4814 | 867 hid_t data_hid = H5Dopen (group_id, "dims"); |
4687 | 868 hid_t space_hid = H5Dget_space (data_hid); |
869 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
4814 | 870 if (rank != 1) |
4687 | 871 { |
4837 | 872 H5Dclose (data_hid); |
873 H5Gclose (group_id); | |
4687 | 874 return false; |
875 } | |
876 | |
4814 | 877 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
878 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
4815 | 879 |
4814 | 880 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
4687 | 881 |
4815 | 882 // Octave uses column-major, while HDF5 uses row-major ordering. |
883 | |
4814 | 884 dv.resize (hdims[0]); |
4815 | 885 |
5351 | 886 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); |
4814 | 887 |
5351 | 888 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
4815 | 889 H5P_DEFAULT, tmp) < 0) |
4687 | 890 { |
4837 | 891 H5Dclose (data_hid); |
892 H5Gclose (group_id); | |
4687 | 893 return false; |
894 } | |
4815 | 895 |
4687 | 896 H5Dclose (data_hid); |
897 H5Gclose (group_id); | |
898 | |
4815 | 899 for (hsize_t i = 0, j = hdims[0] - 1; i < hdims[0]; i++, j--) |
4814 | 900 dv(j) = tmp[i]; |
901 | |
4687 | 902 hdf5_callback_data dsub; |
903 | |
904 herr_t retval2 = -1; | |
4815 | 905 |
4814 | 906 Cell m (dv); |
4815 | 907 |
4687 | 908 int current_item = 0; |
4815 | 909 |
4687 | 910 if (have_h5giterate_bug) |
4815 | 911 current_item = 1; // Skip dims items in group. |
4687 | 912 |
4696 | 913 #ifdef HAVE_H5GGET_NUM_OBJS |
914 hsize_t num_obj = 0; | |
5060 | 915 group_id = H5Gopen (loc_id, name); |
916 H5Gget_num_objs (group_id, &num_obj); | |
917 H5Gclose (group_id); | |
4696 | 918 #endif |
919 | |
5275 | 920 for (octave_idx_type i = 0; i < dv.numel (); i++) |
4687 | 921 { |
4696 | 922 |
923 #ifdef HAVE_H5GGET_NUM_OBJS | |
4814 | 924 if (current_item >= static_cast<int> (num_obj)) |
925 retval2 = -1; | |
926 else | |
4696 | 927 #endif |
4814 | 928 retval2 = H5Giterate (loc_id, name, ¤t_item, |
929 hdf5_read_next_data, &dsub); | |
930 | |
4687 | 931 if (retval2 <= 0) |
932 break; | |
4814 | 933 |
934 octave_value ov = dsub.tc; | |
935 m.elem (i) = ov; | |
936 | |
937 if (have_h5giterate_bug) | |
4815 | 938 current_item++; // H5Giterate returned the last index processed. |
4814 | 939 |
4687 | 940 } |
941 | |
942 if (retval2 >= 0) | |
943 { | |
944 matrix = m; | |
945 retval = true; | |
946 } | |
947 | |
948 return retval; | |
949 } | |
4815 | 950 |
4687 | 951 #endif |
952 | |
3354 | 953 DEFUN (iscell, args, , |
3448 | 954 "-*- texinfo -*-\n\ |
955 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ | |
956 Return true if @var{x} is a cell array object. Otherwise, return\n\ | |
957 false.\n\ | |
958 @end deftypefn") | |
3354 | 959 { |
960 octave_value retval; | |
961 | |
962 if (args.length () == 1) | |
963 retval = args(0).is_cell (); | |
964 else | |
5823 | 965 print_usage (); |
3354 | 966 |
967 return retval; | |
968 } | |
969 | |
970 DEFUN (cell, args, , | |
3448 | 971 "-*- texinfo -*-\n\ |
972 @deftypefn {Built-in Function} {} cell (@var{x})\n\ | |
973 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ | |
974 Create a new cell array object. If invoked with a single scalar\n\ | |
975 argument, @code{cell} returns a square cell array with the dimension\n\ | |
976 specified. If you supply two scalar arguments, @code{cell} takes\n\ | |
977 them to be the number of rows and columns. If given a vector with two\n\ | |
978 elements, @code{cell} uses the values of the elements as the number of\n\ | |
979 rows and columns, respectively.\n\ | |
980 @end deftypefn") | |
3354 | 981 { |
982 octave_value retval; | |
983 | |
984 int nargin = args.length (); | |
985 | |
4563 | 986 dim_vector dims; |
987 | |
3354 | 988 switch (nargin) |
989 { | |
4563 | 990 case 0: |
991 dims = dim_vector (0, 0); | |
3354 | 992 break; |
993 | |
4563 | 994 case 1: |
995 get_dimensions (args(0), "cell", dims); | |
3354 | 996 break; |
997 | |
998 default: | |
4563 | 999 { |
1000 dims.resize (nargin); | |
1001 | |
1002 for (int i = 0; i < nargin; i++) | |
1003 { | |
1004 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); | |
1005 | |
1006 if (error_state) | |
1007 { | |
1008 error ("cell: expecting scalar arguments"); | |
1009 break; | |
1010 } | |
1011 } | |
1012 } | |
3354 | 1013 break; |
1014 } | |
1015 | |
4563 | 1016 if (! error_state) |
1017 { | |
1018 int ndim = dims.length (); | |
1019 | |
1020 check_dimensions (dims, "cell"); | |
1021 | |
1022 if (! error_state) | |
1023 { | |
1024 switch (ndim) | |
1025 { | |
1026 case 1: | |
1027 retval = Cell (dims(0), dims(0), Matrix ()); | |
1028 break; | |
1029 | |
1030 default: | |
1031 retval = Cell (dims, Matrix ()); | |
1032 break; | |
1033 } | |
1034 } | |
1035 } | |
1036 | |
3354 | 1037 return retval; |
1038 } | |
1039 | |
4610 | 1040 DEFUN (iscellstr, args, , |
1041 "-*- texinfo -*-\n\ | |
1042 @deftypefn {Built-in Function} {} iscellstr (@var{cell})\n\ | |
1043 Return true if every element of the cell array @var{cell} is a\n\ | |
1044 character string\n\ | |
1045 @end deftypefn") | |
1046 { | |
4699 | 1047 octave_value retval; |
4610 | 1048 |
1049 if (args.length () == 1) | |
6116 | 1050 retval = args(0).is_cellstr (); |
4610 | 1051 else |
5823 | 1052 print_usage (); |
4610 | 1053 |
1054 return retval; | |
1055 } | |
1056 | |
4817 | 1057 // Note that since Fcellstr calls Fiscellstr, we need to have |
1058 // Fiscellstr defined first (to provide a declaration) and also we | |
1059 // should keep it in the same file (so we don't have to provide a | |
1060 // declaration) and so we don't have to use feval to call it. | |
1061 | |
1062 DEFUN (cellstr, args, , | |
1063 "-*- texinfo -*-\n\ | |
1064 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ | |
1065 Create a new cell array object from the elements of the string\n\ | |
1066 array @var{string}.\n\ | |
1067 @end deftypefn") | |
1068 { | |
1069 octave_value retval; | |
1070 | |
1071 if (args.length () == 1) | |
1072 { | |
1073 octave_value_list tmp = Fiscellstr (args, 1); | |
1074 | |
1075 if (tmp(0).is_true ()) | |
1076 retval = args(0); | |
1077 else | |
1078 { | |
1079 string_vector s = args(0).all_strings (); | |
1080 | |
1081 if (! error_state) | |
11779 | 1082 retval = (s.is_empty () |
1083 ? Cell (octave_value (std::string ())) | |
1084 : Cell (s, true)); | |
4817 | 1085 else |
1086 error ("cellstr: expecting argument to be a 2-d character array"); | |
1087 } | |
1088 } | |
1089 else | |
5823 | 1090 print_usage (); |
4817 | 1091 |
1092 return retval; | |
1093 } | |
1094 | |
4762 | 1095 DEFUN (struct2cell, args, , |
1096 "-*- texinfo -*-\n\ | |
1097 @deftypefn {Built-in Function} {} struct2cell (@var{S})\n\ | |
1098 Create a new cell array from the objects stored in the struct object.\n\ | |
4764 | 1099 If @var{f} is the number of fields in the structure, the resulting\n\ |
1100 cell array will have a dimension vector corresponding to\n\ | |
1101 @code{[@var{F} size(@var{S})]}.\n\ | |
5642 | 1102 @seealso{cell2struct, fieldnames}\n\ |
1103 @end deftypefn") | |
4762 | 1104 { |
1105 octave_value retval; | |
4764 | 1106 |
4762 | 1107 int nargin = args.length (); |
4764 | 1108 |
4762 | 1109 if (nargin == 1) |
1110 { | |
4764 | 1111 Octave_map m = args(0).map_value (); |
1112 | |
4762 | 1113 if (! error_state) |
1114 { | |
1115 dim_vector m_dv = m.dims (); | |
4764 | 1116 |
4762 | 1117 string_vector keys = m.keys (); |
4764 | 1118 |
11770
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1119 octave_idx_type num_fields = keys.length (); |
4764 | 1120 |
4762 | 1121 // The resulting dim_vector should have dimensions: |
1122 // [numel(fields) size(struct)] | |
4764 | 1123 |
4762 | 1124 dim_vector result_dv; |
4764 | 1125 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. |
1126 | |
11770
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1127 result_dv(0) = num_fields; |
4762 | 1128 |
1129 for (int i = 1; i < result_dv.length (); i++) | |
1130 result_dv(i) = m_dv(i-1); | |
4764 | 1131 |
4762 | 1132 Cell c (result_dv); |
4764 | 1133 |
11770
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1134 octave_idx_type n_elts = m.numel (); |
4764 | 1135 |
11770
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1136 for (octave_idx_type j = 0; j < num_fields; j++) |
4762 | 1137 { |
11770
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1138 octave_idx_type k = j; |
4764 | 1139 |
11770
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1140 const Cell vals = m.contents (keys(j)); |
4764 | 1141 |
11770
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1142 for (octave_idx_type i = 0; i < n_elts; i++) |
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1143 { |
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1144 c(k) = vals(i); |
1a7ef7a48be1
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7285
diff
changeset
|
1145 k += num_fields; |
4762 | 1146 } |
1147 } | |
1148 | |
1149 retval = c; | |
1150 } | |
1151 else | |
1152 error ("struct2cell: expecting argument to be a cell array"); | |
1153 } | |
1154 else | |
5823 | 1155 print_usage (); |
4764 | 1156 |
4762 | 1157 return retval; |
1158 } | |
1159 | |
5900 | 1160 mxArray * |
1161 octave_cell::as_mxArray (void) const | |
1162 { | |
1163 mxArray *retval = new mxArray (dims ()); | |
1164 | |
1165 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1166 | |
6686 | 1167 mwSize nel = numel (); |
5900 | 1168 |
1169 const octave_value *p = matrix.data (); | |
1170 | |
6686 | 1171 for (mwIndex i = 0; i < nel; i++) |
5900 | 1172 elts[i] = new mxArray (p[i]); |
1173 | |
1174 return retval; | |
1175 } | |
1176 | |
3353 | 1177 /* |
1178 ;;; Local Variables: *** | |
1179 ;;; mode: C++ *** | |
1180 ;;; End: *** | |
1181 */ |