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