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