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