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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2376
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2376
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3503
|
27 #include <iostream> |
4726
|
28 #include <vector> |
2901
|
29 |
4944
|
30 #include "data-conv.h" |
2376
|
31 #include "lo-ieee.h" |
4944
|
32 #include "mach-info.h" |
2376
|
33 #include "mx-base.h" |
|
34 |
5758
|
35 #include "defun.h" |
|
36 #include "byte-swap.h" |
|
37 #include "gripes.h" |
|
38 #include "ls-oct-ascii.h" |
|
39 #include "ls-hdf5.h" |
|
40 #include "ls-utils.h" |
2407
|
41 #include "oct-obj.h" |
4944
|
42 #include "oct-stream.h" |
2376
|
43 #include "ops.h" |
5033
|
44 #include "ov-scalar.h" |
2376
|
45 #include "ov-re-mat.h" |
|
46 #include "ov-str-mat.h" |
|
47 #include "pr-output.h" |
3836
|
48 #include "pt-mat.h" |
5758
|
49 #include "utils.h" |
4687
|
50 |
3219
|
51 DEFINE_OCTAVE_ALLOCATOR (octave_char_matrix_str); |
5279
|
52 DEFINE_OCTAVE_ALLOCATOR (octave_char_matrix_sq_str); |
2376
|
53 |
4612
|
54 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_str, "string", "char"); |
5279
|
55 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_sq_str, "sq_string", "char"); |
2376
|
56 |
5759
|
57 static octave_base_value * |
|
58 default_numeric_conversion_function (const octave_base_value& a) |
2376
|
59 { |
5759
|
60 octave_base_value *retval = 0; |
5033
|
61 |
2376
|
62 CAST_CONV_ARG (const octave_char_matrix_str&); |
|
63 |
4668
|
64 NDArray nda = v.array_value (true); |
3203
|
65 |
5033
|
66 if (! error_state) |
|
67 { |
|
68 if (nda.numel () == 1) |
|
69 retval = new octave_scalar (nda(0)); |
|
70 else |
|
71 retval = new octave_matrix (nda); |
|
72 } |
|
73 |
|
74 return retval; |
2376
|
75 } |
|
76 |
5759
|
77 octave_base_value::type_conv_fcn |
2376
|
78 octave_char_matrix_str::numeric_conversion_function (void) const |
|
79 { |
|
80 return default_numeric_conversion_function; |
|
81 } |
|
82 |
|
83 octave_value |
5400
|
84 octave_char_matrix_str::do_index_op_internal (const octave_value_list& idx, |
5885
|
85 bool resize_ok, char type) |
2407
|
86 { |
|
87 octave_value retval; |
|
88 |
5275
|
89 octave_idx_type len = idx.length (); |
2407
|
90 |
|
91 switch (len) |
|
92 { |
5539
|
93 case 0: |
|
94 retval = octave_value (matrix, true, type); |
2407
|
95 break; |
|
96 |
|
97 case 1: |
|
98 { |
|
99 idx_vector i = idx (0).index_vector (); |
|
100 |
5086
|
101 if (! error_state) |
|
102 retval = octave_value (charNDArray (matrix.index (i, resize_ok)), |
5400
|
103 true, type); |
2407
|
104 } |
|
105 break; |
|
106 |
5539
|
107 case 2: |
|
108 { |
|
109 idx_vector i = idx (0).index_vector (); |
|
110 idx_vector j = idx (1).index_vector (); |
|
111 |
|
112 if (! error_state) |
|
113 retval = octave_value (charNDArray (matrix.index (i, j, resize_ok)), |
|
114 true, type); |
|
115 } |
5435
|
116 break; |
|
117 |
2407
|
118 default: |
4513
|
119 { |
|
120 Array<idx_vector> idx_vec (len); |
|
121 |
5275
|
122 for (octave_idx_type i = 0; i < len; i++) |
4513
|
123 idx_vec(i) = idx(i).index_vector (); |
|
124 |
5086
|
125 if (! error_state) |
|
126 retval = octave_value (charNDArray (matrix.index (idx_vec, resize_ok)), |
5400
|
127 true, type); |
4513
|
128 } |
2407
|
129 break; |
|
130 } |
|
131 |
|
132 return retval; |
|
133 } |
|
134 |
|
135 void |
|
136 octave_char_matrix_str::assign (const octave_value_list& idx, |
|
137 const charMatrix& rhs) |
|
138 { |
5275
|
139 octave_idx_type len = idx.length (); |
2407
|
140 |
5775
|
141 // FIXME |
2571
|
142 charMatrix tmp = rhs; |
|
143 if (tmp.rows () == 1 && tmp.columns () == 0) |
|
144 tmp.resize (0, 0); |
|
145 |
5275
|
146 for (octave_idx_type i = 0; i < len; i++) |
4513
|
147 matrix.set_index (idx(i).index_vector ()); |
2407
|
148 |
4513
|
149 ::assign (matrix, tmp, Vstring_fill_char); |
2407
|
150 } |
|
151 |
5731
|
152 octave_value |
|
153 octave_char_matrix_str::resize (const dim_vector& dv, bool fill) const |
|
154 { |
|
155 charNDArray retval (matrix); |
|
156 if (fill) |
|
157 retval.resize (dv, charNDArray::resize_fill_value()); |
|
158 else |
|
159 retval.resize (dv); |
|
160 return octave_value (retval, true); |
|
161 } |
|
162 |
2376
|
163 bool |
|
164 octave_char_matrix_str::valid_as_scalar_index (void) const |
|
165 { |
|
166 bool retval = false; |
|
167 error ("octave_char_matrix_str::valid_as_scalar_index(): not implemented"); |
|
168 return retval; |
|
169 } |
|
170 |
4668
|
171 #define CHAR_MATRIX_CONV(T, INIT, TNAME, FCN) \ |
|
172 T retval INIT; \ |
|
173 \ |
|
174 if (! force_string_conv) \ |
|
175 gripe_invalid_conversion ("string", TNAME); \ |
|
176 else \ |
|
177 { \ |
5878
|
178 warning_with_id ("Octave:str-to-num", \ |
5781
|
179 "implicit conversion from %s to %s", \ |
|
180 "string", TNAME); \ |
4668
|
181 \ |
|
182 retval = octave_char_matrix::FCN (); \ |
|
183 } \ |
|
184 \ |
|
185 return retval |
|
186 |
4643
|
187 double |
|
188 octave_char_matrix_str::double_value (bool force_string_conv) const |
|
189 { |
4668
|
190 CHAR_MATRIX_CONV (double, = 0, "real scalar", double_value); |
|
191 } |
4643
|
192 |
4668
|
193 Complex |
|
194 octave_char_matrix_str::complex_value (bool force_string_conv) const |
|
195 { |
|
196 CHAR_MATRIX_CONV (Complex, = 0, "complex scalar", complex_value); |
4643
|
197 } |
|
198 |
2376
|
199 Matrix |
|
200 octave_char_matrix_str::matrix_value (bool force_string_conv) const |
|
201 { |
4668
|
202 CHAR_MATRIX_CONV (Matrix, , "real matrix", matrix_value); |
|
203 } |
|
204 |
|
205 ComplexMatrix |
|
206 octave_char_matrix_str::complex_matrix_value (bool force_string_conv) const |
|
207 { |
|
208 CHAR_MATRIX_CONV (ComplexMatrix, , "complex matrix", complex_matrix_value); |
|
209 } |
2376
|
210 |
4668
|
211 NDArray |
|
212 octave_char_matrix_str::array_value (bool force_string_conv) const |
|
213 { |
|
214 CHAR_MATRIX_CONV (NDArray, , "real N-d array", array_value); |
|
215 } |
2376
|
216 |
4668
|
217 ComplexNDArray |
|
218 octave_char_matrix_str::complex_array_value (bool force_string_conv) const |
|
219 { |
|
220 CHAR_MATRIX_CONV (ComplexNDArray, , "complex N-d array", |
|
221 complex_array_value); |
2376
|
222 } |
|
223 |
2493
|
224 string_vector |
5715
|
225 octave_char_matrix_str::all_strings (bool) const |
2376
|
226 { |
4513
|
227 string_vector retval; |
|
228 |
|
229 if (matrix.ndims () == 2) |
|
230 { |
6816
|
231 charMatrix chm = matrix.matrix_value (); |
4513
|
232 |
6816
|
233 octave_idx_type n = chm.rows (); |
2493
|
234 |
6816
|
235 retval.resize (n); |
2493
|
236 |
6816
|
237 for (octave_idx_type i = 0; i < n; i++) |
|
238 retval[i] = chm.row_as_string (i); |
4513
|
239 } |
|
240 else |
|
241 error ("invalid conversion of charNDArray to string_vector"); |
2493
|
242 |
|
243 return retval; |
2376
|
244 } |
|
245 |
3536
|
246 std::string |
4457
|
247 octave_char_matrix_str::string_value (bool) const |
2376
|
248 { |
4513
|
249 std::string retval; |
|
250 |
|
251 if (matrix.ndims () == 2) |
|
252 { |
|
253 charMatrix chm = matrix.matrix_value (); |
|
254 |
5775
|
255 retval = chm.row_as_string (0); // FIXME??? |
4513
|
256 } |
|
257 else |
|
258 error ("invalid conversion of charNDArray to string"); |
|
259 |
|
260 return retval; |
2376
|
261 } |
|
262 |
|
263 void |
3523
|
264 octave_char_matrix_str::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2376
|
265 { |
3219
|
266 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
267 current_print_indent_level (), true); |
2376
|
268 } |
|
269 |
4687
|
270 bool |
6974
|
271 octave_char_matrix_str::save_ascii (std::ostream& os) |
4687
|
272 { |
4805
|
273 dim_vector d = dims (); |
|
274 if (d.length () > 2) |
|
275 { |
|
276 charNDArray tmp = char_array_value (); |
|
277 os << "# ndims: " << d.length () << "\n"; |
|
278 for (int i=0; i < d.length (); i++) |
|
279 os << " " << d (i); |
|
280 os << "\n"; |
5760
|
281 os.write (tmp.fortran_vec (), d.numel ()); |
4805
|
282 os << "\n"; |
|
283 } |
|
284 else |
4687
|
285 { |
4805
|
286 // Keep this case, rather than use generic code above for |
|
287 // backward compatiability. Makes load_ascii much more complex!! |
|
288 charMatrix chm = char_matrix_value (); |
5275
|
289 octave_idx_type elements = chm.rows (); |
4805
|
290 os << "# elements: " << elements << "\n"; |
5275
|
291 for (octave_idx_type i = 0; i < elements; i++) |
4805
|
292 { |
|
293 unsigned len = chm.cols (); |
|
294 os << "# length: " << len << "\n"; |
|
295 std::string tstr = chm.row_as_string (i, false, true); |
|
296 const char *tmp = tstr.data (); |
|
297 if (tstr.length () > len) |
|
298 panic_impossible (); |
5760
|
299 os.write (tmp, len); |
4805
|
300 os << "\n"; |
|
301 } |
4687
|
302 } |
|
303 |
|
304 return true; |
|
305 } |
|
306 |
|
307 bool |
|
308 octave_char_matrix_str::load_ascii (std::istream& is) |
|
309 { |
|
310 bool success = true; |
5099
|
311 |
|
312 string_vector keywords(3); |
|
313 |
|
314 keywords[0] = "ndims"; |
|
315 keywords[1] = "elements"; |
|
316 keywords[2] = "length"; |
|
317 |
|
318 std::string kw; |
|
319 int val = 0; |
4687
|
320 |
5099
|
321 if (extract_keyword (is, keywords, kw, val, true)) |
4687
|
322 { |
5099
|
323 if (kw == "ndims") |
4687
|
324 { |
5099
|
325 int mdims = val; |
|
326 |
|
327 if (mdims >= 0) |
|
328 { |
|
329 dim_vector dv; |
|
330 dv.resize (mdims); |
4805
|
331 |
5099
|
332 for (int i = 0; i < mdims; i++) |
|
333 is >> dv(i); |
4687
|
334 |
6717
|
335 if (is) |
|
336 { |
|
337 charNDArray tmp(dv); |
|
338 |
|
339 if (tmp.is_empty ()) |
|
340 matrix = tmp; |
|
341 else |
|
342 { |
|
343 char *ftmp = tmp.fortran_vec (); |
5099
|
344 |
6717
|
345 // Skip the return line |
|
346 if (! is.read (ftmp, 1)) |
|
347 return false; |
4805
|
348 |
6717
|
349 if (! is.read (ftmp, dv.numel ()) || !is) |
|
350 { |
|
351 error ("load: failed to load string constant"); |
|
352 success = false; |
|
353 } |
|
354 else |
|
355 matrix = tmp; |
|
356 } |
|
357 } |
|
358 else |
5099
|
359 { |
6717
|
360 error ("load: failed to read dimensions"); |
5099
|
361 success = false; |
|
362 } |
|
363 } |
|
364 else |
4687
|
365 { |
5099
|
366 error ("load: failed to extract matrix size"); |
4805
|
367 success = false; |
4687
|
368 } |
|
369 } |
5099
|
370 else if (kw == "elements") |
4687
|
371 { |
5099
|
372 int elements = val; |
4805
|
373 |
|
374 if (elements >= 0) |
|
375 { |
5775
|
376 // FIXME -- need to be able to get max length |
4805
|
377 // before doing anything. |
4687
|
378 |
4805
|
379 charMatrix chm (elements, 0); |
|
380 int max_len = 0; |
|
381 for (int i = 0; i < elements; i++) |
|
382 { |
|
383 int len; |
|
384 if (extract_keyword (is, "length", len) && len >= 0) |
|
385 { |
6151
|
386 // Use this instead of a C-style character |
|
387 // buffer so that we can properly handle |
|
388 // embedded NUL characters. |
|
389 charMatrix tmp (1, len); |
|
390 char *ptmp = tmp.fortran_vec (); |
|
391 |
|
392 if (len > 0 && ! is.read (ptmp, len)) |
4805
|
393 { |
|
394 error ("load: failed to load string constant"); |
|
395 success = false; |
|
396 break; |
|
397 } |
|
398 else |
|
399 { |
|
400 if (len > max_len) |
|
401 { |
|
402 max_len = len; |
|
403 chm.resize (elements, max_len, 0); |
|
404 } |
6151
|
405 |
4805
|
406 chm.insert (tmp, i, 0); |
|
407 } |
|
408 } |
|
409 else |
|
410 { |
|
411 error ("load: failed to extract string length for element %d", |
|
412 i+1); |
|
413 success = false; |
|
414 } |
|
415 } |
|
416 |
|
417 if (! error_state) |
|
418 matrix = chm; |
4687
|
419 } |
|
420 else |
|
421 { |
4805
|
422 error ("load: failed to extract number of string elements"); |
|
423 success = false; |
|
424 } |
|
425 } |
5099
|
426 else if (kw == "length") |
4805
|
427 { |
5099
|
428 int len = val; |
4805
|
429 |
5099
|
430 if (len >= 0) |
4805
|
431 { |
|
432 // This is cruft for backward compatiability, |
|
433 // but relatively harmless. |
|
434 |
6151
|
435 // Use this instead of a C-style character buffer so |
|
436 // that we can properly handle embedded NUL characters. |
|
437 charMatrix tmp (1, len); |
|
438 char *ptmp = tmp.fortran_vec (); |
4805
|
439 |
6151
|
440 if (len > 0 && ! is.read (ptmp, len)) |
4805
|
441 { |
|
442 error ("load: failed to load string constant"); |
|
443 } |
4687
|
444 else |
4805
|
445 { |
|
446 if (is) |
6151
|
447 matrix = tmp; |
4805
|
448 else |
|
449 error ("load: failed to load string constant"); |
|
450 } |
4687
|
451 } |
|
452 } |
5099
|
453 else |
|
454 panic_impossible (); |
|
455 } |
|
456 else |
|
457 { |
|
458 error ("load: failed to extract number of rows and columns"); |
|
459 success = false; |
4687
|
460 } |
|
461 |
|
462 return success; |
|
463 } |
|
464 |
|
465 bool |
|
466 octave_char_matrix_str::save_binary (std::ostream& os, |
|
467 bool& /* save_as_floats */) |
|
468 { |
4805
|
469 dim_vector d = dims (); |
|
470 if (d.length() < 1) |
|
471 return false; |
|
472 |
|
473 // Use negative value for ndims to differentiate with old format!! |
5828
|
474 int32_t tmp = - d.length(); |
5760
|
475 os.write (reinterpret_cast<char *> (&tmp), 4); |
4805
|
476 for (int i=0; i < d.length (); i++) |
4687
|
477 { |
4805
|
478 tmp = d(i); |
5760
|
479 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687
|
480 } |
4805
|
481 |
|
482 charNDArray m = char_array_value (); |
|
483 os.write (m.fortran_vec (), d.numel ()); |
4687
|
484 return true; |
|
485 } |
|
486 |
|
487 bool |
|
488 octave_char_matrix_str::load_binary (std::istream& is, bool swap, |
|
489 oct_mach_info::float_format /* fmt */) |
|
490 { |
5828
|
491 int32_t elements; |
5760
|
492 if (! is.read (reinterpret_cast<char *> (&elements), 4)) |
4687
|
493 return false; |
|
494 if (swap) |
4944
|
495 swap_bytes<4> (&elements); |
4805
|
496 |
|
497 if (elements < 0) |
4687
|
498 { |
5828
|
499 int32_t mdims = - elements; |
|
500 int32_t di; |
4805
|
501 dim_vector dv; |
|
502 dv.resize (mdims); |
|
503 |
|
504 for (int i = 0; i < mdims; i++) |
|
505 { |
5760
|
506 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4805
|
507 return false; |
|
508 if (swap) |
4944
|
509 swap_bytes<4> (&di); |
4805
|
510 dv(i) = di; |
|
511 } |
|
512 |
5157
|
513 // Convert an array with a single dimension to be a row vector. |
|
514 // Octave should never write files like this, other software |
|
515 // might. |
|
516 |
|
517 if (mdims == 1) |
|
518 { |
|
519 mdims = 2; |
|
520 dv.resize (mdims); |
|
521 dv(1) = dv(0); |
|
522 dv(0) = 1; |
|
523 } |
|
524 |
4805
|
525 charNDArray m(dv); |
|
526 char *tmp = m.fortran_vec (); |
|
527 is.read (tmp, dv.numel ()); |
|
528 |
|
529 if (error_state || ! is) |
4687
|
530 return false; |
4805
|
531 matrix = m; |
|
532 } |
|
533 else |
|
534 { |
|
535 charMatrix chm (elements, 0); |
|
536 int max_len = 0; |
|
537 for (int i = 0; i < elements; i++) |
4687
|
538 { |
5828
|
539 int32_t len; |
5760
|
540 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4805
|
541 return false; |
|
542 if (swap) |
4944
|
543 swap_bytes<4> (&len); |
6151
|
544 charMatrix btmp (1, len); |
|
545 char *pbtmp = btmp.fortran_vec (); |
|
546 if (! is.read (pbtmp, len)) |
4805
|
547 return false; |
|
548 if (len > max_len) |
|
549 { |
|
550 max_len = len; |
|
551 chm.resize (elements, max_len, 0); |
|
552 } |
|
553 chm.insert (btmp, i, 0); |
4687
|
554 } |
4805
|
555 matrix = chm; |
4687
|
556 } |
|
557 return true; |
|
558 } |
|
559 |
|
560 #if defined (HAVE_HDF5) |
4944
|
561 |
4687
|
562 bool |
|
563 octave_char_matrix_str::save_hdf5 (hid_t loc_id, const char *name, |
|
564 bool /* save_as_floats */) |
|
565 { |
4837
|
566 dim_vector dv = dims (); |
|
567 int empty = save_hdf5_empty (loc_id, name, dv); |
|
568 if (empty) |
4805
|
569 return (empty > 0); |
4687
|
570 |
4837
|
571 int rank = dv.length (); |
4805
|
572 hid_t space_hid = -1, data_hid = -1; |
|
573 bool retval = true; |
|
574 charNDArray m = char_array_value (); |
|
575 |
|
576 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
4687
|
577 |
4805
|
578 // Octave uses column-major, while HDF5 uses row-major ordering |
|
579 for (int i = 0; i < rank; i++) |
4837
|
580 hdims[i] = dv (rank-i-1); |
4805
|
581 |
4815
|
582 space_hid = H5Screate_simple (rank, hdims, 0); |
4805
|
583 if (space_hid < 0) |
|
584 return false; |
4687
|
585 |
4805
|
586 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
|
587 H5P_DEFAULT); |
|
588 if (data_hid < 0) |
4687
|
589 { |
4805
|
590 H5Sclose (space_hid); |
4687
|
591 return false; |
|
592 } |
|
593 |
4837
|
594 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687
|
595 |
4837
|
596 for (int i = 0; i < dv.numel (); ++i) |
4805
|
597 s[i] = m(i); |
4687
|
598 |
4805
|
599 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
600 H5P_DEFAULT, s) >= 0; |
4687
|
601 |
|
602 H5Dclose (data_hid); |
|
603 H5Sclose (space_hid); |
4837
|
604 |
4687
|
605 return retval; |
|
606 } |
|
607 |
|
608 bool |
|
609 octave_char_matrix_str::load_hdf5 (hid_t loc_id, const char *name, |
|
610 bool /* have_h5giterate_bug */) |
|
611 { |
4837
|
612 bool retval = false; |
|
613 |
4805
|
614 dim_vector dv; |
|
615 int empty = load_hdf5_empty (loc_id, name, dv); |
|
616 if (empty > 0) |
|
617 matrix.resize(dv); |
4837
|
618 if (empty) |
|
619 return (empty > 0); |
4805
|
620 |
4687
|
621 hid_t data_hid = H5Dopen (loc_id, name); |
|
622 hid_t space_hid = H5Dget_space (data_hid); |
|
623 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
|
624 hid_t type_hid = H5Dget_type (data_hid); |
4805
|
625 hid_t type_class_hid = H5Tget_class (type_hid); |
4687
|
626 |
4805
|
627 if (type_class_hid == H5T_INTEGER) |
4687
|
628 { |
4805
|
629 if (rank < 1) |
4687
|
630 { |
|
631 H5Tclose (type_hid); |
|
632 H5Sclose (space_hid); |
|
633 H5Dclose (data_hid); |
|
634 return false; |
|
635 } |
4805
|
636 |
|
637 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
638 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
639 |
|
640 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
641 |
|
642 // Octave uses column-major, while HDF5 uses row-major ordering |
|
643 if (rank == 1) |
|
644 { |
|
645 dv.resize (2); |
|
646 dv(0) = 1; |
|
647 dv(1) = hdims[0]; |
|
648 } |
4687
|
649 else |
|
650 { |
4805
|
651 dv.resize (rank); |
4815
|
652 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4805
|
653 dv(j) = hdims[i]; |
|
654 } |
|
655 |
|
656 charNDArray m (dv); |
|
657 char *str = m.fortran_vec (); |
|
658 if (H5Dread (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
659 H5P_DEFAULT, str) >= 0) |
4805
|
660 { |
|
661 retval = true; |
|
662 matrix = m; |
|
663 } |
|
664 |
|
665 H5Tclose (type_hid); |
|
666 H5Sclose (space_hid); |
|
667 H5Dclose (data_hid); |
|
668 return true; |
|
669 } |
|
670 else |
|
671 { |
|
672 // This is cruft for backward compatiability and easy data |
|
673 // importation |
|
674 if (rank == 0) |
|
675 { |
|
676 // a single string: |
|
677 int slen = H5Tget_size (type_hid); |
|
678 if (slen < 0) |
4687
|
679 { |
|
680 H5Tclose (type_hid); |
|
681 H5Sclose (space_hid); |
|
682 H5Dclose (data_hid); |
|
683 return false; |
|
684 } |
4805
|
685 else |
|
686 { |
|
687 OCTAVE_LOCAL_BUFFER (char, s, slen); |
|
688 // create datatype for (null-terminated) string |
|
689 // to read into: |
|
690 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
691 H5Tset_size (st_id, slen); |
5760
|
692 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, s) < 0) |
4805
|
693 { |
|
694 H5Tclose (st_id); |
|
695 H5Tclose (type_hid); |
|
696 H5Sclose (space_hid); |
|
697 H5Dclose (data_hid); |
|
698 return false; |
|
699 } |
4687
|
700 |
4805
|
701 matrix = charMatrix (s); |
4687
|
702 |
4805
|
703 H5Tclose (st_id); |
|
704 H5Tclose (type_hid); |
|
705 H5Sclose (space_hid); |
|
706 H5Dclose (data_hid); |
|
707 return true; |
|
708 } |
4687
|
709 } |
4805
|
710 else if (rank == 1) |
|
711 { |
|
712 // string vector |
|
713 hsize_t elements, maxdim; |
|
714 H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); |
|
715 int slen = H5Tget_size (type_hid); |
|
716 if (slen < 0) |
|
717 { |
|
718 H5Tclose (type_hid); |
|
719 H5Sclose (space_hid); |
|
720 H5Dclose (data_hid); |
|
721 return false; |
|
722 } |
|
723 else |
|
724 { |
|
725 // hdf5 string arrays store strings of all the |
|
726 // same physical length (I think), which is |
|
727 // slightly wasteful, but oh well. |
|
728 |
|
729 OCTAVE_LOCAL_BUFFER (char, s, elements * slen); |
|
730 |
|
731 // create datatype for (null-terminated) string |
|
732 // to read into: |
|
733 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
734 H5Tset_size (st_id, slen); |
|
735 |
5760
|
736 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, s) < 0) |
4805
|
737 { |
|
738 H5Tclose (st_id); |
|
739 H5Tclose (type_hid); |
|
740 H5Sclose (space_hid); |
|
741 H5Dclose (data_hid); |
|
742 return false; |
|
743 } |
|
744 |
|
745 charMatrix chm (elements, slen - 1); |
|
746 for (hsize_t i = 0; i < elements; ++i) |
|
747 { |
|
748 chm.insert (s + i*slen, i, 0); |
|
749 } |
|
750 |
|
751 matrix = chm; |
|
752 |
|
753 H5Tclose (st_id); |
|
754 H5Tclose (type_hid); |
|
755 H5Sclose (space_hid); |
|
756 H5Dclose (data_hid); |
|
757 return true; |
|
758 } |
|
759 } |
|
760 else |
4687
|
761 { |
|
762 H5Tclose (type_hid); |
|
763 H5Sclose (space_hid); |
|
764 H5Dclose (data_hid); |
|
765 return false; |
|
766 } |
|
767 } |
4837
|
768 |
|
769 return retval; |
4687
|
770 } |
4944
|
771 |
4687
|
772 #endif |
|
773 |
2376
|
774 /* |
|
775 ;;; Local Variables: *** |
|
776 ;;; mode: C++ *** |
|
777 ;;; End: *** |
|
778 */ |