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