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 |
5157
|
478 // Convert an array with a single dimension to be a row vector. |
|
479 // Octave should never write files like this, other software |
|
480 // might. |
|
481 |
|
482 if (mdims == 1) |
|
483 { |
|
484 mdims = 2; |
|
485 dv.resize (mdims); |
|
486 dv(1) = dv(0); |
|
487 dv(0) = 1; |
|
488 } |
|
489 |
4805
|
490 charNDArray m(dv); |
|
491 char *tmp = m.fortran_vec (); |
|
492 is.read (tmp, dv.numel ()); |
|
493 |
|
494 if (error_state || ! is) |
4687
|
495 return false; |
4805
|
496 matrix = m; |
|
497 } |
|
498 else |
|
499 { |
|
500 charMatrix chm (elements, 0); |
|
501 int max_len = 0; |
|
502 for (int i = 0; i < elements; i++) |
4687
|
503 { |
4805
|
504 FOUR_BYTE_INT len; |
|
505 if (! is.read (X_CAST (char *, &len), 4)) |
|
506 return false; |
|
507 if (swap) |
4944
|
508 swap_bytes<4> (&len); |
4805
|
509 OCTAVE_LOCAL_BUFFER (char, btmp, len+1); |
|
510 if (! is.read (X_CAST (char *, btmp), len)) |
|
511 return false; |
|
512 if (len > max_len) |
|
513 { |
|
514 max_len = len; |
|
515 chm.resize (elements, max_len, 0); |
|
516 } |
|
517 btmp [len] = '\0'; |
|
518 chm.insert (btmp, i, 0); |
4687
|
519 } |
4805
|
520 matrix = chm; |
4687
|
521 } |
|
522 return true; |
|
523 } |
|
524 |
|
525 #if defined (HAVE_HDF5) |
4944
|
526 |
4687
|
527 bool |
|
528 octave_char_matrix_str::save_hdf5 (hid_t loc_id, const char *name, |
|
529 bool /* save_as_floats */) |
|
530 { |
4837
|
531 dim_vector dv = dims (); |
|
532 int empty = save_hdf5_empty (loc_id, name, dv); |
|
533 if (empty) |
4805
|
534 return (empty > 0); |
4687
|
535 |
4837
|
536 int rank = dv.length (); |
4805
|
537 hid_t space_hid = -1, data_hid = -1; |
|
538 bool retval = true; |
|
539 charNDArray m = char_array_value (); |
|
540 |
|
541 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
4687
|
542 |
4805
|
543 // Octave uses column-major, while HDF5 uses row-major ordering |
|
544 for (int i = 0; i < rank; i++) |
4837
|
545 hdims[i] = dv (rank-i-1); |
4805
|
546 |
4815
|
547 space_hid = H5Screate_simple (rank, hdims, 0); |
4805
|
548 if (space_hid < 0) |
|
549 return false; |
4687
|
550 |
4805
|
551 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
|
552 H5P_DEFAULT); |
|
553 if (data_hid < 0) |
4687
|
554 { |
4805
|
555 H5Sclose (space_hid); |
4687
|
556 return false; |
|
557 } |
|
558 |
4837
|
559 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687
|
560 |
4837
|
561 for (int i = 0; i < dv.numel (); ++i) |
4805
|
562 s[i] = m(i); |
4687
|
563 |
4805
|
564 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
565 H5P_DEFAULT, s) >= 0; |
4687
|
566 |
|
567 H5Dclose (data_hid); |
|
568 H5Sclose (space_hid); |
4837
|
569 |
4687
|
570 return retval; |
|
571 } |
|
572 |
|
573 bool |
|
574 octave_char_matrix_str::load_hdf5 (hid_t loc_id, const char *name, |
|
575 bool /* have_h5giterate_bug */) |
|
576 { |
4837
|
577 bool retval = false; |
|
578 |
4805
|
579 dim_vector dv; |
|
580 int empty = load_hdf5_empty (loc_id, name, dv); |
|
581 if (empty > 0) |
|
582 matrix.resize(dv); |
4837
|
583 if (empty) |
|
584 return (empty > 0); |
4805
|
585 |
4687
|
586 hid_t data_hid = H5Dopen (loc_id, name); |
|
587 hid_t space_hid = H5Dget_space (data_hid); |
|
588 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
|
589 hid_t type_hid = H5Dget_type (data_hid); |
4805
|
590 hid_t type_class_hid = H5Tget_class (type_hid); |
4687
|
591 |
4805
|
592 if (type_class_hid == H5T_INTEGER) |
4687
|
593 { |
4805
|
594 if (rank < 1) |
4687
|
595 { |
|
596 H5Tclose (type_hid); |
|
597 H5Sclose (space_hid); |
|
598 H5Dclose (data_hid); |
|
599 return false; |
|
600 } |
4805
|
601 |
|
602 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
603 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
604 |
|
605 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
606 |
|
607 // Octave uses column-major, while HDF5 uses row-major ordering |
|
608 if (rank == 1) |
|
609 { |
|
610 dv.resize (2); |
|
611 dv(0) = 1; |
|
612 dv(1) = hdims[0]; |
|
613 } |
4687
|
614 else |
|
615 { |
4805
|
616 dv.resize (rank); |
4815
|
617 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4805
|
618 dv(j) = hdims[i]; |
|
619 } |
|
620 |
|
621 charNDArray m (dv); |
|
622 char *str = m.fortran_vec (); |
|
623 if (H5Dread (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
624 H5P_DEFAULT, str) >= 0) |
4805
|
625 { |
|
626 retval = true; |
|
627 matrix = m; |
|
628 } |
|
629 |
|
630 H5Tclose (type_hid); |
|
631 H5Sclose (space_hid); |
|
632 H5Dclose (data_hid); |
|
633 return true; |
|
634 } |
|
635 else |
|
636 { |
|
637 // This is cruft for backward compatiability and easy data |
|
638 // importation |
|
639 if (rank == 0) |
|
640 { |
|
641 // a single string: |
|
642 int slen = H5Tget_size (type_hid); |
|
643 if (slen < 0) |
4687
|
644 { |
|
645 H5Tclose (type_hid); |
|
646 H5Sclose (space_hid); |
|
647 H5Dclose (data_hid); |
|
648 return false; |
|
649 } |
4805
|
650 else |
|
651 { |
|
652 OCTAVE_LOCAL_BUFFER (char, s, slen); |
|
653 // create datatype for (null-terminated) string |
|
654 // to read into: |
|
655 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
656 H5Tset_size (st_id, slen); |
|
657 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, |
|
658 H5P_DEFAULT, (void *) s) < 0) |
|
659 { |
|
660 H5Tclose (st_id); |
|
661 H5Tclose (type_hid); |
|
662 H5Sclose (space_hid); |
|
663 H5Dclose (data_hid); |
|
664 return false; |
|
665 } |
4687
|
666 |
4805
|
667 matrix = charMatrix (s); |
4687
|
668 |
4805
|
669 H5Tclose (st_id); |
|
670 H5Tclose (type_hid); |
|
671 H5Sclose (space_hid); |
|
672 H5Dclose (data_hid); |
|
673 return true; |
|
674 } |
4687
|
675 } |
4805
|
676 else if (rank == 1) |
|
677 { |
|
678 // string vector |
|
679 hsize_t elements, maxdim; |
|
680 H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); |
|
681 int slen = H5Tget_size (type_hid); |
|
682 if (slen < 0) |
|
683 { |
|
684 H5Tclose (type_hid); |
|
685 H5Sclose (space_hid); |
|
686 H5Dclose (data_hid); |
|
687 return false; |
|
688 } |
|
689 else |
|
690 { |
|
691 // hdf5 string arrays store strings of all the |
|
692 // same physical length (I think), which is |
|
693 // slightly wasteful, but oh well. |
|
694 |
|
695 OCTAVE_LOCAL_BUFFER (char, s, elements * slen); |
|
696 |
|
697 // create datatype for (null-terminated) string |
|
698 // to read into: |
|
699 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
700 H5Tset_size (st_id, slen); |
|
701 |
|
702 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, |
|
703 H5P_DEFAULT, (void *) s) < 0) |
|
704 { |
|
705 H5Tclose (st_id); |
|
706 H5Tclose (type_hid); |
|
707 H5Sclose (space_hid); |
|
708 H5Dclose (data_hid); |
|
709 return false; |
|
710 } |
|
711 |
|
712 charMatrix chm (elements, slen - 1); |
|
713 for (hsize_t i = 0; i < elements; ++i) |
|
714 { |
|
715 chm.insert (s + i*slen, i, 0); |
|
716 } |
|
717 |
|
718 matrix = chm; |
|
719 |
|
720 H5Tclose (st_id); |
|
721 H5Tclose (type_hid); |
|
722 H5Sclose (space_hid); |
|
723 H5Dclose (data_hid); |
|
724 return true; |
|
725 } |
|
726 } |
|
727 else |
4687
|
728 { |
|
729 H5Tclose (type_hid); |
|
730 H5Sclose (space_hid); |
|
731 H5Dclose (data_hid); |
|
732 return false; |
|
733 } |
|
734 } |
4837
|
735 |
|
736 return retval; |
4687
|
737 } |
4944
|
738 |
4687
|
739 #endif |
|
740 |
2376
|
741 /* |
|
742 ;;; Local Variables: *** |
|
743 ;;; mode: C++ *** |
|
744 ;;; End: *** |
|
745 */ |