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