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