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