Mercurial > octave-nkf
annotate libinterp/octave-value/ov-str-mat.cc @ 20574:dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
* lo-array-gripes.h, lo-array-gripes.cc (index_exception):
New base class for indexing errors.
(invalid_index, out_of_range): New classes.
(gripe_index_out_of_range): New overloaded function.
(gripe_invalid_index): New overloaded functions.
Delete version with no arguments.
(gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch):
Delete.
Change uses of gripe functions as needed.
* Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use
exceptions to collect error info about and handle indexing errors.
* data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__,
do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__):
Likewise.
* oct-map.cc (octave_map::index, octave_map::assign,
octave_map::delete_elements): Likewise.
* sparse.cc (Fsparse): Likewise.
* sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests.
* utils.cc (dims_to_numel): Likewise.
* ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op,
octave_base_diag<DMT, MT>::subsasgn): Likewise.
* ov-base-mat.cc (octave_base_matrix<MT>::subsref,
octave_base_matrix<MT>::assign): Likewise.
* ov-base-sparse.cc (octave_base_sparse<T>::do_index_op,
octave_base_sparse<T>::assign,
octave_base_sparse<MT>::delete_elements): Likewise.
* ov-classdef.cc (cdef_object_array::subsref,
cdef_object_array::subsasgn): Likewise.
* ov-java.cc (make_java_index): Likewise.
* ov-perm.cc (octave_perm_matrix::do_index_op): Likewise.
* ov-range.cc (octave_range::do_index_op): Likewise.
* ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise.
* ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise.
* pt-assign.cc (tree_simple_assignment::rvalue1): Likewise.
* pt-idx.cc (tree_index_expression::rvalue,
tree_index_expression::lvalue): Likewise.
* Array-util.cc (sub2ind): Likewise.
* toplev.cc (main_loop): Also catch unhandled index_exception
exceptions.
* ov-base.cc (octave_base_value::index_vector): Improve error message.
* ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise.
* ov-complex.cc (complex_index): New class.
(gripe_complex_index): New function.
(octave_complex::index_vector): Use it.
* pt-id.h, pt-id.cc (tree_identifier::is_variable,
tree_black_hole::is_variable): Now const.
* pt-idx.cc (final_index_error): New static function.
(tree_index_expression::rvalue, tree_index_expression::lvalue):
Use it.
* index.tst: New tests.
author | Lachlan Andrew <lachlanbis@gmail.com> |
---|---|
date | Fri, 02 Oct 2015 15:07:37 -0400 |
parents | df4165dfc676 |
children | f90c8372b7ba |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19632
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
11523 | 4 Copyright (C) 2009-2010 VZLU Prague |
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" |
20482
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20253
diff
changeset
|
44 #include "ls-oct-text.h" |
5758 | 45 #include "ls-utils.h" |
2407 | 46 #include "oct-obj.h" |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
47 #include "oct-hdf5.h" |
4944 | 48 #include "oct-stream.h" |
2376 | 49 #include "ops.h" |
5033 | 50 #include "ov-scalar.h" |
2376 | 51 #include "ov-re-mat.h" |
52 #include "ov-str-mat.h" | |
53 #include "pr-output.h" | |
3836 | 54 #include "pt-mat.h" |
5758 | 55 #include "utils.h" |
4687 | 56 |
2376 | 57 |
4612 | 58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_str, "string", "char"); |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
59 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_sq_str, "sq_string", |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
60 "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, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
91 bool resize_ok, char type) |
2407 | 92 { |
93 octave_value retval; | |
94 | |
5275 | 95 octave_idx_type len = idx.length (); |
2407 | 96 |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
97 // If we catch an indexing error in index_vector, we flag an error in |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
98 // index k. Ensure it is the right value befor each idx_vector call. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
99 // Same variable as used in the for loop in the default case. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
100 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
101 octave_idx_type k = 0; |
2407 | 102 |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
103 try |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
104 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
105 switch (len) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
106 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
107 case 0: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
108 retval = octave_value (matrix, type); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
109 break; |
2407 | 110 |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
111 case 1: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
112 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
113 idx_vector i = idx (0).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
114 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
115 if (! error_state) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
116 retval = octave_value (charNDArray (matrix.index (i, resize_ok)), |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
117 type); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
118 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
119 break; |
2407 | 120 |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
121 case 2: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
122 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
123 idx_vector i = idx (0).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
124 k = 1; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
125 idx_vector j = idx (1).index_vector (); |
5539 | 126 |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
127 if (! error_state) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
128 retval = octave_value (charNDArray (matrix.index (i, j, resize_ok)), |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
129 type); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
130 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
131 break; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
132 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
133 default: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
134 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
135 Array<idx_vector> idx_vec (dim_vector (len, 1)); |
5435 | 136 |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
137 for (k = 0; k < len; k++) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
138 idx_vec(k) = idx(k).index_vector (); |
4513 | 139 |
20574
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
140 if (! error_state) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
141 retval = |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
142 octave_value (charNDArray (matrix.index (idx_vec, resize_ok)), |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
143 type); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
144 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
145 break; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
146 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
147 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
148 catch (index_exception& e) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
149 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
150 // Rethrow to allow more info to be reported later. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
151 e.set_pos_if_unset (len, k+1); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20484
diff
changeset
|
152 throw; |
2407 | 153 } |
154 | |
155 return retval; | |
156 } | |
157 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
158 octave_value |
5731 | 159 octave_char_matrix_str::resize (const dim_vector& dv, bool fill) const |
160 { | |
161 charNDArray retval (matrix); | |
162 if (fill) | |
14616
13cc11418393
improve handling of default resize fill value for arrays
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
163 retval.resize (dv, 0); |
5731 | 164 else |
165 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
|
166 return octave_value (retval, is_sq_string () ? '\'' : '"'); |
5731 | 167 } |
168 | |
4668 | 169 #define CHAR_MATRIX_CONV(T, INIT, TNAME, FCN) \ |
170 T retval INIT; \ | |
171 \ | |
172 if (! force_string_conv) \ | |
173 gripe_invalid_conversion ("string", TNAME); \ | |
174 else \ | |
175 { \ | |
5878 | 176 warning_with_id ("Octave:str-to-num", \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
177 "implicit conversion from %s to %s", \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 "string", TNAME); \ |
4668 | 179 \ |
180 retval = octave_char_matrix::FCN (); \ | |
181 } \ | |
182 \ | |
183 return retval | |
184 | |
4643 | 185 double |
186 octave_char_matrix_str::double_value (bool force_string_conv) const | |
187 { | |
4668 | 188 CHAR_MATRIX_CONV (double, = 0, "real scalar", double_value); |
189 } | |
4643 | 190 |
4668 | 191 Complex |
192 octave_char_matrix_str::complex_value (bool force_string_conv) const | |
193 { | |
194 CHAR_MATRIX_CONV (Complex, = 0, "complex scalar", complex_value); | |
4643 | 195 } |
196 | |
2376 | 197 Matrix |
198 octave_char_matrix_str::matrix_value (bool force_string_conv) const | |
199 { | |
4668 | 200 CHAR_MATRIX_CONV (Matrix, , "real matrix", matrix_value); |
201 } | |
202 | |
203 ComplexMatrix | |
204 octave_char_matrix_str::complex_matrix_value (bool force_string_conv) const | |
205 { | |
206 CHAR_MATRIX_CONV (ComplexMatrix, , "complex matrix", complex_matrix_value); | |
207 } | |
2376 | 208 |
4668 | 209 NDArray |
210 octave_char_matrix_str::array_value (bool force_string_conv) const | |
211 { | |
212 CHAR_MATRIX_CONV (NDArray, , "real N-d array", array_value); | |
213 } | |
2376 | 214 |
4668 | 215 ComplexNDArray |
216 octave_char_matrix_str::complex_array_value (bool force_string_conv) const | |
217 { | |
218 CHAR_MATRIX_CONV (ComplexNDArray, , "complex N-d array", | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
219 complex_array_value); |
2376 | 220 } |
221 | |
2493 | 222 string_vector |
5715 | 223 octave_char_matrix_str::all_strings (bool) const |
2376 | 224 { |
4513 | 225 string_vector retval; |
226 | |
227 if (matrix.ndims () == 2) | |
228 { | |
19350
6c9ea5be96bf
Change charMatrix to subclass charNDArray rather than be another Array<char>.
Carnë Draug <carandraug@octave.org>
parents:
18099
diff
changeset
|
229 charMatrix chm (matrix); |
4513 | 230 |
6816 | 231 octave_idx_type n = chm.rows (); |
2493 | 232 |
6816 | 233 retval.resize (n); |
2493 | 234 |
6816 | 235 for (octave_idx_type i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
236 retval[i] = chm.row_as_string (i); |
4513 | 237 } |
238 else | |
239 error ("invalid conversion of charNDArray to string_vector"); | |
2493 | 240 |
241 return retval; | |
2376 | 242 } |
243 | |
3536 | 244 std::string |
4457 | 245 octave_char_matrix_str::string_value (bool) const |
2376 | 246 { |
4513 | 247 std::string retval; |
248 | |
249 if (matrix.ndims () == 2) | |
250 { | |
19350
6c9ea5be96bf
Change charMatrix to subclass charNDArray rather than be another Array<char>.
Carnë Draug <carandraug@octave.org>
parents:
18099
diff
changeset
|
251 charMatrix chm (matrix); |
4513 | 252 |
17861
870f3e12e163
maint: Use phrase "FIXME:" for problem areas in code.
Rik <rik@octave.org>
parents:
17825
diff
changeset
|
253 retval = chm.row_as_string (0); // FIXME? |
4513 | 254 } |
255 else | |
256 error ("invalid conversion of charNDArray to string"); | |
257 | |
258 return retval; | |
2376 | 259 } |
260 | |
10729
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
261 Array<std::string> |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
262 octave_char_matrix_str::cellstr_value (void) const |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
263 { |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
264 Array<std::string> retval; |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
265 |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
266 if (matrix.ndims () == 2) |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
267 { |
19350
6c9ea5be96bf
Change charMatrix to subclass charNDArray rather than be another Array<char>.
Carnë Draug <carandraug@octave.org>
parents:
18099
diff
changeset
|
268 const charMatrix chm (matrix); |
10729
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
269 octave_idx_type nr = chm.rows (); |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
270 retval.clear (nr, 1); |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
271 for (octave_idx_type i = 0; i < nr; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
272 retval.xelem (i) = chm.row_as_string (i); |
10729
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
273 } |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
274 else |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
275 error ("cellstr: cannot convert multidimensional arrays"); |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
276 |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
277 return retval; |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
278 } |
172253d75d94
implement cellstr extractors for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10552
diff
changeset
|
279 |
2376 | 280 void |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
281 octave_char_matrix_str::print_raw (std::ostream& os, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
282 bool pr_as_read_syntax) const |
2376 | 283 { |
3219 | 284 octave_print_internal (os, matrix, pr_as_read_syntax, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
285 current_print_indent_level (), true); |
2376 | 286 } |
287 | |
17870 | 288 void |
289 octave_char_matrix_str::short_disp (std::ostream& os) const | |
17825
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
290 { |
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
291 if (matrix.ndims () == 2 && numel () > 0) |
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
292 { |
17870 | 293 std::string tmp = string_value (); |
17825
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
294 |
17861
870f3e12e163
maint: Use phrase "FIXME:" for problem areas in code.
Rik <rik@octave.org>
parents:
17825
diff
changeset
|
295 // FIXME: should this be configurable? |
17870 | 296 size_t max_len = 100; |
17825
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
297 |
17876
b951a8351fd7
Fix display of char arrays in GUI Variable window.
Rik <rik@octave.org>
parents:
17870
diff
changeset
|
298 os << (tmp.length () > max_len ? tmp.substr (0, 100) : tmp); |
17825
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
299 } |
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
300 } |
53f433bae63b
display (possibly truncated) values of strings in workspace view
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
301 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
302 bool |
6974 | 303 octave_char_matrix_str::save_ascii (std::ostream& os) |
4687 | 304 { |
4805 | 305 dim_vector d = dims (); |
306 if (d.length () > 2) | |
307 { | |
308 charNDArray tmp = char_array_value (); | |
309 os << "# ndims: " << d.length () << "\n"; | |
310 for (int i=0; i < d.length (); i++) | |
20253
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
311 os << " " << d(i); |
4805 | 312 os << "\n"; |
5760 | 313 os.write (tmp.fortran_vec (), d.numel ()); |
4805 | 314 os << "\n"; |
315 } | |
316 else | |
4687 | 317 { |
4805 | 318 // Keep this case, rather than use generic code above for |
20484
df4165dfc676
maint: Fix misspelled word compatibility in code comments.
Rik <rik@octave.org>
parents:
20482
diff
changeset
|
319 // backward compatibility. Makes load_ascii much more complex!! |
4805 | 320 charMatrix chm = char_matrix_value (); |
5275 | 321 octave_idx_type elements = chm.rows (); |
4805 | 322 os << "# elements: " << elements << "\n"; |
5275 | 323 for (octave_idx_type i = 0; i < elements; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
324 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
325 unsigned len = chm.cols (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
326 os << "# length: " << len << "\n"; |
11265
a117dc8ea1b9
charMatrix::row_as_string: never strip trailing nul characters
John W. Eaton <jwe@octave.org>
parents:
10729
diff
changeset
|
327 std::string tstr = chm.row_as_string (i); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
328 const char *tmp = tstr.data (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
329 if (tstr.length () > len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
330 panic_impossible (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
331 os.write (tmp, len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
332 os << "\n"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
333 } |
4687 | 334 } |
335 | |
336 return true; | |
337 } | |
338 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
339 bool |
4687 | 340 octave_char_matrix_str::load_ascii (std::istream& is) |
341 { | |
342 bool success = true; | |
5099 | 343 |
344 string_vector keywords(3); | |
345 | |
346 keywords[0] = "ndims"; | |
347 keywords[1] = "elements"; | |
348 keywords[2] = "length"; | |
349 | |
350 std::string kw; | |
351 int val = 0; | |
4687 | 352 |
5099 | 353 if (extract_keyword (is, keywords, kw, val, true)) |
4687 | 354 { |
5099 | 355 if (kw == "ndims") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
356 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
357 int mdims = val; |
5099 | 358 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
359 if (mdims >= 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
360 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
361 dim_vector dv; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
362 dv.resize (mdims); |
4805 | 363 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
364 for (int i = 0; i < mdims; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
365 is >> dv(i); |
4687 | 366 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
367 if (is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
368 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
369 charNDArray tmp(dv); |
6717 | 370 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
371 if (tmp.is_empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
372 matrix = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
373 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
374 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
375 char *ftmp = tmp.fortran_vec (); |
5099 | 376 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
377 skip_preceeding_newline (is); |
4805 | 378 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
379 if (! is.read (ftmp, dv.numel ()) || !is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
380 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
381 error ("load: failed to load string constant"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
382 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
383 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
384 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
385 matrix = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
386 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
387 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
388 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
389 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
390 error ("load: failed to read dimensions"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
391 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
392 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
393 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
394 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
395 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
396 error ("load: failed to extract matrix size"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
397 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
398 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
399 } |
5099 | 400 else if (kw == "elements") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
401 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
402 int elements = val; |
4805 | 403 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
404 if (elements >= 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
405 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
406 // FIXME: need to be able to get max length before doing anything. |
4687 | 407 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
408 charMatrix chm (elements, 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
409 int max_len = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
410 for (int i = 0; i < elements; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
411 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
412 int len; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
413 if (extract_keyword (is, "length", len) && len >= 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
414 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
415 // Use this instead of a C-style character |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
416 // buffer so that we can properly handle |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
417 // embedded NUL characters. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
418 charMatrix tmp (1, len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
419 char *ptmp = tmp.fortran_vec (); |
6151 | 420 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
421 if (len > 0 && ! is.read (ptmp, len)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
422 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
423 error ("load: failed to load string constant"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
424 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
425 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
426 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
427 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
428 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
429 if (len > max_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
430 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
431 max_len = len; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
432 chm.resize (elements, max_len, 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
433 } |
6151 | 434 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
435 chm.insert (tmp, i, 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
436 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
437 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
438 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
439 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
440 error ("load: failed to extract string length for element %d", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
441 i+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
442 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
443 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
444 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
445 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
446 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
447 matrix = chm; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
448 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
449 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
450 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
451 error ("load: failed to extract number of string elements"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
452 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
453 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
454 } |
5099 | 455 else if (kw == "length") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
456 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
457 int len = val; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
458 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
459 if (len >= 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
460 { |
20484
df4165dfc676
maint: Fix misspelled word compatibility in code comments.
Rik <rik@octave.org>
parents:
20482
diff
changeset
|
461 // This is cruft for backward compatibility, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
462 // but relatively harmless. |
4805 | 463 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
464 // Use this instead of a C-style character buffer so |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
465 // that we can properly handle embedded NUL characters. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
466 charMatrix tmp (1, len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
467 char *ptmp = tmp.fortran_vec (); |
4805 | 468 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
469 if (len > 0 && ! is.read (ptmp, len)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
470 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
471 error ("load: failed to load string constant"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
472 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
473 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
474 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
475 if (is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
476 matrix = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
477 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
478 error ("load: failed to load string constant"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
479 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
480 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
481 } |
5099 | 482 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
483 panic_impossible (); |
5099 | 484 } |
485 else | |
486 { | |
487 error ("load: failed to extract number of rows and columns"); | |
488 success = false; | |
4687 | 489 } |
490 | |
491 return success; | |
492 } | |
493 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
494 bool |
4687 | 495 octave_char_matrix_str::save_binary (std::ostream& os, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
496 bool& /* save_as_floats */) |
4687 | 497 { |
4805 | 498 dim_vector d = dims (); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14616
diff
changeset
|
499 if (d.length () < 1) |
4805 | 500 return false; |
501 | |
502 // Use negative value for ndims to differentiate with old format!! | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14616
diff
changeset
|
503 int32_t tmp = - d.length (); |
5760 | 504 os.write (reinterpret_cast<char *> (&tmp), 4); |
4805 | 505 for (int i=0; i < d.length (); i++) |
4687 | 506 { |
4805 | 507 tmp = d(i); |
5760 | 508 os.write (reinterpret_cast<char *> (&tmp), 4); |
4687 | 509 } |
4805 | 510 |
511 charNDArray m = char_array_value (); | |
512 os.write (m.fortran_vec (), d.numel ()); | |
4687 | 513 return true; |
514 } | |
515 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
516 bool |
4687 | 517 octave_char_matrix_str::load_binary (std::istream& is, bool swap, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
518 oct_mach_info::float_format /* fmt */) |
4687 | 519 { |
5828 | 520 int32_t elements; |
5760 | 521 if (! is.read (reinterpret_cast<char *> (&elements), 4)) |
4687 | 522 return false; |
523 if (swap) | |
4944 | 524 swap_bytes<4> (&elements); |
4805 | 525 |
526 if (elements < 0) | |
4687 | 527 { |
5828 | 528 int32_t mdims = - elements; |
529 int32_t di; | |
4805 | 530 dim_vector dv; |
531 dv.resize (mdims); | |
532 | |
533 for (int i = 0; i < mdims; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
534 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
535 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
536 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
537 if (swap) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
538 swap_bytes<4> (&di); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
539 dv(i) = di; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
540 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
541 |
5157 | 542 // Convert an array with a single dimension to be a row vector. |
543 // Octave should never write files like this, other software | |
544 // might. | |
545 | |
546 if (mdims == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
547 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
548 mdims = 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
549 dv.resize (mdims); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
550 dv(1) = dv(0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
551 dv(0) = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
552 } |
5157 | 553 |
4805 | 554 charNDArray m(dv); |
555 char *tmp = m.fortran_vec (); | |
556 is.read (tmp, dv.numel ()); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
557 |
4805 | 558 if (error_state || ! is) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
559 return false; |
4805 | 560 matrix = m; |
561 } | |
562 else | |
563 { | |
564 charMatrix chm (elements, 0); | |
565 int max_len = 0; | |
566 for (int i = 0; i < elements; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
567 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
568 int32_t len; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
569 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
570 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
571 if (swap) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
572 swap_bytes<4> (&len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
573 charMatrix btmp (1, len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
574 char *pbtmp = btmp.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
575 if (! is.read (pbtmp, len)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
576 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
577 if (len > max_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
578 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
579 max_len = len; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
580 chm.resize (elements, max_len, 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
581 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
582 chm.insert (btmp, i, 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
583 } |
4805 | 584 matrix = chm; |
4687 | 585 } |
586 return true; | |
587 } | |
588 | |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
589 bool |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
590 octave_char_matrix_str::save_hdf5 (octave_hdf5_id loc_id, const char *name, |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
591 bool /* save_as_floats */) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
592 { |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
593 bool retval = false; |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
594 |
4687 | 595 #if defined (HAVE_HDF5) |
4944 | 596 |
4837 | 597 dim_vector dv = dims (); |
598 int empty = save_hdf5_empty (loc_id, name, dv); | |
599 if (empty) | |
4805 | 600 return (empty > 0); |
4687 | 601 |
4837 | 602 int rank = dv.length (); |
18099
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17876
diff
changeset
|
603 hid_t space_hid, data_hid; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17876
diff
changeset
|
604 space_hid = data_hid = -1; |
4805 | 605 charNDArray m = char_array_value (); |
606 | |
607 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
4687 | 608 |
4805 | 609 // Octave uses column-major, while HDF5 uses row-major ordering |
610 for (int i = 0; i < rank; i++) | |
20253
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
19897
diff
changeset
|
611 hdims[i] = dv(rank-i-1); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
612 |
4815 | 613 space_hid = H5Screate_simple (rank, hdims, 0); |
4805 | 614 if (space_hid < 0) |
615 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
|
616 #if HAVE_HDF5_18 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
617 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
618 H5P_DEFAULT, H5P_DEFAULT, 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
|
619 #else |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
620 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
621 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
|
622 #endif |
4805 | 623 if (data_hid < 0) |
4687 | 624 { |
4805 | 625 H5Sclose (space_hid); |
4687 | 626 return false; |
627 } | |
628 | |
4837 | 629 OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); |
4687 | 630 |
4837 | 631 for (int i = 0; i < dv.numel (); ++i) |
4805 | 632 s[i] = m(i); |
4687 | 633 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
634 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
635 H5P_DEFAULT, s) >= 0; |
4687 | 636 |
637 H5Dclose (data_hid); | |
638 H5Sclose (space_hid); | |
4837 | 639 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
640 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
641 gripe_save ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
642 #endif |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
643 |
4687 | 644 return retval; |
645 } | |
646 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
647 bool |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
648 octave_char_matrix_str::load_hdf5 (octave_hdf5_id loc_id, const char *name) |
4687 | 649 { |
4837 | 650 bool retval = false; |
651 | |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
652 #if defined (HAVE_HDF5) |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
653 |
4805 | 654 dim_vector dv; |
655 int empty = load_hdf5_empty (loc_id, name, dv); | |
656 if (empty > 0) | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
657 matrix.resize (dv); |
4837 | 658 if (empty) |
659 return (empty > 0); | |
4805 | 660 |
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
|
661 #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
|
662 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
|
663 #else |
4687 | 664 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
|
665 #endif |
4687 | 666 hid_t space_hid = H5Dget_space (data_hid); |
667 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
668 hid_t type_hid = H5Dget_type (data_hid); | |
4805 | 669 hid_t type_class_hid = H5Tget_class (type_hid); |
4687 | 670 |
4805 | 671 if (type_class_hid == H5T_INTEGER) |
4687 | 672 { |
4805 | 673 if (rank < 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
674 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
675 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
676 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
677 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
678 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
679 } |
4805 | 680 |
681 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
682 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
683 | |
684 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
685 | |
686 // Octave uses column-major, while HDF5 uses row-major ordering | |
687 if (rank == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
688 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
689 dv.resize (2); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
690 dv(0) = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
691 dv(1) = hdims[0]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
692 } |
4687 | 693 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
694 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
695 dv.resize (rank); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
696 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
697 dv(j) = hdims[i]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
698 } |
4805 | 699 |
700 charNDArray m (dv); | |
701 char *str = m.fortran_vec (); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
702 if (H5Dread (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
703 H5P_DEFAULT, str) >= 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
704 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
705 retval = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
706 matrix = m; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
707 } |
4805 | 708 |
709 H5Tclose (type_hid); | |
710 H5Sclose (space_hid); | |
711 H5Dclose (data_hid); | |
712 return true; | |
713 } | |
714 else | |
715 { | |
20484
df4165dfc676
maint: Fix misspelled word compatibility in code comments.
Rik <rik@octave.org>
parents:
20482
diff
changeset
|
716 // This is cruft for backward compatibility and easy data |
4805 | 717 // importation |
17643
d0a197b9962a
Fix loading of string arrays from 3rd party hdf5 files (bug #38789)
Markus Appel <masolomaster3000@gmail.com>
parents:
15195
diff
changeset
|
718 if (rank == 0) //FIXME: Does rank==0 even exist for strings in HDF5? |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
719 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
720 // a single string: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
721 int slen = H5Tget_size (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
722 if (slen < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
723 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
724 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
725 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
726 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
727 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
728 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
729 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
730 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
731 OCTAVE_LOCAL_BUFFER (char, s, slen); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
732 // create datatype for (null-terminated) string |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
733 // to read into: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
734 hid_t st_id = H5Tcopy (H5T_C_S1); |
17643
d0a197b9962a
Fix loading of string arrays from 3rd party hdf5 files (bug #38789)
Markus Appel <masolomaster3000@gmail.com>
parents:
15195
diff
changeset
|
735 H5Tset_size (st_id, slen+1); |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
736 if (H5Dread (data_hid, st_id, H5S_ALL, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
737 H5S_ALL, H5P_DEFAULT, s) < 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
738 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
739 H5Tclose (st_id); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
740 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
741 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
742 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
743 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
744 } |
4687 | 745 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
746 matrix = charMatrix (s); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
747 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
748 H5Tclose (st_id); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
749 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
750 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
751 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
752 return true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
753 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
754 } |
4805 | 755 else if (rank == 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
756 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
757 // string vector |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
758 hsize_t elements, maxdim; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
759 H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
760 int slen = H5Tget_size (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
761 if (slen < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
762 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
763 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
764 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
765 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
766 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
767 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
768 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
769 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
770 // hdf5 string arrays store strings of all the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
771 // same physical length (I think), which is |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
772 // slightly wasteful, but oh well. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
773 |
17643
d0a197b9962a
Fix loading of string arrays from 3rd party hdf5 files (bug #38789)
Markus Appel <masolomaster3000@gmail.com>
parents:
15195
diff
changeset
|
774 OCTAVE_LOCAL_BUFFER (char, s, elements * (slen+1)); |
4805 | 775 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
776 // create datatype for (null-terminated) string |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
777 // to read into: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
778 hid_t st_id = H5Tcopy (H5T_C_S1); |
17643
d0a197b9962a
Fix loading of string arrays from 3rd party hdf5 files (bug #38789)
Markus Appel <masolomaster3000@gmail.com>
parents:
15195
diff
changeset
|
779 H5Tset_size (st_id, slen+1); |
4805 | 780 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
781 if (H5Dread (data_hid, st_id, H5S_ALL, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
782 H5S_ALL, H5P_DEFAULT, s) < 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
783 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
784 H5Tclose (st_id); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
785 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
786 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
787 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
788 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
789 } |
4805 | 790 |
17643
d0a197b9962a
Fix loading of string arrays from 3rd party hdf5 files (bug #38789)
Markus Appel <masolomaster3000@gmail.com>
parents:
15195
diff
changeset
|
791 charMatrix chm (elements, slen, ' '); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
792 for (hsize_t i = 0; i < elements; ++i) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
793 { |
17643
d0a197b9962a
Fix loading of string arrays from 3rd party hdf5 files (bug #38789)
Markus Appel <masolomaster3000@gmail.com>
parents:
15195
diff
changeset
|
794 chm.insert (s + i*(slen+1), i, 0); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
795 } |
4805 | 796 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
797 matrix = chm; |
4805 | 798 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
799 H5Tclose (st_id); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
800 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
801 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
802 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
803 return true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
804 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
805 } |
4805 | 806 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
807 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
808 H5Tclose (type_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
809 H5Sclose (space_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
810 H5Dclose (data_hid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
811 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
812 } |
4687 | 813 } |
4837 | 814 |
19897
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
815 #else |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
816 gripe_load ("hdf5"); |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
817 #endif |
09ed6f7538dd
avoid needing to include hdf5 in public header files (bug #44370, #43180)
John W. Eaton <jwe@octave.org> and Mike Miller <mtmiller@ieee.org>
parents:
19731
diff
changeset
|
818 |
4837 | 819 return retval; |
4687 | 820 } |