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