Mercurial > hg > octave-lojdl
annotate src/ov-str-mat.cc @ 10160:cd96d29c5efa
remove Emacs local-variable settings from source files in src directory
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 20 Jan 2010 20:39:26 -0500 |
parents | ac69e6f4b33d |
children | 57a59eae83cc |
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 |
9071
034800482c79
fix default string->real array conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
71 if (! error_state) |
034800482c79
fix default string->real array conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
72 { |
034800482c79
fix default string->real array conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
73 if (nda.numel () == 1) |
034800482c79
fix default string->real array conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
74 retval = new octave_scalar (nda(0)); |
034800482c79
fix default string->real array conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
75 else |
034800482c79
fix default string->real array conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
76 retval = new octave_matrix (nda); |
034800482c79
fix default string->real array conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
77 } |
5033 | 78 |
79 return retval; | |
2376 | 80 } |
81 | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8168
diff
changeset
|
82 octave_base_value::type_conv_info |
2376 | 83 octave_char_matrix_str::numeric_conversion_function (void) const |
84 { | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8168
diff
changeset
|
85 return octave_base_value::type_conv_info (default_numeric_conversion_function, |
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8168
diff
changeset
|
86 octave_matrix::static_type_id ()); |
2376 | 87 } |
88 | |
89 octave_value | |
5400 | 90 octave_char_matrix_str::do_index_op_internal (const octave_value_list& idx, |
5885 | 91 bool resize_ok, char type) |
2407 | 92 { |
93 octave_value retval; | |
94 | |
5275 | 95 octave_idx_type len = idx.length (); |
2407 | 96 |
97 switch (len) | |
98 { | |
5539 | 99 case 0: |
9689
34d6f005db4b
eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents:
9071
diff
changeset
|
100 retval = octave_value (matrix, type); |
2407 | 101 break; |
102 | |
103 case 1: | |
104 { | |
105 idx_vector i = idx (0).index_vector (); | |
106 | |
5086 | 107 if (! error_state) |
108 retval = octave_value (charNDArray (matrix.index (i, resize_ok)), | |
9689
34d6f005db4b
eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents:
9071
diff
changeset
|
109 type); |
2407 | 110 } |
111 break; | |
112 | |
5539 | 113 case 2: |
114 { | |
115 idx_vector i = idx (0).index_vector (); | |
116 idx_vector j = idx (1).index_vector (); | |
117 | |
118 if (! error_state) | |
119 retval = octave_value (charNDArray (matrix.index (i, j, resize_ok)), | |
9689
34d6f005db4b
eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents:
9071
diff
changeset
|
120 type); |
5539 | 121 } |
5435 | 122 break; |
123 | |
2407 | 124 default: |
4513 | 125 { |
126 Array<idx_vector> idx_vec (len); | |
127 | |
5275 | 128 for (octave_idx_type i = 0; i < len; i++) |
4513 | 129 idx_vec(i) = idx(i).index_vector (); |
130 | |
5086 | 131 if (! error_state) |
132 retval = octave_value (charNDArray (matrix.index (idx_vec, resize_ok)), | |
9689
34d6f005db4b
eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents:
9071
diff
changeset
|
133 type); |
4513 | 134 } |
2407 | 135 break; |
136 } | |
137 | |
138 return retval; | |
139 } | |
140 | |
5731 | 141 octave_value |
142 octave_char_matrix_str::resize (const dim_vector& dv, bool fill) const | |
143 { | |
144 charNDArray retval (matrix); | |
145 if (fill) | |
9689
34d6f005db4b
eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents:
9071
diff
changeset
|
146 retval.resize (dv, charNDArray::resize_fill_value ()); |
5731 | 147 else |
148 retval.resize (dv); | |
9689
34d6f005db4b
eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents:
9071
diff
changeset
|
149 return octave_value (retval, is_sq_string () ? '\'' : '"'); |
5731 | 150 } |
151 | |
4668 | 152 #define CHAR_MATRIX_CONV(T, INIT, TNAME, FCN) \ |
153 T retval INIT; \ | |
154 \ | |
155 if (! force_string_conv) \ | |
156 gripe_invalid_conversion ("string", TNAME); \ | |
157 else \ | |
158 { \ | |
5878 | 159 warning_with_id ("Octave:str-to-num", \ |
5781 | 160 "implicit conversion from %s to %s", \ |
161 "string", TNAME); \ | |
4668 | 162 \ |
163 retval = octave_char_matrix::FCN (); \ | |
164 } \ | |
165 \ | |
166 return retval | |
167 | |
4643 | 168 double |
169 octave_char_matrix_str::double_value (bool force_string_conv) const | |
170 { | |
4668 | 171 CHAR_MATRIX_CONV (double, = 0, "real scalar", double_value); |
172 } | |
4643 | 173 |
4668 | 174 Complex |
175 octave_char_matrix_str::complex_value (bool force_string_conv) const | |
176 { | |
177 CHAR_MATRIX_CONV (Complex, = 0, "complex scalar", complex_value); | |
4643 | 178 } |
179 | |
2376 | 180 Matrix |
181 octave_char_matrix_str::matrix_value (bool force_string_conv) const | |
182 { | |
4668 | 183 CHAR_MATRIX_CONV (Matrix, , "real matrix", matrix_value); |
184 } | |
185 | |
186 ComplexMatrix | |
187 octave_char_matrix_str::complex_matrix_value (bool force_string_conv) const | |
188 { | |
189 CHAR_MATRIX_CONV (ComplexMatrix, , "complex matrix", complex_matrix_value); | |
190 } | |
2376 | 191 |
4668 | 192 NDArray |
193 octave_char_matrix_str::array_value (bool force_string_conv) const | |
194 { | |
195 CHAR_MATRIX_CONV (NDArray, , "real N-d array", array_value); | |
196 } | |
2376 | 197 |
4668 | 198 ComplexNDArray |
199 octave_char_matrix_str::complex_array_value (bool force_string_conv) const | |
200 { | |
201 CHAR_MATRIX_CONV (ComplexNDArray, , "complex N-d array", | |
202 complex_array_value); | |
2376 | 203 } |
204 | |
2493 | 205 string_vector |
5715 | 206 octave_char_matrix_str::all_strings (bool) const |
2376 | 207 { |
4513 | 208 string_vector retval; |
209 | |
210 if (matrix.ndims () == 2) | |
211 { | |
6816 | 212 charMatrix chm = matrix.matrix_value (); |
4513 | 213 |
6816 | 214 octave_idx_type n = chm.rows (); |
2493 | 215 |
6816 | 216 retval.resize (n); |
2493 | 217 |
6816 | 218 for (octave_idx_type i = 0; i < n; i++) |
219 retval[i] = chm.row_as_string (i); | |
4513 | 220 } |
221 else | |
222 error ("invalid conversion of charNDArray to string_vector"); | |
2493 | 223 |
224 return retval; | |
2376 | 225 } |
226 | |
3536 | 227 std::string |
4457 | 228 octave_char_matrix_str::string_value (bool) const |
2376 | 229 { |
4513 | 230 std::string retval; |
231 | |
232 if (matrix.ndims () == 2) | |
233 { | |
234 charMatrix chm = matrix.matrix_value (); | |
235 | |
5775 | 236 retval = chm.row_as_string (0); // FIXME??? |
4513 | 237 } |
238 else | |
239 error ("invalid conversion of charNDArray to string"); | |
240 | |
241 return retval; | |
2376 | 242 } |
243 | |
244 void | |
3523 | 245 octave_char_matrix_str::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2376 | 246 { |
3219 | 247 octave_print_internal (os, matrix, pr_as_read_syntax, |
248 current_print_indent_level (), true); | |
2376 | 249 } |
250 | |
4687 | 251 bool |
6974 | 252 octave_char_matrix_str::save_ascii (std::ostream& os) |
4687 | 253 { |
4805 | 254 dim_vector d = dims (); |
255 if (d.length () > 2) | |
256 { | |
257 charNDArray tmp = char_array_value (); | |
258 os << "# ndims: " << d.length () << "\n"; | |
259 for (int i=0; i < d.length (); i++) | |
260 os << " " << d (i); | |
261 os << "\n"; | |
5760 | 262 os.write (tmp.fortran_vec (), d.numel ()); |
4805 | 263 os << "\n"; |
264 } | |
265 else | |
4687 | 266 { |
4805 | 267 // Keep this case, rather than use generic code above for |
268 // backward compatiability. Makes load_ascii much more complex!! | |
269 charMatrix chm = char_matrix_value (); | |
5275 | 270 octave_idx_type elements = chm.rows (); |
4805 | 271 os << "# elements: " << elements << "\n"; |
5275 | 272 for (octave_idx_type i = 0; i < elements; i++) |
4805 | 273 { |
274 unsigned len = chm.cols (); | |
275 os << "# length: " << len << "\n"; | |
276 std::string tstr = chm.row_as_string (i, false, true); | |
277 const char *tmp = tstr.data (); | |
278 if (tstr.length () > len) | |
279 panic_impossible (); | |
5760 | 280 os.write (tmp, len); |
4805 | 281 os << "\n"; |
282 } | |
4687 | 283 } |
284 | |
285 return true; | |
286 } | |
287 | |
288 bool | |
289 octave_char_matrix_str::load_ascii (std::istream& is) | |
290 { | |
291 bool success = true; | |
5099 | 292 |
293 string_vector keywords(3); | |
294 | |
295 keywords[0] = "ndims"; | |
296 keywords[1] = "elements"; | |
297 keywords[2] = "length"; | |
298 | |
299 std::string kw; | |
300 int val = 0; | |
4687 | 301 |
5099 | 302 if (extract_keyword (is, keywords, kw, val, true)) |
4687 | 303 { |
5099 | 304 if (kw == "ndims") |
4687 | 305 { |
5099 | 306 int mdims = val; |
307 | |
308 if (mdims >= 0) | |
309 { | |
310 dim_vector dv; | |
311 dv.resize (mdims); | |
4805 | 312 |
5099 | 313 for (int i = 0; i < mdims; i++) |
314 is >> dv(i); | |
4687 | 315 |
6717 | 316 if (is) |
317 { | |
318 charNDArray tmp(dv); | |
319 | |
320 if (tmp.is_empty ()) | |
321 matrix = tmp; | |
322 else | |
323 { | |
324 char *ftmp = tmp.fortran_vec (); | |
5099 | 325 |
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
|
326 skip_preceeding_newline (is); |
4805 | 327 |
6717 | 328 if (! is.read (ftmp, dv.numel ()) || !is) |
329 { | |
330 error ("load: failed to load string constant"); | |
331 success = false; | |
332 } | |
333 else | |
334 matrix = tmp; | |
335 } | |
336 } | |
337 else | |
5099 | 338 { |
6717 | 339 error ("load: failed to read dimensions"); |
5099 | 340 success = false; |
341 } | |
342 } | |
343 else | |
4687 | 344 { |
5099 | 345 error ("load: failed to extract matrix size"); |
4805 | 346 success = false; |
4687 | 347 } |
348 } | |
5099 | 349 else if (kw == "elements") |
4687 | 350 { |
5099 | 351 int elements = val; |
4805 | 352 |
353 if (elements >= 0) | |
354 { | |
5775 | 355 // FIXME -- need to be able to get max length |
4805 | 356 // before doing anything. |
4687 | 357 |
4805 | 358 charMatrix chm (elements, 0); |
359 int max_len = 0; | |
360 for (int i = 0; i < elements; i++) | |
361 { | |
362 int len; | |
363 if (extract_keyword (is, "length", len) && len >= 0) | |
364 { | |
6151 | 365 // Use this instead of a C-style character |
366 // buffer so that we can properly handle | |
367 // embedded NUL characters. | |
368 charMatrix tmp (1, len); | |
369 char *ptmp = tmp.fortran_vec (); | |
370 | |
371 if (len > 0 && ! is.read (ptmp, len)) | |
4805 | 372 { |
373 error ("load: failed to load string constant"); | |
374 success = false; | |
375 break; | |
376 } | |
377 else | |
378 { | |
379 if (len > max_len) | |
380 { | |
381 max_len = len; | |
382 chm.resize (elements, max_len, 0); | |
383 } | |
6151 | 384 |
4805 | 385 chm.insert (tmp, i, 0); |
386 } | |
387 } | |
388 else | |
389 { | |
390 error ("load: failed to extract string length for element %d", | |
391 i+1); | |
392 success = false; | |
393 } | |
394 } | |
395 | |
396 if (! error_state) | |
397 matrix = chm; | |
4687 | 398 } |
399 else | |
400 { | |
4805 | 401 error ("load: failed to extract number of string elements"); |
402 success = false; | |
403 } | |
404 } | |
5099 | 405 else if (kw == "length") |
4805 | 406 { |
5099 | 407 int len = val; |
4805 | 408 |
5099 | 409 if (len >= 0) |
4805 | 410 { |
411 // This is cruft for backward compatiability, | |
412 // but relatively harmless. | |
413 | |
6151 | 414 // Use this instead of a C-style character buffer so |
415 // that we can properly handle embedded NUL characters. | |
416 charMatrix tmp (1, len); | |
417 char *ptmp = tmp.fortran_vec (); | |
4805 | 418 |
6151 | 419 if (len > 0 && ! is.read (ptmp, len)) |
4805 | 420 { |
421 error ("load: failed to load string constant"); | |
422 } | |
4687 | 423 else |
4805 | 424 { |
425 if (is) | |
6151 | 426 matrix = tmp; |
4805 | 427 else |
428 error ("load: failed to load string constant"); | |
429 } | |
4687 | 430 } |
431 } | |
5099 | 432 else |
433 panic_impossible (); | |
434 } | |
435 else | |
436 { | |
437 error ("load: failed to extract number of rows and columns"); | |
438 success = false; | |
4687 | 439 } |
440 | |
441 return success; | |
442 } | |
443 | |
444 bool | |
445 octave_char_matrix_str::save_binary (std::ostream& os, | |
446 bool& /* save_as_floats */) | |
447 { | |
4805 | 448 dim_vector d = dims (); |
449 if (d.length() < 1) | |
450 return false; | |
451 | |
452 // Use negative value for ndims to differentiate with old format!! | |
5828 | 453 int32_t tmp = - d.length(); |
5760 | 454 os.write (reinterpret_cast<char *> (&tmp), 4); |
4805 | 455 for (int i=0; i < d.length (); i++) |
4687 | 456 { |
4805 | 457 tmp = d(i); |
5760 | 458 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687 | 459 } |
4805 | 460 |
461 charNDArray m = char_array_value (); | |
462 os.write (m.fortran_vec (), d.numel ()); | |
4687 | 463 return true; |
464 } | |
465 | |
466 bool | |
467 octave_char_matrix_str::load_binary (std::istream& is, bool swap, | |
468 oct_mach_info::float_format /* fmt */) | |
469 { | |
5828 | 470 int32_t elements; |
5760 | 471 if (! is.read (reinterpret_cast<char *> (&elements), 4)) |
4687 | 472 return false; |
473 if (swap) | |
4944 | 474 swap_bytes<4> (&elements); |
4805 | 475 |
476 if (elements < 0) | |
4687 | 477 { |
5828 | 478 int32_t mdims = - elements; |
479 int32_t di; | |
4805 | 480 dim_vector dv; |
481 dv.resize (mdims); | |
482 | |
483 for (int i = 0; i < mdims; i++) | |
484 { | |
5760 | 485 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4805 | 486 return false; |
487 if (swap) | |
4944 | 488 swap_bytes<4> (&di); |
4805 | 489 dv(i) = di; |
490 } | |
491 | |
5157 | 492 // Convert an array with a single dimension to be a row vector. |
493 // Octave should never write files like this, other software | |
494 // might. | |
495 | |
496 if (mdims == 1) | |
497 { | |
498 mdims = 2; | |
499 dv.resize (mdims); | |
500 dv(1) = dv(0); | |
501 dv(0) = 1; | |
502 } | |
503 | |
4805 | 504 charNDArray m(dv); |
505 char *tmp = m.fortran_vec (); | |
506 is.read (tmp, dv.numel ()); | |
507 | |
508 if (error_state || ! is) | |
4687 | 509 return false; |
4805 | 510 matrix = m; |
511 } | |
512 else | |
513 { | |
514 charMatrix chm (elements, 0); | |
515 int max_len = 0; | |
516 for (int i = 0; i < elements; i++) | |
4687 | 517 { |
5828 | 518 int32_t len; |
5760 | 519 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
4805 | 520 return false; |
521 if (swap) | |
4944 | 522 swap_bytes<4> (&len); |
6151 | 523 charMatrix btmp (1, len); |
524 char *pbtmp = btmp.fortran_vec (); | |
525 if (! is.read (pbtmp, len)) | |
4805 | 526 return false; |
527 if (len > max_len) | |
528 { | |
529 max_len = len; | |
530 chm.resize (elements, max_len, 0); | |
531 } | |
532 chm.insert (btmp, i, 0); | |
4687 | 533 } |
4805 | 534 matrix = chm; |
4687 | 535 } |
536 return true; | |
537 } | |
538 | |
539 #if defined (HAVE_HDF5) | |
4944 | 540 |
4687 | 541 bool |
542 octave_char_matrix_str::save_hdf5 (hid_t loc_id, const char *name, | |
543 bool /* save_as_floats */) | |
544 { | |
4837 | 545 dim_vector dv = dims (); |
546 int empty = save_hdf5_empty (loc_id, name, dv); | |
547 if (empty) | |
4805 | 548 return (empty > 0); |
4687 | 549 |
4837 | 550 int rank = dv.length (); |
4805 | 551 hid_t space_hid = -1, data_hid = -1; |
552 bool retval = true; | |
553 charNDArray m = char_array_value (); | |
554 | |
555 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
4687 | 556 |
4805 | 557 // Octave uses column-major, while HDF5 uses row-major ordering |
558 for (int i = 0; i < rank; i++) | |
4837 | 559 hdims[i] = dv (rank-i-1); |
4805 | 560 |
4815 | 561 space_hid = H5Screate_simple (rank, hdims, 0); |
4805 | 562 if (space_hid < 0) |
563 return false; | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
564 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
565 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
566 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
567 #else |
4805 | 568 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
569 H5P_DEFAULT); | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
570 #endif |
4805 | 571 if (data_hid < 0) |
4687 | 572 { |
4805 | 573 H5Sclose (space_hid); |
4687 | 574 return false; |
575 } | |
576 | |
4837 | 577 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687 | 578 |
4837 | 579 for (int i = 0; i < dv.numel (); ++i) |
4805 | 580 s[i] = m(i); |
4687 | 581 |
4805 | 582 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4815 | 583 H5P_DEFAULT, s) >= 0; |
4687 | 584 |
585 H5Dclose (data_hid); | |
586 H5Sclose (space_hid); | |
4837 | 587 |
4687 | 588 return retval; |
589 } | |
590 | |
591 bool | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
9689
diff
changeset
|
592 octave_char_matrix_str::load_hdf5 (hid_t loc_id, const char *name) |
4687 | 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 |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
603 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
604 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
605 #else |
4687 | 606 hid_t data_hid = H5Dopen (loc_id, name); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
607 #endif |
4687 | 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 |