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, |
5885
|
86 bool 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 { \ |
5878
|
179 warning_with_id ("Octave:str-to-num", \ |
5781
|
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 |
5958
|
272 octave_char_matrix_str::save_ascii (std::ostream& os, bool& /* infnan_warned */) |
4687
|
273 { |
4805
|
274 dim_vector d = dims (); |
|
275 if (d.length () > 2) |
|
276 { |
|
277 charNDArray tmp = char_array_value (); |
|
278 os << "# ndims: " << d.length () << "\n"; |
|
279 for (int i=0; i < d.length (); i++) |
|
280 os << " " << d (i); |
|
281 os << "\n"; |
5760
|
282 os.write (tmp.fortran_vec (), d.numel ()); |
4805
|
283 os << "\n"; |
|
284 } |
|
285 else |
4687
|
286 { |
4805
|
287 // Keep this case, rather than use generic code above for |
|
288 // backward compatiability. Makes load_ascii much more complex!! |
|
289 charMatrix chm = char_matrix_value (); |
5275
|
290 octave_idx_type elements = chm.rows (); |
4805
|
291 os << "# elements: " << elements << "\n"; |
5275
|
292 for (octave_idx_type i = 0; i < elements; i++) |
4805
|
293 { |
|
294 unsigned len = chm.cols (); |
|
295 os << "# length: " << len << "\n"; |
|
296 std::string tstr = chm.row_as_string (i, false, true); |
|
297 const char *tmp = tstr.data (); |
|
298 if (tstr.length () > len) |
|
299 panic_impossible (); |
5760
|
300 os.write (tmp, len); |
4805
|
301 os << "\n"; |
|
302 } |
4687
|
303 } |
|
304 |
|
305 return true; |
|
306 } |
|
307 |
|
308 bool |
|
309 octave_char_matrix_str::load_ascii (std::istream& is) |
|
310 { |
|
311 bool success = true; |
5099
|
312 |
|
313 string_vector keywords(3); |
|
314 |
|
315 keywords[0] = "ndims"; |
|
316 keywords[1] = "elements"; |
|
317 keywords[2] = "length"; |
|
318 |
|
319 std::string kw; |
|
320 int val = 0; |
4687
|
321 |
5099
|
322 if (extract_keyword (is, keywords, kw, val, true)) |
4687
|
323 { |
5099
|
324 if (kw == "ndims") |
4687
|
325 { |
5099
|
326 int mdims = val; |
|
327 |
|
328 if (mdims >= 0) |
|
329 { |
|
330 dim_vector dv; |
|
331 dv.resize (mdims); |
4805
|
332 |
5099
|
333 for (int i = 0; i < mdims; i++) |
|
334 is >> dv(i); |
4687
|
335 |
6717
|
336 if (is) |
|
337 { |
|
338 charNDArray tmp(dv); |
|
339 |
|
340 if (tmp.is_empty ()) |
|
341 matrix = tmp; |
|
342 else |
|
343 { |
|
344 char *ftmp = tmp.fortran_vec (); |
5099
|
345 |
6717
|
346 // Skip the return line |
|
347 if (! is.read (ftmp, 1)) |
|
348 return false; |
4805
|
349 |
6717
|
350 if (! is.read (ftmp, dv.numel ()) || !is) |
|
351 { |
|
352 error ("load: failed to load string constant"); |
|
353 success = false; |
|
354 } |
|
355 else |
|
356 matrix = tmp; |
|
357 } |
|
358 } |
|
359 else |
5099
|
360 { |
6717
|
361 error ("load: failed to read dimensions"); |
5099
|
362 success = false; |
|
363 } |
|
364 } |
|
365 else |
4687
|
366 { |
5099
|
367 error ("load: failed to extract matrix size"); |
4805
|
368 success = false; |
4687
|
369 } |
|
370 } |
5099
|
371 else if (kw == "elements") |
4687
|
372 { |
5099
|
373 int elements = val; |
4805
|
374 |
|
375 if (elements >= 0) |
|
376 { |
5775
|
377 // FIXME -- need to be able to get max length |
4805
|
378 // before doing anything. |
4687
|
379 |
4805
|
380 charMatrix chm (elements, 0); |
|
381 int max_len = 0; |
|
382 for (int i = 0; i < elements; i++) |
|
383 { |
|
384 int len; |
|
385 if (extract_keyword (is, "length", len) && len >= 0) |
|
386 { |
6151
|
387 // Use this instead of a C-style character |
|
388 // buffer so that we can properly handle |
|
389 // embedded NUL characters. |
|
390 charMatrix tmp (1, len); |
|
391 char *ptmp = tmp.fortran_vec (); |
|
392 |
|
393 if (len > 0 && ! is.read (ptmp, len)) |
4805
|
394 { |
|
395 error ("load: failed to load string constant"); |
|
396 success = false; |
|
397 break; |
|
398 } |
|
399 else |
|
400 { |
|
401 if (len > max_len) |
|
402 { |
|
403 max_len = len; |
|
404 chm.resize (elements, max_len, 0); |
|
405 } |
6151
|
406 |
4805
|
407 chm.insert (tmp, i, 0); |
|
408 } |
|
409 } |
|
410 else |
|
411 { |
|
412 error ("load: failed to extract string length for element %d", |
|
413 i+1); |
|
414 success = false; |
|
415 } |
|
416 } |
|
417 |
|
418 if (! error_state) |
|
419 matrix = chm; |
4687
|
420 } |
|
421 else |
|
422 { |
4805
|
423 error ("load: failed to extract number of string elements"); |
|
424 success = false; |
|
425 } |
|
426 } |
5099
|
427 else if (kw == "length") |
4805
|
428 { |
5099
|
429 int len = val; |
4805
|
430 |
5099
|
431 if (len >= 0) |
4805
|
432 { |
|
433 // This is cruft for backward compatiability, |
|
434 // but relatively harmless. |
|
435 |
6151
|
436 // Use this instead of a C-style character buffer so |
|
437 // that we can properly handle embedded NUL characters. |
|
438 charMatrix tmp (1, len); |
|
439 char *ptmp = tmp.fortran_vec (); |
4805
|
440 |
6151
|
441 if (len > 0 && ! is.read (ptmp, len)) |
4805
|
442 { |
|
443 error ("load: failed to load string constant"); |
|
444 } |
4687
|
445 else |
4805
|
446 { |
|
447 if (is) |
6151
|
448 matrix = tmp; |
4805
|
449 else |
|
450 error ("load: failed to load string constant"); |
|
451 } |
4687
|
452 } |
|
453 } |
5099
|
454 else |
|
455 panic_impossible (); |
|
456 } |
|
457 else |
|
458 { |
|
459 error ("load: failed to extract number of rows and columns"); |
|
460 success = false; |
4687
|
461 } |
|
462 |
|
463 return success; |
|
464 } |
|
465 |
|
466 bool |
|
467 octave_char_matrix_str::save_binary (std::ostream& os, |
|
468 bool& /* save_as_floats */) |
|
469 { |
4805
|
470 dim_vector d = dims (); |
|
471 if (d.length() < 1) |
|
472 return false; |
|
473 |
|
474 // Use negative value for ndims to differentiate with old format!! |
5828
|
475 int32_t tmp = - d.length(); |
5760
|
476 os.write (reinterpret_cast<char *> (&tmp), 4); |
4805
|
477 for (int i=0; i < d.length (); i++) |
4687
|
478 { |
4805
|
479 tmp = d(i); |
5760
|
480 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687
|
481 } |
4805
|
482 |
|
483 charNDArray m = char_array_value (); |
|
484 os.write (m.fortran_vec (), d.numel ()); |
4687
|
485 return true; |
|
486 } |
|
487 |
|
488 bool |
|
489 octave_char_matrix_str::load_binary (std::istream& is, bool swap, |
|
490 oct_mach_info::float_format /* fmt */) |
|
491 { |
5828
|
492 int32_t elements; |
5760
|
493 if (! is.read (reinterpret_cast<char *> (&elements), 4)) |
4687
|
494 return false; |
|
495 if (swap) |
4944
|
496 swap_bytes<4> (&elements); |
4805
|
497 |
|
498 if (elements < 0) |
4687
|
499 { |
5828
|
500 int32_t mdims = - elements; |
|
501 int32_t di; |
4805
|
502 dim_vector dv; |
|
503 dv.resize (mdims); |
|
504 |
|
505 for (int i = 0; i < mdims; i++) |
|
506 { |
5760
|
507 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4805
|
508 return false; |
|
509 if (swap) |
4944
|
510 swap_bytes<4> (&di); |
4805
|
511 dv(i) = di; |
|
512 } |
|
513 |
5157
|
514 // Convert an array with a single dimension to be a row vector. |
|
515 // Octave should never write files like this, other software |
|
516 // might. |
|
517 |
|
518 if (mdims == 1) |
|
519 { |
|
520 mdims = 2; |
|
521 dv.resize (mdims); |
|
522 dv(1) = dv(0); |
|
523 dv(0) = 1; |
|
524 } |
|
525 |
4805
|
526 charNDArray m(dv); |
|
527 char *tmp = m.fortran_vec (); |
|
528 is.read (tmp, dv.numel ()); |
|
529 |
|
530 if (error_state || ! is) |
4687
|
531 return false; |
4805
|
532 matrix = m; |
|
533 } |
|
534 else |
|
535 { |
|
536 charMatrix chm (elements, 0); |
|
537 int max_len = 0; |
|
538 for (int i = 0; i < elements; i++) |
4687
|
539 { |
5828
|
540 int32_t len; |
5760
|
541 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4805
|
542 return false; |
|
543 if (swap) |
4944
|
544 swap_bytes<4> (&len); |
6151
|
545 charMatrix btmp (1, len); |
|
546 char *pbtmp = btmp.fortran_vec (); |
|
547 if (! is.read (pbtmp, len)) |
4805
|
548 return false; |
|
549 if (len > max_len) |
|
550 { |
|
551 max_len = len; |
|
552 chm.resize (elements, max_len, 0); |
|
553 } |
|
554 chm.insert (btmp, i, 0); |
4687
|
555 } |
4805
|
556 matrix = chm; |
4687
|
557 } |
|
558 return true; |
|
559 } |
|
560 |
|
561 #if defined (HAVE_HDF5) |
4944
|
562 |
4687
|
563 bool |
|
564 octave_char_matrix_str::save_hdf5 (hid_t loc_id, const char *name, |
|
565 bool /* save_as_floats */) |
|
566 { |
4837
|
567 dim_vector dv = dims (); |
|
568 int empty = save_hdf5_empty (loc_id, name, dv); |
|
569 if (empty) |
4805
|
570 return (empty > 0); |
4687
|
571 |
4837
|
572 int rank = dv.length (); |
4805
|
573 hid_t space_hid = -1, data_hid = -1; |
|
574 bool retval = true; |
|
575 charNDArray m = char_array_value (); |
|
576 |
|
577 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
4687
|
578 |
4805
|
579 // Octave uses column-major, while HDF5 uses row-major ordering |
|
580 for (int i = 0; i < rank; i++) |
4837
|
581 hdims[i] = dv (rank-i-1); |
4805
|
582 |
4815
|
583 space_hid = H5Screate_simple (rank, hdims, 0); |
4805
|
584 if (space_hid < 0) |
|
585 return false; |
4687
|
586 |
4805
|
587 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
|
588 H5P_DEFAULT); |
|
589 if (data_hid < 0) |
4687
|
590 { |
4805
|
591 H5Sclose (space_hid); |
4687
|
592 return false; |
|
593 } |
|
594 |
4837
|
595 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687
|
596 |
4837
|
597 for (int i = 0; i < dv.numel (); ++i) |
4805
|
598 s[i] = m(i); |
4687
|
599 |
4805
|
600 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
601 H5P_DEFAULT, s) >= 0; |
4687
|
602 |
|
603 H5Dclose (data_hid); |
|
604 H5Sclose (space_hid); |
4837
|
605 |
4687
|
606 return retval; |
|
607 } |
|
608 |
|
609 bool |
|
610 octave_char_matrix_str::load_hdf5 (hid_t loc_id, const char *name, |
|
611 bool /* have_h5giterate_bug */) |
|
612 { |
4837
|
613 bool retval = false; |
|
614 |
4805
|
615 dim_vector dv; |
|
616 int empty = load_hdf5_empty (loc_id, name, dv); |
|
617 if (empty > 0) |
|
618 matrix.resize(dv); |
4837
|
619 if (empty) |
|
620 return (empty > 0); |
4805
|
621 |
4687
|
622 hid_t data_hid = H5Dopen (loc_id, name); |
|
623 hid_t space_hid = H5Dget_space (data_hid); |
|
624 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
|
625 hid_t type_hid = H5Dget_type (data_hid); |
4805
|
626 hid_t type_class_hid = H5Tget_class (type_hid); |
4687
|
627 |
4805
|
628 if (type_class_hid == H5T_INTEGER) |
4687
|
629 { |
4805
|
630 if (rank < 1) |
4687
|
631 { |
|
632 H5Tclose (type_hid); |
|
633 H5Sclose (space_hid); |
|
634 H5Dclose (data_hid); |
|
635 return false; |
|
636 } |
4805
|
637 |
|
638 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
639 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
640 |
|
641 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
642 |
|
643 // Octave uses column-major, while HDF5 uses row-major ordering |
|
644 if (rank == 1) |
|
645 { |
|
646 dv.resize (2); |
|
647 dv(0) = 1; |
|
648 dv(1) = hdims[0]; |
|
649 } |
4687
|
650 else |
|
651 { |
4805
|
652 dv.resize (rank); |
4815
|
653 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4805
|
654 dv(j) = hdims[i]; |
|
655 } |
|
656 |
|
657 charNDArray m (dv); |
|
658 char *str = m.fortran_vec (); |
|
659 if (H5Dread (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
660 H5P_DEFAULT, str) >= 0) |
4805
|
661 { |
|
662 retval = true; |
|
663 matrix = m; |
|
664 } |
|
665 |
|
666 H5Tclose (type_hid); |
|
667 H5Sclose (space_hid); |
|
668 H5Dclose (data_hid); |
|
669 return true; |
|
670 } |
|
671 else |
|
672 { |
|
673 // This is cruft for backward compatiability and easy data |
|
674 // importation |
|
675 if (rank == 0) |
|
676 { |
|
677 // a single string: |
|
678 int slen = H5Tget_size (type_hid); |
|
679 if (slen < 0) |
4687
|
680 { |
|
681 H5Tclose (type_hid); |
|
682 H5Sclose (space_hid); |
|
683 H5Dclose (data_hid); |
|
684 return false; |
|
685 } |
4805
|
686 else |
|
687 { |
|
688 OCTAVE_LOCAL_BUFFER (char, s, slen); |
|
689 // create datatype for (null-terminated) string |
|
690 // to read into: |
|
691 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
692 H5Tset_size (st_id, slen); |
5760
|
693 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, s) < 0) |
4805
|
694 { |
|
695 H5Tclose (st_id); |
|
696 H5Tclose (type_hid); |
|
697 H5Sclose (space_hid); |
|
698 H5Dclose (data_hid); |
|
699 return false; |
|
700 } |
4687
|
701 |
4805
|
702 matrix = charMatrix (s); |
4687
|
703 |
4805
|
704 H5Tclose (st_id); |
|
705 H5Tclose (type_hid); |
|
706 H5Sclose (space_hid); |
|
707 H5Dclose (data_hid); |
|
708 return true; |
|
709 } |
4687
|
710 } |
4805
|
711 else if (rank == 1) |
|
712 { |
|
713 // string vector |
|
714 hsize_t elements, maxdim; |
|
715 H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); |
|
716 int slen = H5Tget_size (type_hid); |
|
717 if (slen < 0) |
|
718 { |
|
719 H5Tclose (type_hid); |
|
720 H5Sclose (space_hid); |
|
721 H5Dclose (data_hid); |
|
722 return false; |
|
723 } |
|
724 else |
|
725 { |
|
726 // hdf5 string arrays store strings of all the |
|
727 // same physical length (I think), which is |
|
728 // slightly wasteful, but oh well. |
|
729 |
|
730 OCTAVE_LOCAL_BUFFER (char, s, elements * slen); |
|
731 |
|
732 // create datatype for (null-terminated) string |
|
733 // to read into: |
|
734 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
735 H5Tset_size (st_id, slen); |
|
736 |
5760
|
737 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, s) < 0) |
4805
|
738 { |
|
739 H5Tclose (st_id); |
|
740 H5Tclose (type_hid); |
|
741 H5Sclose (space_hid); |
|
742 H5Dclose (data_hid); |
|
743 return false; |
|
744 } |
|
745 |
|
746 charMatrix chm (elements, slen - 1); |
|
747 for (hsize_t i = 0; i < elements; ++i) |
|
748 { |
|
749 chm.insert (s + i*slen, i, 0); |
|
750 } |
|
751 |
|
752 matrix = chm; |
|
753 |
|
754 H5Tclose (st_id); |
|
755 H5Tclose (type_hid); |
|
756 H5Sclose (space_hid); |
|
757 H5Dclose (data_hid); |
|
758 return true; |
|
759 } |
|
760 } |
|
761 else |
4687
|
762 { |
|
763 H5Tclose (type_hid); |
|
764 H5Sclose (space_hid); |
|
765 H5Dclose (data_hid); |
|
766 return false; |
|
767 } |
|
768 } |
4837
|
769 |
|
770 return retval; |
4687
|
771 } |
4944
|
772 |
4687
|
773 #endif |
|
774 |
2376
|
775 /* |
|
776 ;;; Local Variables: *** |
|
777 ;;; mode: C++ *** |
|
778 ;;; End: *** |
|
779 */ |