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