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