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 |
5731
|
152 octave_value |
|
153 octave_char_matrix_str::resize (const dim_vector& dv, bool fill) const |
|
154 { |
|
155 charNDArray retval (matrix); |
|
156 if (fill) |
|
157 retval.resize (dv, charNDArray::resize_fill_value()); |
|
158 else |
|
159 retval.resize (dv); |
|
160 return octave_value (retval, true); |
|
161 } |
|
162 |
2376
|
163 bool |
|
164 octave_char_matrix_str::valid_as_scalar_index (void) const |
|
165 { |
|
166 bool retval = false; |
|
167 error ("octave_char_matrix_str::valid_as_scalar_index(): not implemented"); |
|
168 return retval; |
|
169 } |
|
170 |
4668
|
171 #define CHAR_MATRIX_CONV(T, INIT, TNAME, FCN) \ |
|
172 T retval INIT; \ |
|
173 \ |
|
174 if (! force_string_conv) \ |
|
175 gripe_invalid_conversion ("string", TNAME); \ |
|
176 else \ |
|
177 { \ |
|
178 if (Vwarn_str_to_num) \ |
|
179 gripe_implicit_conversion ("string", TNAME); \ |
|
180 \ |
|
181 retval = octave_char_matrix::FCN (); \ |
|
182 } \ |
|
183 \ |
|
184 return retval |
|
185 |
4643
|
186 double |
|
187 octave_char_matrix_str::double_value (bool force_string_conv) const |
|
188 { |
4668
|
189 CHAR_MATRIX_CONV (double, = 0, "real scalar", double_value); |
|
190 } |
4643
|
191 |
4668
|
192 Complex |
|
193 octave_char_matrix_str::complex_value (bool force_string_conv) const |
|
194 { |
|
195 CHAR_MATRIX_CONV (Complex, = 0, "complex scalar", complex_value); |
4643
|
196 } |
|
197 |
2376
|
198 Matrix |
|
199 octave_char_matrix_str::matrix_value (bool force_string_conv) const |
|
200 { |
4668
|
201 CHAR_MATRIX_CONV (Matrix, , "real matrix", matrix_value); |
|
202 } |
|
203 |
|
204 ComplexMatrix |
|
205 octave_char_matrix_str::complex_matrix_value (bool force_string_conv) const |
|
206 { |
|
207 CHAR_MATRIX_CONV (ComplexMatrix, , "complex matrix", complex_matrix_value); |
|
208 } |
2376
|
209 |
4668
|
210 NDArray |
|
211 octave_char_matrix_str::array_value (bool force_string_conv) const |
|
212 { |
|
213 CHAR_MATRIX_CONV (NDArray, , "real N-d array", array_value); |
|
214 } |
2376
|
215 |
4668
|
216 ComplexNDArray |
|
217 octave_char_matrix_str::complex_array_value (bool force_string_conv) const |
|
218 { |
|
219 CHAR_MATRIX_CONV (ComplexNDArray, , "complex N-d array", |
|
220 complex_array_value); |
2376
|
221 } |
|
222 |
2493
|
223 string_vector |
5715
|
224 octave_char_matrix_str::all_strings (bool) const |
2376
|
225 { |
4513
|
226 string_vector retval; |
|
227 |
|
228 if (matrix.ndims () == 2) |
|
229 { |
|
230 charMatrix chm = matrix.matrix_value (); |
|
231 |
5275
|
232 octave_idx_type n = chm.rows (); |
2493
|
233 |
4513
|
234 retval.resize (n); |
2493
|
235 |
5275
|
236 for (octave_idx_type i = 0; i < n; i++) |
5707
|
237 retval[i] = chm.row_as_string (i); |
4513
|
238 } |
|
239 else |
|
240 error ("invalid conversion of charNDArray to string_vector"); |
2493
|
241 |
|
242 return retval; |
2376
|
243 } |
|
244 |
3536
|
245 std::string |
4457
|
246 octave_char_matrix_str::string_value (bool) const |
2376
|
247 { |
4513
|
248 std::string retval; |
|
249 |
|
250 if (matrix.ndims () == 2) |
|
251 { |
|
252 charMatrix chm = matrix.matrix_value (); |
|
253 |
|
254 retval = chm.row_as_string (0); // XXX FIXME??? XXX |
|
255 } |
|
256 else |
|
257 error ("invalid conversion of charNDArray to string"); |
|
258 |
|
259 return retval; |
2376
|
260 } |
|
261 |
|
262 void |
3523
|
263 octave_char_matrix_str::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2376
|
264 { |
3219
|
265 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
266 current_print_indent_level (), true); |
2376
|
267 } |
|
268 |
4687
|
269 bool |
|
270 octave_char_matrix_str::save_ascii (std::ostream& os, |
|
271 bool& /* infnan_warned */, |
|
272 bool /* strip_nan_and_inf */) |
|
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"; |
|
282 os.write (X_CAST (char *, tmp.fortran_vec ()), d.numel ()); |
|
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 (); |
|
300 os.write (X_CAST (char *, tmp), len); |
|
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 |
5099
|
336 charNDArray tmp(dv); |
|
337 char *ftmp = tmp.fortran_vec (); |
|
338 |
|
339 // Skip the return line |
|
340 if (! is.read (ftmp, 1)) |
|
341 return false; |
4805
|
342 |
5099
|
343 if (! is.read (ftmp, dv.numel ()) || !is) |
|
344 { |
|
345 error ("load: failed to load string constant"); |
|
346 success = false; |
|
347 } |
|
348 else |
|
349 matrix = tmp; |
|
350 } |
|
351 else |
4687
|
352 { |
5099
|
353 error ("load: failed to extract matrix size"); |
4805
|
354 success = false; |
4687
|
355 } |
|
356 } |
5099
|
357 else if (kw == "elements") |
4687
|
358 { |
5099
|
359 int elements = val; |
4805
|
360 |
|
361 if (elements >= 0) |
|
362 { |
|
363 // XXX FIXME XXX -- need to be able to get max length |
|
364 // before doing anything. |
4687
|
365 |
4805
|
366 charMatrix chm (elements, 0); |
|
367 int max_len = 0; |
|
368 for (int i = 0; i < elements; i++) |
|
369 { |
|
370 int len; |
|
371 if (extract_keyword (is, "length", len) && len >= 0) |
|
372 { |
|
373 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
374 |
|
375 if (len > 0 && ! |
|
376 is.read (X_CAST (char *, tmp), len)) |
|
377 { |
|
378 error ("load: failed to load string constant"); |
|
379 success = false; |
|
380 break; |
|
381 } |
|
382 else |
|
383 { |
|
384 tmp [len] = '\0'; |
|
385 if (len > max_len) |
|
386 { |
|
387 max_len = len; |
|
388 chm.resize (elements, max_len, 0); |
|
389 } |
|
390 chm.insert (tmp, i, 0); |
|
391 } |
|
392 } |
|
393 else |
|
394 { |
|
395 error ("load: failed to extract string length for element %d", |
|
396 i+1); |
|
397 success = false; |
|
398 } |
|
399 } |
|
400 |
|
401 if (! error_state) |
|
402 matrix = chm; |
4687
|
403 } |
|
404 else |
|
405 { |
4805
|
406 error ("load: failed to extract number of string elements"); |
|
407 success = false; |
|
408 } |
|
409 } |
5099
|
410 else if (kw == "length") |
4805
|
411 { |
5099
|
412 int len = val; |
4805
|
413 |
5099
|
414 if (len >= 0) |
4805
|
415 { |
|
416 // This is cruft for backward compatiability, |
|
417 // but relatively harmless. |
|
418 |
|
419 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
420 |
|
421 if (len > 0 && ! is.read (X_CAST (char *, tmp), len)) |
|
422 { |
|
423 error ("load: failed to load string constant"); |
|
424 } |
4687
|
425 else |
4805
|
426 { |
|
427 tmp [len] = '\0'; |
|
428 |
|
429 if (is) |
|
430 matrix = charMatrix (tmp); |
|
431 else |
|
432 error ("load: failed to load string constant"); |
|
433 } |
4687
|
434 } |
|
435 } |
5099
|
436 else |
|
437 panic_impossible (); |
|
438 } |
|
439 else |
|
440 { |
|
441 error ("load: failed to extract number of rows and columns"); |
|
442 success = false; |
4687
|
443 } |
|
444 |
|
445 return success; |
|
446 } |
|
447 |
|
448 bool |
|
449 octave_char_matrix_str::save_binary (std::ostream& os, |
|
450 bool& /* save_as_floats */) |
|
451 { |
4805
|
452 dim_vector d = dims (); |
|
453 if (d.length() < 1) |
|
454 return false; |
|
455 |
|
456 // Use negative value for ndims to differentiate with old format!! |
|
457 FOUR_BYTE_INT tmp = - d.length(); |
|
458 os.write (X_CAST (char *, &tmp), 4); |
|
459 for (int i=0; i < d.length (); i++) |
4687
|
460 { |
4805
|
461 tmp = d(i); |
|
462 os.write (X_CAST (char *, &tmp), 4); |
4687
|
463 } |
4805
|
464 |
|
465 charNDArray m = char_array_value (); |
|
466 os.write (m.fortran_vec (), d.numel ()); |
4687
|
467 return true; |
|
468 } |
|
469 |
|
470 bool |
|
471 octave_char_matrix_str::load_binary (std::istream& is, bool swap, |
|
472 oct_mach_info::float_format /* fmt */) |
|
473 { |
|
474 FOUR_BYTE_INT elements; |
|
475 if (! is.read (X_CAST (char *, &elements), 4)) |
|
476 return false; |
|
477 if (swap) |
4944
|
478 swap_bytes<4> (&elements); |
4805
|
479 |
|
480 if (elements < 0) |
4687
|
481 { |
4805
|
482 FOUR_BYTE_INT mdims = - elements; |
|
483 FOUR_BYTE_INT di; |
|
484 dim_vector dv; |
|
485 dv.resize (mdims); |
|
486 |
|
487 for (int i = 0; i < mdims; i++) |
|
488 { |
|
489 if (! is.read (X_CAST (char *, &di), 4)) |
|
490 return false; |
|
491 if (swap) |
4944
|
492 swap_bytes<4> (&di); |
4805
|
493 dv(i) = di; |
|
494 } |
|
495 |
5157
|
496 // Convert an array with a single dimension to be a row vector. |
|
497 // Octave should never write files like this, other software |
|
498 // might. |
|
499 |
|
500 if (mdims == 1) |
|
501 { |
|
502 mdims = 2; |
|
503 dv.resize (mdims); |
|
504 dv(1) = dv(0); |
|
505 dv(0) = 1; |
|
506 } |
|
507 |
4805
|
508 charNDArray m(dv); |
|
509 char *tmp = m.fortran_vec (); |
|
510 is.read (tmp, dv.numel ()); |
|
511 |
|
512 if (error_state || ! is) |
4687
|
513 return false; |
4805
|
514 matrix = m; |
|
515 } |
|
516 else |
|
517 { |
|
518 charMatrix chm (elements, 0); |
|
519 int max_len = 0; |
|
520 for (int i = 0; i < elements; i++) |
4687
|
521 { |
4805
|
522 FOUR_BYTE_INT len; |
|
523 if (! is.read (X_CAST (char *, &len), 4)) |
|
524 return false; |
|
525 if (swap) |
4944
|
526 swap_bytes<4> (&len); |
4805
|
527 OCTAVE_LOCAL_BUFFER (char, btmp, len+1); |
|
528 if (! is.read (X_CAST (char *, btmp), len)) |
|
529 return false; |
|
530 if (len > max_len) |
|
531 { |
|
532 max_len = len; |
|
533 chm.resize (elements, max_len, 0); |
|
534 } |
|
535 btmp [len] = '\0'; |
|
536 chm.insert (btmp, i, 0); |
4687
|
537 } |
4805
|
538 matrix = chm; |
4687
|
539 } |
|
540 return true; |
|
541 } |
|
542 |
|
543 #if defined (HAVE_HDF5) |
4944
|
544 |
4687
|
545 bool |
|
546 octave_char_matrix_str::save_hdf5 (hid_t loc_id, const char *name, |
|
547 bool /* save_as_floats */) |
|
548 { |
4837
|
549 dim_vector dv = dims (); |
|
550 int empty = save_hdf5_empty (loc_id, name, dv); |
|
551 if (empty) |
4805
|
552 return (empty > 0); |
4687
|
553 |
4837
|
554 int rank = dv.length (); |
4805
|
555 hid_t space_hid = -1, data_hid = -1; |
|
556 bool retval = true; |
|
557 charNDArray m = char_array_value (); |
|
558 |
|
559 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
4687
|
560 |
4805
|
561 // Octave uses column-major, while HDF5 uses row-major ordering |
|
562 for (int i = 0; i < rank; i++) |
4837
|
563 hdims[i] = dv (rank-i-1); |
4805
|
564 |
4815
|
565 space_hid = H5Screate_simple (rank, hdims, 0); |
4805
|
566 if (space_hid < 0) |
|
567 return false; |
4687
|
568 |
4805
|
569 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
|
570 H5P_DEFAULT); |
|
571 if (data_hid < 0) |
4687
|
572 { |
4805
|
573 H5Sclose (space_hid); |
4687
|
574 return false; |
|
575 } |
|
576 |
4837
|
577 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687
|
578 |
4837
|
579 for (int i = 0; i < dv.numel (); ++i) |
4805
|
580 s[i] = m(i); |
4687
|
581 |
4805
|
582 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
583 H5P_DEFAULT, s) >= 0; |
4687
|
584 |
|
585 H5Dclose (data_hid); |
|
586 H5Sclose (space_hid); |
4837
|
587 |
4687
|
588 return retval; |
|
589 } |
|
590 |
|
591 bool |
|
592 octave_char_matrix_str::load_hdf5 (hid_t loc_id, const char *name, |
|
593 bool /* have_h5giterate_bug */) |
|
594 { |
4837
|
595 bool retval = false; |
|
596 |
4805
|
597 dim_vector dv; |
|
598 int empty = load_hdf5_empty (loc_id, name, dv); |
|
599 if (empty > 0) |
|
600 matrix.resize(dv); |
4837
|
601 if (empty) |
|
602 return (empty > 0); |
4805
|
603 |
4687
|
604 hid_t data_hid = H5Dopen (loc_id, name); |
|
605 hid_t space_hid = H5Dget_space (data_hid); |
|
606 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
|
607 hid_t type_hid = H5Dget_type (data_hid); |
4805
|
608 hid_t type_class_hid = H5Tget_class (type_hid); |
4687
|
609 |
4805
|
610 if (type_class_hid == H5T_INTEGER) |
4687
|
611 { |
4805
|
612 if (rank < 1) |
4687
|
613 { |
|
614 H5Tclose (type_hid); |
|
615 H5Sclose (space_hid); |
|
616 H5Dclose (data_hid); |
|
617 return false; |
|
618 } |
4805
|
619 |
|
620 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
621 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
622 |
|
623 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
624 |
|
625 // Octave uses column-major, while HDF5 uses row-major ordering |
|
626 if (rank == 1) |
|
627 { |
|
628 dv.resize (2); |
|
629 dv(0) = 1; |
|
630 dv(1) = hdims[0]; |
|
631 } |
4687
|
632 else |
|
633 { |
4805
|
634 dv.resize (rank); |
4815
|
635 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4805
|
636 dv(j) = hdims[i]; |
|
637 } |
|
638 |
|
639 charNDArray m (dv); |
|
640 char *str = m.fortran_vec (); |
|
641 if (H5Dread (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815
|
642 H5P_DEFAULT, str) >= 0) |
4805
|
643 { |
|
644 retval = true; |
|
645 matrix = m; |
|
646 } |
|
647 |
|
648 H5Tclose (type_hid); |
|
649 H5Sclose (space_hid); |
|
650 H5Dclose (data_hid); |
|
651 return true; |
|
652 } |
|
653 else |
|
654 { |
|
655 // This is cruft for backward compatiability and easy data |
|
656 // importation |
|
657 if (rank == 0) |
|
658 { |
|
659 // a single string: |
|
660 int slen = H5Tget_size (type_hid); |
|
661 if (slen < 0) |
4687
|
662 { |
|
663 H5Tclose (type_hid); |
|
664 H5Sclose (space_hid); |
|
665 H5Dclose (data_hid); |
|
666 return false; |
|
667 } |
4805
|
668 else |
|
669 { |
|
670 OCTAVE_LOCAL_BUFFER (char, s, slen); |
|
671 // create datatype for (null-terminated) string |
|
672 // to read into: |
|
673 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
674 H5Tset_size (st_id, slen); |
|
675 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, |
|
676 H5P_DEFAULT, (void *) s) < 0) |
|
677 { |
|
678 H5Tclose (st_id); |
|
679 H5Tclose (type_hid); |
|
680 H5Sclose (space_hid); |
|
681 H5Dclose (data_hid); |
|
682 return false; |
|
683 } |
4687
|
684 |
4805
|
685 matrix = charMatrix (s); |
4687
|
686 |
4805
|
687 H5Tclose (st_id); |
|
688 H5Tclose (type_hid); |
|
689 H5Sclose (space_hid); |
|
690 H5Dclose (data_hid); |
|
691 return true; |
|
692 } |
4687
|
693 } |
4805
|
694 else if (rank == 1) |
|
695 { |
|
696 // string vector |
|
697 hsize_t elements, maxdim; |
|
698 H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); |
|
699 int slen = H5Tget_size (type_hid); |
|
700 if (slen < 0) |
|
701 { |
|
702 H5Tclose (type_hid); |
|
703 H5Sclose (space_hid); |
|
704 H5Dclose (data_hid); |
|
705 return false; |
|
706 } |
|
707 else |
|
708 { |
|
709 // hdf5 string arrays store strings of all the |
|
710 // same physical length (I think), which is |
|
711 // slightly wasteful, but oh well. |
|
712 |
|
713 OCTAVE_LOCAL_BUFFER (char, s, elements * slen); |
|
714 |
|
715 // create datatype for (null-terminated) string |
|
716 // to read into: |
|
717 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
718 H5Tset_size (st_id, slen); |
|
719 |
|
720 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, |
|
721 H5P_DEFAULT, (void *) s) < 0) |
|
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 */ |