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