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