Mercurial > octave-nkf
annotate src/ov-str-mat.cc @ 7961:a5d1e27ee1f4 ss-3-1-51
3.1.51 snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 22 Jul 2008 11:40:48 -0400 |
parents | 26d8a92644de |
children | dadf478ddc42 |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, |
4 2006, 2007 John W. Eaton | |
2376 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2376 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
28 #include <cctype> |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
29 |
3503 | 30 #include <iostream> |
4726 | 31 #include <vector> |
2901 | 32 |
4944 | 33 #include "data-conv.h" |
2376 | 34 #include "lo-ieee.h" |
4944 | 35 #include "mach-info.h" |
2376 | 36 #include "mx-base.h" |
37 | |
5758 | 38 #include "defun.h" |
39 #include "byte-swap.h" | |
40 #include "gripes.h" | |
41 #include "ls-oct-ascii.h" | |
42 #include "ls-hdf5.h" | |
43 #include "ls-utils.h" | |
2407 | 44 #include "oct-obj.h" |
4944 | 45 #include "oct-stream.h" |
2376 | 46 #include "ops.h" |
5033 | 47 #include "ov-scalar.h" |
2376 | 48 #include "ov-re-mat.h" |
49 #include "ov-str-mat.h" | |
50 #include "pr-output.h" | |
3836 | 51 #include "pt-mat.h" |
5758 | 52 #include "utils.h" |
4687 | 53 |
3219 | 54 DEFINE_OCTAVE_ALLOCATOR (octave_char_matrix_str); |
5279 | 55 DEFINE_OCTAVE_ALLOCATOR (octave_char_matrix_sq_str); |
2376 | 56 |
4612 | 57 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_str, "string", "char"); |
5279 | 58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_sq_str, "sq_string", "char"); |
2376 | 59 |
5759 | 60 static octave_base_value * |
61 default_numeric_conversion_function (const octave_base_value& a) | |
2376 | 62 { |
5759 | 63 octave_base_value *retval = 0; |
5033 | 64 |
2376 | 65 CAST_CONV_ARG (const octave_char_matrix_str&); |
66 | |
4668 | 67 NDArray nda = v.array_value (true); |
3203 | 68 |
5033 | 69 if (! error_state) |
70 { | |
71 if (nda.numel () == 1) | |
72 retval = new octave_scalar (nda(0)); | |
73 else | |
74 retval = new octave_matrix (nda); | |
75 } | |
76 | |
77 return retval; | |
2376 | 78 } |
79 | |
5759 | 80 octave_base_value::type_conv_fcn |
2376 | 81 octave_char_matrix_str::numeric_conversion_function (void) const |
82 { | |
83 return default_numeric_conversion_function; | |
84 } | |
85 | |
86 octave_value | |
5400 | 87 octave_char_matrix_str::do_index_op_internal (const octave_value_list& idx, |
5885 | 88 bool resize_ok, char type) |
2407 | 89 { |
90 octave_value retval; | |
91 | |
5275 | 92 octave_idx_type len = idx.length (); |
2407 | 93 |
94 switch (len) | |
95 { | |
5539 | 96 case 0: |
97 retval = octave_value (matrix, true, type); | |
2407 | 98 break; |
99 | |
100 case 1: | |
101 { | |
102 idx_vector i = idx (0).index_vector (); | |
103 | |
5086 | 104 if (! error_state) |
105 retval = octave_value (charNDArray (matrix.index (i, resize_ok)), | |
5400 | 106 true, type); |
2407 | 107 } |
108 break; | |
109 | |
5539 | 110 case 2: |
111 { | |
112 idx_vector i = idx (0).index_vector (); | |
113 idx_vector j = idx (1).index_vector (); | |
114 | |
115 if (! error_state) | |
116 retval = octave_value (charNDArray (matrix.index (i, j, resize_ok)), | |
117 true, type); | |
118 } | |
5435 | 119 break; |
120 | |
2407 | 121 default: |
4513 | 122 { |
123 Array<idx_vector> idx_vec (len); | |
124 | |
5275 | 125 for (octave_idx_type i = 0; i < len; i++) |
4513 | 126 idx_vec(i) = idx(i).index_vector (); |
127 | |
5086 | 128 if (! error_state) |
129 retval = octave_value (charNDArray (matrix.index (idx_vec, resize_ok)), | |
5400 | 130 true, type); |
4513 | 131 } |
2407 | 132 break; |
133 } | |
134 | |
135 return retval; | |
136 } | |
137 | |
138 void | |
139 octave_char_matrix_str::assign (const octave_value_list& idx, | |
140 const charMatrix& rhs) | |
141 { | |
5275 | 142 octave_idx_type len = idx.length (); |
2407 | 143 |
5775 | 144 // FIXME |
2571 | 145 charMatrix tmp = rhs; |
146 if (tmp.rows () == 1 && tmp.columns () == 0) | |
147 tmp.resize (0, 0); | |
148 | |
5275 | 149 for (octave_idx_type i = 0; i < len; i++) |
4513 | 150 matrix.set_index (idx(i).index_vector ()); |
2407 | 151 |
4513 | 152 ::assign (matrix, tmp, Vstring_fill_char); |
2407 | 153 } |
154 | |
5731 | 155 octave_value |
156 octave_char_matrix_str::resize (const dim_vector& dv, bool fill) const | |
157 { | |
158 charNDArray retval (matrix); | |
159 if (fill) | |
160 retval.resize (dv, charNDArray::resize_fill_value()); | |
161 else | |
162 retval.resize (dv); | |
163 return octave_value (retval, true); | |
164 } | |
165 | |
2376 | 166 bool |
167 octave_char_matrix_str::valid_as_scalar_index (void) const | |
168 { | |
169 bool retval = false; | |
170 error ("octave_char_matrix_str::valid_as_scalar_index(): not implemented"); | |
171 return retval; | |
172 } | |
173 | |
4668 | 174 #define CHAR_MATRIX_CONV(T, INIT, TNAME, FCN) \ |
175 T retval INIT; \ | |
176 \ | |
177 if (! force_string_conv) \ | |
178 gripe_invalid_conversion ("string", TNAME); \ | |
179 else \ | |
180 { \ | |
5878 | 181 warning_with_id ("Octave:str-to-num", \ |
5781 | 182 "implicit conversion from %s to %s", \ |
183 "string", TNAME); \ | |
4668 | 184 \ |
185 retval = octave_char_matrix::FCN (); \ | |
186 } \ | |
187 \ | |
188 return retval | |
189 | |
4643 | 190 double |
191 octave_char_matrix_str::double_value (bool force_string_conv) const | |
192 { | |
4668 | 193 CHAR_MATRIX_CONV (double, = 0, "real scalar", double_value); |
194 } | |
4643 | 195 |
4668 | 196 Complex |
197 octave_char_matrix_str::complex_value (bool force_string_conv) const | |
198 { | |
199 CHAR_MATRIX_CONV (Complex, = 0, "complex scalar", complex_value); | |
4643 | 200 } |
201 | |
2376 | 202 Matrix |
203 octave_char_matrix_str::matrix_value (bool force_string_conv) const | |
204 { | |
4668 | 205 CHAR_MATRIX_CONV (Matrix, , "real matrix", matrix_value); |
206 } | |
207 | |
208 ComplexMatrix | |
209 octave_char_matrix_str::complex_matrix_value (bool force_string_conv) const | |
210 { | |
211 CHAR_MATRIX_CONV (ComplexMatrix, , "complex matrix", complex_matrix_value); | |
212 } | |
2376 | 213 |
4668 | 214 NDArray |
215 octave_char_matrix_str::array_value (bool force_string_conv) const | |
216 { | |
217 CHAR_MATRIX_CONV (NDArray, , "real N-d array", array_value); | |
218 } | |
2376 | 219 |
4668 | 220 ComplexNDArray |
221 octave_char_matrix_str::complex_array_value (bool force_string_conv) const | |
222 { | |
223 CHAR_MATRIX_CONV (ComplexNDArray, , "complex N-d array", | |
224 complex_array_value); | |
2376 | 225 } |
226 | |
2493 | 227 string_vector |
5715 | 228 octave_char_matrix_str::all_strings (bool) const |
2376 | 229 { |
4513 | 230 string_vector retval; |
231 | |
232 if (matrix.ndims () == 2) | |
233 { | |
6816 | 234 charMatrix chm = matrix.matrix_value (); |
4513 | 235 |
6816 | 236 octave_idx_type n = chm.rows (); |
2493 | 237 |
6816 | 238 retval.resize (n); |
2493 | 239 |
6816 | 240 for (octave_idx_type i = 0; i < n; i++) |
241 retval[i] = chm.row_as_string (i); | |
4513 | 242 } |
243 else | |
244 error ("invalid conversion of charNDArray to string_vector"); | |
2493 | 245 |
246 return retval; | |
2376 | 247 } |
248 | |
3536 | 249 std::string |
4457 | 250 octave_char_matrix_str::string_value (bool) const |
2376 | 251 { |
4513 | 252 std::string retval; |
253 | |
254 if (matrix.ndims () == 2) | |
255 { | |
256 charMatrix chm = matrix.matrix_value (); | |
257 | |
5775 | 258 retval = chm.row_as_string (0); // FIXME??? |
4513 | 259 } |
260 else | |
261 error ("invalid conversion of charNDArray to string"); | |
262 | |
263 return retval; | |
2376 | 264 } |
265 | |
266 void | |
3523 | 267 octave_char_matrix_str::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2376 | 268 { |
3219 | 269 octave_print_internal (os, matrix, pr_as_read_syntax, |
270 current_print_indent_level (), true); | |
2376 | 271 } |
272 | |
4687 | 273 bool |
6974 | 274 octave_char_matrix_str::save_ascii (std::ostream& os) |
4687 | 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 |
6717 | 338 if (is) |
339 { | |
340 charNDArray tmp(dv); | |
341 | |
342 if (tmp.is_empty ()) | |
343 matrix = tmp; | |
344 else | |
345 { | |
346 char *ftmp = tmp.fortran_vec (); | |
5099 | 347 |
6717 | 348 // Skip the return line |
349 if (! is.read (ftmp, 1)) | |
350 return false; | |
4805 | 351 |
6717 | 352 if (! is.read (ftmp, dv.numel ()) || !is) |
353 { | |
354 error ("load: failed to load string constant"); | |
355 success = false; | |
356 } | |
357 else | |
358 matrix = tmp; | |
359 } | |
360 } | |
361 else | |
5099 | 362 { |
6717 | 363 error ("load: failed to read dimensions"); |
5099 | 364 success = false; |
365 } | |
366 } | |
367 else | |
4687 | 368 { |
5099 | 369 error ("load: failed to extract matrix size"); |
4805 | 370 success = false; |
4687 | 371 } |
372 } | |
5099 | 373 else if (kw == "elements") |
4687 | 374 { |
5099 | 375 int elements = val; |
4805 | 376 |
377 if (elements >= 0) | |
378 { | |
5775 | 379 // FIXME -- need to be able to get max length |
4805 | 380 // before doing anything. |
4687 | 381 |
4805 | 382 charMatrix chm (elements, 0); |
383 int max_len = 0; | |
384 for (int i = 0; i < elements; i++) | |
385 { | |
386 int len; | |
387 if (extract_keyword (is, "length", len) && len >= 0) | |
388 { | |
6151 | 389 // Use this instead of a C-style character |
390 // buffer so that we can properly handle | |
391 // embedded NUL characters. | |
392 charMatrix tmp (1, len); | |
393 char *ptmp = tmp.fortran_vec (); | |
394 | |
395 if (len > 0 && ! is.read (ptmp, len)) | |
4805 | 396 { |
397 error ("load: failed to load string constant"); | |
398 success = false; | |
399 break; | |
400 } | |
401 else | |
402 { | |
403 if (len > max_len) | |
404 { | |
405 max_len = len; | |
406 chm.resize (elements, max_len, 0); | |
407 } | |
6151 | 408 |
4805 | 409 chm.insert (tmp, i, 0); |
410 } | |
411 } | |
412 else | |
413 { | |
414 error ("load: failed to extract string length for element %d", | |
415 i+1); | |
416 success = false; | |
417 } | |
418 } | |
419 | |
420 if (! error_state) | |
421 matrix = chm; | |
4687 | 422 } |
423 else | |
424 { | |
4805 | 425 error ("load: failed to extract number of string elements"); |
426 success = false; | |
427 } | |
428 } | |
5099 | 429 else if (kw == "length") |
4805 | 430 { |
5099 | 431 int len = val; |
4805 | 432 |
5099 | 433 if (len >= 0) |
4805 | 434 { |
435 // This is cruft for backward compatiability, | |
436 // but relatively harmless. | |
437 | |
6151 | 438 // Use this instead of a C-style character buffer so |
439 // that we can properly handle embedded NUL characters. | |
440 charMatrix tmp (1, len); | |
441 char *ptmp = tmp.fortran_vec (); | |
4805 | 442 |
6151 | 443 if (len > 0 && ! is.read (ptmp, len)) |
4805 | 444 { |
445 error ("load: failed to load string constant"); | |
446 } | |
4687 | 447 else |
4805 | 448 { |
449 if (is) | |
6151 | 450 matrix = tmp; |
4805 | 451 else |
452 error ("load: failed to load string constant"); | |
453 } | |
4687 | 454 } |
455 } | |
5099 | 456 else |
457 panic_impossible (); | |
458 } | |
459 else | |
460 { | |
461 error ("load: failed to extract number of rows and columns"); | |
462 success = false; | |
4687 | 463 } |
464 | |
465 return success; | |
466 } | |
467 | |
468 bool | |
469 octave_char_matrix_str::save_binary (std::ostream& os, | |
470 bool& /* save_as_floats */) | |
471 { | |
4805 | 472 dim_vector d = dims (); |
473 if (d.length() < 1) | |
474 return false; | |
475 | |
476 // Use negative value for ndims to differentiate with old format!! | |
5828 | 477 int32_t tmp = - d.length(); |
5760 | 478 os.write (reinterpret_cast<char *> (&tmp), 4); |
4805 | 479 for (int i=0; i < d.length (); i++) |
4687 | 480 { |
4805 | 481 tmp = d(i); |
5760 | 482 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687 | 483 } |
4805 | 484 |
485 charNDArray m = char_array_value (); | |
486 os.write (m.fortran_vec (), d.numel ()); | |
4687 | 487 return true; |
488 } | |
489 | |
490 bool | |
491 octave_char_matrix_str::load_binary (std::istream& is, bool swap, | |
492 oct_mach_info::float_format /* fmt */) | |
493 { | |
5828 | 494 int32_t elements; |
5760 | 495 if (! is.read (reinterpret_cast<char *> (&elements), 4)) |
4687 | 496 return false; |
497 if (swap) | |
4944 | 498 swap_bytes<4> (&elements); |
4805 | 499 |
500 if (elements < 0) | |
4687 | 501 { |
5828 | 502 int32_t mdims = - elements; |
503 int32_t di; | |
4805 | 504 dim_vector dv; |
505 dv.resize (mdims); | |
506 | |
507 for (int i = 0; i < mdims; i++) | |
508 { | |
5760 | 509 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4805 | 510 return false; |
511 if (swap) | |
4944 | 512 swap_bytes<4> (&di); |
4805 | 513 dv(i) = di; |
514 } | |
515 | |
5157 | 516 // Convert an array with a single dimension to be a row vector. |
517 // Octave should never write files like this, other software | |
518 // might. | |
519 | |
520 if (mdims == 1) | |
521 { | |
522 mdims = 2; | |
523 dv.resize (mdims); | |
524 dv(1) = dv(0); | |
525 dv(0) = 1; | |
526 } | |
527 | |
4805 | 528 charNDArray m(dv); |
529 char *tmp = m.fortran_vec (); | |
530 is.read (tmp, dv.numel ()); | |
531 | |
532 if (error_state || ! is) | |
4687 | 533 return false; |
4805 | 534 matrix = m; |
535 } | |
536 else | |
537 { | |
538 charMatrix chm (elements, 0); | |
539 int max_len = 0; | |
540 for (int i = 0; i < elements; i++) | |
4687 | 541 { |
5828 | 542 int32_t len; |
5760 | 543 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4805 | 544 return false; |
545 if (swap) | |
4944 | 546 swap_bytes<4> (&len); |
6151 | 547 charMatrix btmp (1, len); |
548 char *pbtmp = btmp.fortran_vec (); | |
549 if (! is.read (pbtmp, len)) | |
4805 | 550 return false; |
551 if (len > max_len) | |
552 { | |
553 max_len = len; | |
554 chm.resize (elements, max_len, 0); | |
555 } | |
556 chm.insert (btmp, i, 0); | |
4687 | 557 } |
4805 | 558 matrix = chm; |
4687 | 559 } |
560 return true; | |
561 } | |
562 | |
563 #if defined (HAVE_HDF5) | |
4944 | 564 |
4687 | 565 bool |
566 octave_char_matrix_str::save_hdf5 (hid_t loc_id, const char *name, | |
567 bool /* save_as_floats */) | |
568 { | |
4837 | 569 dim_vector dv = dims (); |
570 int empty = save_hdf5_empty (loc_id, name, dv); | |
571 if (empty) | |
4805 | 572 return (empty > 0); |
4687 | 573 |
4837 | 574 int rank = dv.length (); |
4805 | 575 hid_t space_hid = -1, data_hid = -1; |
576 bool retval = true; | |
577 charNDArray m = char_array_value (); | |
578 | |
579 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
4687 | 580 |
4805 | 581 // Octave uses column-major, while HDF5 uses row-major ordering |
582 for (int i = 0; i < rank; i++) | |
4837 | 583 hdims[i] = dv (rank-i-1); |
4805 | 584 |
4815 | 585 space_hid = H5Screate_simple (rank, hdims, 0); |
4805 | 586 if (space_hid < 0) |
587 return false; | |
4687 | 588 |
4805 | 589 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
590 H5P_DEFAULT); | |
591 if (data_hid < 0) | |
4687 | 592 { |
4805 | 593 H5Sclose (space_hid); |
4687 | 594 return false; |
595 } | |
596 | |
4837 | 597 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687 | 598 |
4837 | 599 for (int i = 0; i < dv.numel (); ++i) |
4805 | 600 s[i] = m(i); |
4687 | 601 |
4805 | 602 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815 | 603 H5P_DEFAULT, s) >= 0; |
4687 | 604 |
605 H5Dclose (data_hid); | |
606 H5Sclose (space_hid); | |
4837 | 607 |
4687 | 608 return retval; |
609 } | |
610 | |
611 bool | |
612 octave_char_matrix_str::load_hdf5 (hid_t loc_id, const char *name, | |
613 bool /* have_h5giterate_bug */) | |
614 { | |
4837 | 615 bool retval = false; |
616 | |
4805 | 617 dim_vector dv; |
618 int empty = load_hdf5_empty (loc_id, name, dv); | |
619 if (empty > 0) | |
620 matrix.resize(dv); | |
4837 | 621 if (empty) |
622 return (empty > 0); | |
4805 | 623 |
4687 | 624 hid_t data_hid = H5Dopen (loc_id, name); |
625 hid_t space_hid = H5Dget_space (data_hid); | |
626 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
627 hid_t type_hid = H5Dget_type (data_hid); | |
4805 | 628 hid_t type_class_hid = H5Tget_class (type_hid); |
4687 | 629 |
4805 | 630 if (type_class_hid == H5T_INTEGER) |
4687 | 631 { |
4805 | 632 if (rank < 1) |
4687 | 633 { |
634 H5Tclose (type_hid); | |
635 H5Sclose (space_hid); | |
636 H5Dclose (data_hid); | |
637 return false; | |
638 } | |
4805 | 639 |
640 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
641 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
642 | |
643 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
644 | |
645 // Octave uses column-major, while HDF5 uses row-major ordering | |
646 if (rank == 1) | |
647 { | |
648 dv.resize (2); | |
649 dv(0) = 1; | |
650 dv(1) = hdims[0]; | |
651 } | |
4687 | 652 else |
653 { | |
4805 | 654 dv.resize (rank); |
4815 | 655 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
4805 | 656 dv(j) = hdims[i]; |
657 } | |
658 | |
659 charNDArray m (dv); | |
660 char *str = m.fortran_vec (); | |
661 if (H5Dread (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, | |
4815 | 662 H5P_DEFAULT, str) >= 0) |
4805 | 663 { |
664 retval = true; | |
665 matrix = m; | |
666 } | |
667 | |
668 H5Tclose (type_hid); | |
669 H5Sclose (space_hid); | |
670 H5Dclose (data_hid); | |
671 return true; | |
672 } | |
673 else | |
674 { | |
675 // This is cruft for backward compatiability and easy data | |
676 // importation | |
677 if (rank == 0) | |
678 { | |
679 // a single string: | |
680 int slen = H5Tget_size (type_hid); | |
681 if (slen < 0) | |
4687 | 682 { |
683 H5Tclose (type_hid); | |
684 H5Sclose (space_hid); | |
685 H5Dclose (data_hid); | |
686 return false; | |
687 } | |
4805 | 688 else |
689 { | |
690 OCTAVE_LOCAL_BUFFER (char, s, slen); | |
691 // create datatype for (null-terminated) string | |
692 // to read into: | |
693 hid_t st_id = H5Tcopy (H5T_C_S1); | |
694 H5Tset_size (st_id, slen); | |
5760 | 695 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, s) < 0) |
4805 | 696 { |
697 H5Tclose (st_id); | |
698 H5Tclose (type_hid); | |
699 H5Sclose (space_hid); | |
700 H5Dclose (data_hid); | |
701 return false; | |
702 } | |
4687 | 703 |
4805 | 704 matrix = charMatrix (s); |
4687 | 705 |
4805 | 706 H5Tclose (st_id); |
707 H5Tclose (type_hid); | |
708 H5Sclose (space_hid); | |
709 H5Dclose (data_hid); | |
710 return true; | |
711 } | |
4687 | 712 } |
4805 | 713 else if (rank == 1) |
714 { | |
715 // string vector | |
716 hsize_t elements, maxdim; | |
717 H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); | |
718 int slen = H5Tget_size (type_hid); | |
719 if (slen < 0) | |
720 { | |
721 H5Tclose (type_hid); | |
722 H5Sclose (space_hid); | |
723 H5Dclose (data_hid); | |
724 return false; | |
725 } | |
726 else | |
727 { | |
728 // hdf5 string arrays store strings of all the | |
729 // same physical length (I think), which is | |
730 // slightly wasteful, but oh well. | |
731 | |
732 OCTAVE_LOCAL_BUFFER (char, s, elements * slen); | |
733 | |
734 // create datatype for (null-terminated) string | |
735 // to read into: | |
736 hid_t st_id = H5Tcopy (H5T_C_S1); | |
737 H5Tset_size (st_id, slen); | |
738 | |
5760 | 739 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, s) < 0) |
4805 | 740 { |
741 H5Tclose (st_id); | |
742 H5Tclose (type_hid); | |
743 H5Sclose (space_hid); | |
744 H5Dclose (data_hid); | |
745 return false; | |
746 } | |
747 | |
748 charMatrix chm (elements, slen - 1); | |
749 for (hsize_t i = 0; i < elements; ++i) | |
750 { | |
751 chm.insert (s + i*slen, i, 0); | |
752 } | |
753 | |
754 matrix = chm; | |
755 | |
756 H5Tclose (st_id); | |
757 H5Tclose (type_hid); | |
758 H5Sclose (space_hid); | |
759 H5Dclose (data_hid); | |
760 return true; | |
761 } | |
762 } | |
763 else | |
4687 | 764 { |
765 H5Tclose (type_hid); | |
766 H5Sclose (space_hid); | |
767 H5Dclose (data_hid); | |
768 return false; | |
769 } | |
770 } | |
4837 | 771 |
772 return retval; | |
4687 | 773 } |
4944 | 774 |
4687 | 775 #endif |
776 | |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
777 #define MACRO_WRAPPER(FCN, CTYPE_FCN) \ |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
778 static int x ## FCN (int c) { return CTYPE_FCN (c); } |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
779 |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
780 #define STRING_MAPPER(FCN, AMAP, CTYPE_FCN) \ |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
781 MACRO_WRAPPER (FCN, CTYPE_FCN) \ |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
782 \ |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
783 octave_value \ |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
784 octave_char_matrix_str::FCN (void) const \ |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
785 { \ |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
786 static charNDArray::mapper smap = x ## FCN; \ |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
787 return matrix.AMAP (smap); \ |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
788 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
789 |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
790 #define TOSTRING_MAPPER(FCN, AMAP, CTYPE_FCN) \ |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
791 MACRO_WRAPPER (FCN, CTYPE_FCN) \ |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
792 \ |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
793 octave_value \ |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
794 octave_char_matrix_str::FCN (void) const \ |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
795 { \ |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
796 static charNDArray::mapper smap = x ## FCN; \ |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
797 return octave_value (matrix.AMAP (smap), true, \ |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
798 is_sq_string () ? '\'' : '"'); \ |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
799 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
800 |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
801 STRING_MAPPER (xisalnum, bmap, isalnum) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
802 STRING_MAPPER (xisalpha, bmap, isalpha) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
803 STRING_MAPPER (xisascii, bmap, isascii) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
804 STRING_MAPPER (xiscntrl, bmap, iscntrl) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
805 STRING_MAPPER (xisdigit, bmap, isdigit) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
806 STRING_MAPPER (xisgraph, bmap, isgraph) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
807 STRING_MAPPER (xislower, bmap, islower) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
808 STRING_MAPPER (xisprint, bmap, isprint) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
809 STRING_MAPPER (xispunct, bmap, ispunct) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
810 STRING_MAPPER (xisspace, bmap, isspace) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
811 STRING_MAPPER (xisupper, bmap, isupper) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
812 STRING_MAPPER (xisxdigit, bmap, isxdigit) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
813 STRING_MAPPER (xtoascii, dmap, toascii) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
814 TOSTRING_MAPPER (xtolower, smap, tolower) |
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
815 TOSTRING_MAPPER (xtoupper, smap, toupper) |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
816 |
2376 | 817 /* |
818 ;;; Local Variables: *** | |
819 ;;; mode: C++ *** | |
820 ;;; End: *** | |
821 */ |