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