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