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 { |
|
297 bool success = true; |
5099
|
298 |
|
299 string_vector keywords(3); |
|
300 |
|
301 keywords[0] = "ndims"; |
|
302 keywords[1] = "elements"; |
|
303 keywords[2] = "length"; |
|
304 |
|
305 std::string kw; |
|
306 int val = 0; |
4687
|
307 |
5099
|
308 if (extract_keyword (is, keywords, kw, val, true)) |
4687
|
309 { |
5099
|
310 if (kw == "ndims") |
4687
|
311 { |
5099
|
312 int mdims = val; |
|
313 |
|
314 if (mdims >= 0) |
|
315 { |
|
316 dim_vector dv; |
|
317 dv.resize (mdims); |
4805
|
318 |
5099
|
319 for (int i = 0; i < mdims; i++) |
|
320 is >> dv(i); |
4687
|
321 |
5099
|
322 charNDArray tmp(dv); |
|
323 char *ftmp = tmp.fortran_vec (); |
|
324 |
|
325 // Skip the return line |
|
326 if (! is.read (ftmp, 1)) |
|
327 return false; |
4805
|
328 |
5099
|
329 if (! is.read (ftmp, dv.numel ()) || !is) |
|
330 { |
|
331 error ("load: failed to load string constant"); |
|
332 success = false; |
|
333 } |
|
334 else |
|
335 matrix = tmp; |
|
336 } |
|
337 else |
4687
|
338 { |
5099
|
339 error ("load: failed to extract matrix size"); |
4805
|
340 success = false; |
4687
|
341 } |
|
342 } |
5099
|
343 else if (kw == "elements") |
4687
|
344 { |
5099
|
345 int elements = val; |
4805
|
346 |
|
347 if (elements >= 0) |
|
348 { |
|
349 // XXX FIXME XXX -- need to be able to get max length |
|
350 // before doing anything. |
4687
|
351 |
4805
|
352 charMatrix chm (elements, 0); |
|
353 int max_len = 0; |
|
354 for (int i = 0; i < elements; i++) |
|
355 { |
|
356 int len; |
|
357 if (extract_keyword (is, "length", len) && len >= 0) |
|
358 { |
|
359 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
360 |
|
361 if (len > 0 && ! |
|
362 is.read (X_CAST (char *, tmp), len)) |
|
363 { |
|
364 error ("load: failed to load string constant"); |
|
365 success = false; |
|
366 break; |
|
367 } |
|
368 else |
|
369 { |
|
370 tmp [len] = '\0'; |
|
371 if (len > max_len) |
|
372 { |
|
373 max_len = len; |
|
374 chm.resize (elements, max_len, 0); |
|
375 } |
|
376 chm.insert (tmp, i, 0); |
|
377 } |
|
378 } |
|
379 else |
|
380 { |
|
381 error ("load: failed to extract string length for element %d", |
|
382 i+1); |
|
383 success = false; |
|
384 } |
|
385 } |
|
386 |
|
387 if (! error_state) |
|
388 matrix = chm; |
4687
|
389 } |
|
390 else |
|
391 { |
4805
|
392 error ("load: failed to extract number of string elements"); |
|
393 success = false; |
|
394 } |
|
395 } |
5099
|
396 else if (kw == "length") |
4805
|
397 { |
5099
|
398 int len = val; |
4805
|
399 |
5099
|
400 if (len >= 0) |
4805
|
401 { |
|
402 // This is cruft for backward compatiability, |
|
403 // but relatively harmless. |
|
404 |
|
405 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
406 |
|
407 if (len > 0 && ! is.read (X_CAST (char *, tmp), len)) |
|
408 { |
|
409 error ("load: failed to load string constant"); |
|
410 } |
4687
|
411 else |
4805
|
412 { |
|
413 tmp [len] = '\0'; |
|
414 |
|
415 if (is) |
|
416 matrix = charMatrix (tmp); |
|
417 else |
|
418 error ("load: failed to load string constant"); |
|
419 } |
4687
|
420 } |
|
421 } |
5099
|
422 else |
|
423 panic_impossible (); |
|
424 } |
|
425 else |
|
426 { |
|
427 error ("load: failed to extract number of rows and columns"); |
|
428 success = false; |
4687
|
429 } |
|
430 |
|
431 return success; |
|
432 } |
|
433 |
|
434 bool |
|
435 octave_char_matrix_str::save_binary (std::ostream& os, |
|
436 bool& /* save_as_floats */) |
|
437 { |
4805
|
438 dim_vector d = dims (); |
|
439 if (d.length() < 1) |
|
440 return false; |
|
441 |
|
442 // Use negative value for ndims to differentiate with old format!! |
|
443 FOUR_BYTE_INT tmp = - d.length(); |
|
444 os.write (X_CAST (char *, &tmp), 4); |
|
445 for (int i=0; i < d.length (); i++) |
4687
|
446 { |
4805
|
447 tmp = d(i); |
|
448 os.write (X_CAST (char *, &tmp), 4); |
4687
|
449 } |
4805
|
450 |
|
451 charNDArray m = char_array_value (); |
|
452 os.write (m.fortran_vec (), d.numel ()); |
4687
|
453 return true; |
|
454 } |
|
455 |
|
456 bool |
|
457 octave_char_matrix_str::load_binary (std::istream& is, bool swap, |
|
458 oct_mach_info::float_format /* fmt */) |
|
459 { |
|
460 FOUR_BYTE_INT elements; |
|
461 if (! is.read (X_CAST (char *, &elements), 4)) |
|
462 return false; |
|
463 if (swap) |
4944
|
464 swap_bytes<4> (&elements); |
4805
|
465 |
|
466 if (elements < 0) |
4687
|
467 { |
4805
|
468 FOUR_BYTE_INT mdims = - elements; |
|
469 FOUR_BYTE_INT di; |
|
470 dim_vector dv; |
|
471 dv.resize (mdims); |
|
472 |
|
473 for (int i = 0; i < mdims; i++) |
|
474 { |
|
475 if (! is.read (X_CAST (char *, &di), 4)) |
|
476 return false; |
|
477 if (swap) |
4944
|
478 swap_bytes<4> (&di); |
4805
|
479 dv(i) = di; |
|
480 } |
|
481 |
|
482 charNDArray m(dv); |
|
483 char *tmp = m.fortran_vec (); |
|
484 is.read (tmp, dv.numel ()); |
|
485 |
|
486 if (error_state || ! is) |
4687
|
487 return false; |
4805
|
488 matrix = m; |
|
489 } |
|
490 else |
|
491 { |
|
492 charMatrix chm (elements, 0); |
|
493 int max_len = 0; |
|
494 for (int i = 0; i < elements; i++) |
4687
|
495 { |
4805
|
496 FOUR_BYTE_INT len; |
|
497 if (! is.read (X_CAST (char *, &len), 4)) |
|
498 return false; |
|
499 if (swap) |
4944
|
500 swap_bytes<4> (&len); |
4805
|
501 OCTAVE_LOCAL_BUFFER (char, btmp, len+1); |
|
502 if (! is.read (X_CAST (char *, btmp), len)) |
|
503 return false; |
|
504 if (len > max_len) |
|
505 { |
|
506 max_len = len; |
|
507 chm.resize (elements, max_len, 0); |
|
508 } |
|
509 btmp [len] = '\0'; |
|
510 chm.insert (btmp, i, 0); |
4687
|
511 } |
4805
|
512 matrix = chm; |
4687
|
513 } |
|
514 return true; |
|
515 } |
|
516 |
|
517 #if defined (HAVE_HDF5) |
4944
|
518 |
4687
|
519 bool |
|
520 octave_char_matrix_str::save_hdf5 (hid_t loc_id, const char *name, |
|
521 bool /* save_as_floats */) |
|
522 { |
4837
|
523 dim_vector dv = dims (); |
|
524 int empty = save_hdf5_empty (loc_id, name, dv); |
|
525 if (empty) |
4805
|
526 return (empty > 0); |
4687
|
527 |
4837
|
528 int rank = dv.length (); |
4805
|
529 hid_t space_hid = -1, data_hid = -1; |
|
530 bool retval = true; |
|
531 charNDArray m = char_array_value (); |
|
532 |
|
533 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
4687
|
534 |
4805
|
535 // Octave uses column-major, while HDF5 uses row-major ordering |
|
536 for (int i = 0; i < rank; i++) |
4837
|
537 hdims[i] = dv (rank-i-1); |
4805
|
538 |
4815
|
539 space_hid = H5Screate_simple (rank, hdims, 0); |
4805
|
540 if (space_hid < 0) |
|
541 return false; |
4687
|
542 |
4805
|
543 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
|
544 H5P_DEFAULT); |
|
545 if (data_hid < 0) |
4687
|
546 { |
4805
|
547 H5Sclose (space_hid); |
4687
|
548 return false; |
|
549 } |
|
550 |
4837
|
551 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687
|
552 |
4837
|
553 for (int i = 0; i < dv.numel (); ++i) |
4805
|
554 s[i] = m(i); |
4687
|
555 |
4805
|
556 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
557 H5P_DEFAULT, s) >= 0; |
4687
|
558 |
|
559 H5Dclose (data_hid); |
|
560 H5Sclose (space_hid); |
4837
|
561 |
4687
|
562 return retval; |
|
563 } |
|
564 |
|
565 bool |
|
566 octave_char_matrix_str::load_hdf5 (hid_t loc_id, const char *name, |
|
567 bool /* have_h5giterate_bug */) |
|
568 { |
4837
|
569 bool retval = false; |
|
570 |
4805
|
571 dim_vector dv; |
|
572 int empty = load_hdf5_empty (loc_id, name, dv); |
|
573 if (empty > 0) |
|
574 matrix.resize(dv); |
4837
|
575 if (empty) |
|
576 return (empty > 0); |
4805
|
577 |
4687
|
578 hid_t data_hid = H5Dopen (loc_id, name); |
|
579 hid_t space_hid = H5Dget_space (data_hid); |
|
580 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
|
581 hid_t type_hid = H5Dget_type (data_hid); |
4805
|
582 hid_t type_class_hid = H5Tget_class (type_hid); |
4687
|
583 |
4805
|
584 if (type_class_hid == H5T_INTEGER) |
4687
|
585 { |
4805
|
586 if (rank < 1) |
4687
|
587 { |
|
588 H5Tclose (type_hid); |
|
589 H5Sclose (space_hid); |
|
590 H5Dclose (data_hid); |
|
591 return false; |
|
592 } |
4805
|
593 |
|
594 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
595 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
596 |
|
597 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
598 |
|
599 // Octave uses column-major, while HDF5 uses row-major ordering |
|
600 if (rank == 1) |
|
601 { |
|
602 dv.resize (2); |
|
603 dv(0) = 1; |
|
604 dv(1) = hdims[0]; |
|
605 } |
4687
|
606 else |
|
607 { |
4805
|
608 dv.resize (rank); |
4815
|
609 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4805
|
610 dv(j) = hdims[i]; |
|
611 } |
|
612 |
|
613 charNDArray m (dv); |
|
614 char *str = m.fortran_vec (); |
|
615 if (H5Dread (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
616 H5P_DEFAULT, str) >= 0) |
4805
|
617 { |
|
618 retval = true; |
|
619 matrix = m; |
|
620 } |
|
621 |
|
622 H5Tclose (type_hid); |
|
623 H5Sclose (space_hid); |
|
624 H5Dclose (data_hid); |
|
625 return true; |
|
626 } |
|
627 else |
|
628 { |
|
629 // This is cruft for backward compatiability and easy data |
|
630 // importation |
|
631 if (rank == 0) |
|
632 { |
|
633 // a single string: |
|
634 int slen = H5Tget_size (type_hid); |
|
635 if (slen < 0) |
4687
|
636 { |
|
637 H5Tclose (type_hid); |
|
638 H5Sclose (space_hid); |
|
639 H5Dclose (data_hid); |
|
640 return false; |
|
641 } |
4805
|
642 else |
|
643 { |
|
644 OCTAVE_LOCAL_BUFFER (char, s, slen); |
|
645 // create datatype for (null-terminated) string |
|
646 // to read into: |
|
647 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
648 H5Tset_size (st_id, slen); |
|
649 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, |
|
650 H5P_DEFAULT, (void *) s) < 0) |
|
651 { |
|
652 H5Tclose (st_id); |
|
653 H5Tclose (type_hid); |
|
654 H5Sclose (space_hid); |
|
655 H5Dclose (data_hid); |
|
656 return false; |
|
657 } |
4687
|
658 |
4805
|
659 matrix = charMatrix (s); |
4687
|
660 |
4805
|
661 H5Tclose (st_id); |
|
662 H5Tclose (type_hid); |
|
663 H5Sclose (space_hid); |
|
664 H5Dclose (data_hid); |
|
665 return true; |
|
666 } |
4687
|
667 } |
4805
|
668 else if (rank == 1) |
|
669 { |
|
670 // string vector |
|
671 hsize_t elements, maxdim; |
|
672 H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); |
|
673 int slen = H5Tget_size (type_hid); |
|
674 if (slen < 0) |
|
675 { |
|
676 H5Tclose (type_hid); |
|
677 H5Sclose (space_hid); |
|
678 H5Dclose (data_hid); |
|
679 return false; |
|
680 } |
|
681 else |
|
682 { |
|
683 // hdf5 string arrays store strings of all the |
|
684 // same physical length (I think), which is |
|
685 // slightly wasteful, but oh well. |
|
686 |
|
687 OCTAVE_LOCAL_BUFFER (char, s, elements * slen); |
|
688 |
|
689 // create datatype for (null-terminated) string |
|
690 // to read into: |
|
691 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
692 H5Tset_size (st_id, slen); |
|
693 |
|
694 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, |
|
695 H5P_DEFAULT, (void *) s) < 0) |
|
696 { |
|
697 H5Tclose (st_id); |
|
698 H5Tclose (type_hid); |
|
699 H5Sclose (space_hid); |
|
700 H5Dclose (data_hid); |
|
701 return false; |
|
702 } |
|
703 |
|
704 charMatrix chm (elements, slen - 1); |
|
705 for (hsize_t i = 0; i < elements; ++i) |
|
706 { |
|
707 chm.insert (s + i*slen, i, 0); |
|
708 } |
|
709 |
|
710 matrix = chm; |
|
711 |
|
712 H5Tclose (st_id); |
|
713 H5Tclose (type_hid); |
|
714 H5Sclose (space_hid); |
|
715 H5Dclose (data_hid); |
|
716 return true; |
|
717 } |
|
718 } |
|
719 else |
4687
|
720 { |
|
721 H5Tclose (type_hid); |
|
722 H5Sclose (space_hid); |
|
723 H5Dclose (data_hid); |
|
724 return false; |
|
725 } |
|
726 } |
4837
|
727 |
|
728 return retval; |
4687
|
729 } |
4944
|
730 |
4687
|
731 #endif |
|
732 |
2376
|
733 /* |
|
734 ;;; Local Variables: *** |
|
735 ;;; mode: C++ *** |
|
736 ;;; End: *** |
|
737 */ |