Mercurial > octave
view libinterp/octave-value/ov-str-mat.cc @ 31607:aac27ad79be6 stable
maint: Re-indent code after switch to using namespace macros.
* build-env.h, build-env.in.cc, Cell.h, __betainc__.cc, __eigs__.cc,
__ftp__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __magick_read__.cc,
__pchip_deriv__.cc, amd.cc, base-text-renderer.cc, base-text-renderer.h,
besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.h, call-stack.cc,
call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, dasrt.cc, data.cc,
debug.cc, defaults.cc, defaults.h, det.cc, display.cc, display.h, dlmread.cc,
dynamic-ld.cc, dynamic-ld.h, ellipj.cc, environment.cc, environment.h,
error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h,
event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc,
file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h,
gcd.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h,
graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, gsvd.cc, gtk-manager.cc,
gtk-manager.h, help.cc, help.h, hook-fcn.cc, hook-fcn.h, input.cc, input.h,
interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h,
inv.cc, jsondecode.cc, jsonencode.cc, latex-text-renderer.cc,
latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h,
lookup.cc, ls-hdf5.cc, ls-mat4.cc, ls-mat5.cc, lsode.cc, lu.cc, mappers.cc,
matrix_type.cc, max.cc, mex.cc, mexproto.h, mxarray.h, mxtypes.in.h,
oct-errno.in.cc, oct-hdf5-types.cc, oct-hist.cc, oct-hist.h, oct-map.cc,
oct-map.h, oct-opengl.h, oct-prcstrm.h, oct-process.cc, oct-process.h,
oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.h,
octave-default-image.h, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc,
pow2.cc, pr-output.cc, psi.cc, qr.cc, quadcc.cc, rand.cc, regexp.cc,
settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xpow.cc,
sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfns.cc, svd.cc,
syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h,
symtab.cc, symtab.h, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h,
text-renderer.cc, text-renderer.h, time.cc, toplev.cc, typecast.cc,
url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h,
variables.cc, variables.h, xdiv.cc, __delaunayn__.cc, __init_fltk__.cc,
__init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audioread.cc, convhulln.cc,
gzip.cc, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc,
cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h,
cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h,
cdef-utils.cc, cdef-utils.h, ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc,
ov-base-mat.h, ov-base-scalar.cc, ov-base.cc, ov-base.h, ov-bool-mat.cc,
ov-bool-mat.h, ov-bool-sparse.cc, ov-bool.cc, ov-builtin.h, ov-cell.cc,
ov-ch-mat.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h,
ov-complex.cc, ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-dld-fcn.cc,
ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-float.cc,
ov-flt-complex.cc, ov-flt-cx-diag.cc, ov-flt-cx-mat.cc, ov-flt-re-diag.cc,
ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-intx.h, ov-java.cc, ov-lazy-idx.cc,
ov-legacy-range.cc, ov-magic-int.cc, ov-mex-fcn.cc, ov-mex-fcn.h,
ov-null-mat.cc, ov-perm.cc, ov-range.cc, ov-re-diag.cc, ov-re-mat.cc,
ov-re-mat.h, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc, ov-struct.cc,
ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.h,
octave.cc, octave.h, op-b-sbm.cc, op-bm-sbm.cc, op-cs-scm.cc, op-fm-fcm.cc,
op-fs-fcm.cc, op-s-scm.cc, op-scm-cs.cc, op-scm-s.cc, op-sm-cs.cc, ops.h,
anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h,
comment-list.cc, comment-list.h, filepos.h, lex.h, oct-lvalue.cc, oct-lvalue.h,
parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h,
pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h,
pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc,
pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc,
pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h,
pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h,
pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h,
pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h,
pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h,
pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h,
pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h,
pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h,
idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h,
aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h,
gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc,
lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h,
oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc,
oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h,
randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h,
sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc,
sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h,
file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h,
lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h,
oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc,
oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h,
action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h,
cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h,
lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h,
lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc,
oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc,
oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc,
oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc,
pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc,
url-transfer.h:
Re-indent code after switch to using namespace macros.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 01 Dec 2022 18:02:15 -0800 |
parents | b3ca7f891750 |
children | 597f3ee61a48 |
line wrap: on
line source
//////////////////////////////////////////////////////////////////////// // // Copyright (C) 1996-2022 The Octave Project Developers // // See the file COPYRIGHT.md in the top-level directory of this // distribution or <https://octave.org/copyright/>. // // This file is part of Octave. // // Octave is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Octave is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Octave; see the file COPYING. If not, see // <https://www.gnu.org/licenses/>. // //////////////////////////////////////////////////////////////////////// #if defined (HAVE_CONFIG_H) # include "config.h" #endif #include <cctype> #include <istream> #include <ostream> #include <vector> #include "data-conv.h" #include "lo-ieee.h" #include "mach-info.h" #include "mx-base.h" #include "oct-locbuf.h" #include "byte-swap.h" #include "defun.h" #include "errwarn.h" #include "ls-ascii-helper.h" #include "ls-hdf5.h" #include "ls-oct-text.h" #include "ls-utils.h" #include "ovl.h" #include "oct-hdf5.h" #include "oct-stream.h" #include "ops.h" #include "ov-scalar.h" #include "ov-re-mat.h" #include "ov-str-mat.h" #include "pr-output.h" #include "utils.h" DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_str, "string", "char"); DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_char_matrix_sq_str, "sq_string", "char"); static octave_base_value * default_numeric_conversion_function (const octave_base_value& a) { octave_base_value *retval = nullptr; const octave_char_matrix_str& v = dynamic_cast<const octave_char_matrix_str&> (a); NDArray nda = v.array_value (true); if (nda.numel () == 1) retval = new octave_scalar (nda(0)); else retval = new octave_matrix (nda); return retval; } octave_base_value::type_conv_info octave_char_matrix_str::numeric_conversion_function (void) const { return octave_base_value::type_conv_info (default_numeric_conversion_function, octave_matrix::static_type_id ()); } octave_value octave_char_matrix_str::do_index_op_internal (const octave_value_list& idx, bool resize_ok, char type) { octave_value retval; octave_idx_type len = idx.length (); // If we catch an indexing error in index_vector, we flag an error in // index k. Ensure it is the right value before each idx_vector call. // Same variable as used in the for loop in the default case. octave_idx_type k = 0; try { switch (len) { case 0: retval = octave_value (m_matrix, type); break; case 1: { octave::idx_vector i = idx (0).index_vector (); retval = octave_value (charNDArray (m_matrix.index (i, resize_ok)), type); } break; case 2: { octave::idx_vector i = idx (0).index_vector (); k = 1; octave::idx_vector j = idx (1).index_vector (); retval = octave_value (charNDArray (m_matrix.index (i, j, resize_ok)), type); } break; default: { Array<octave::idx_vector> idx_vec (dim_vector (len, 1)); for (k = 0; k < len; k++) idx_vec(k) = idx(k).index_vector (); retval = octave_value (charNDArray (m_matrix.index (idx_vec, resize_ok)), type); } break; } } catch (octave::index_exception& ie) { // Rethrow to allow more info to be reported later. ie.set_pos_if_unset (len, k+1); throw; } return retval; } octave_value octave_char_matrix_str::resize (const dim_vector& dv, bool fill) const { charNDArray retval (m_matrix); if (fill) retval.resize (dv, 0); else retval.resize (dv); return octave_value (retval, is_sq_string () ? '\'' : '"'); } #define CHAR_MATRIX_CONV(TNAME, FCN) \ \ if (! force_string_conv) \ err_invalid_conversion ("string", TNAME); \ \ warn_implicit_conversion ("Octave:str-to-num", "string", TNAME); \ \ return octave_char_matrix::FCN () double octave_char_matrix_str::double_value (bool force_string_conv) const { CHAR_MATRIX_CONV ("real scalar", double_value); } Complex octave_char_matrix_str::complex_value (bool force_string_conv) const { CHAR_MATRIX_CONV ("complex scalar", complex_value); } Matrix octave_char_matrix_str::matrix_value (bool force_string_conv) const { CHAR_MATRIX_CONV ("real matrix", matrix_value); } ComplexMatrix octave_char_matrix_str::complex_matrix_value (bool force_string_conv) const { CHAR_MATRIX_CONV ("complex matrix", complex_matrix_value); } NDArray octave_char_matrix_str::array_value (bool force_string_conv) const { CHAR_MATRIX_CONV ("real N-D array", array_value); } ComplexNDArray octave_char_matrix_str::complex_array_value (bool force_string_conv) const { CHAR_MATRIX_CONV ("complex N-D array", complex_array_value); } string_vector octave_char_matrix_str::string_vector_value (bool) const { string_vector retval; if (m_matrix.ndims () != 2) error ("invalid conversion of charNDArray to string_vector"); charMatrix chm (m_matrix); octave_idx_type n = chm.rows (); retval.resize (n); for (octave_idx_type i = 0; i < n; i++) retval[i] = chm.row_as_string (i); return retval; } std::string octave_char_matrix_str::string_value (bool) const { if (m_matrix.ndims () != 2) error ("invalid conversion of charNDArray to string"); charMatrix chm (m_matrix); if (chm.rows () > 1) warning_with_id ("Octave:charmat-truncated", "multi-row character matrix converted to a string, only the first row is used"); // FIXME: Is this correct? return chm.row_as_string (0); } /* %!test <*49536> %! warning ("on", "Octave:charmat-truncated", "local"); %! s = char ("this", "is", "a", "char", "matrix"); %! fail ("sprintf (s)", ... %! "warning", ... %! "multi-row character matrix converted to a string"); */ Array<std::string> octave_char_matrix_str::cellstr_value (void) const { Array<std::string> retval; if (m_matrix.ndims () != 2) error ("cellstr: cannot convert multidimensional arrays"); const charMatrix chm (m_matrix); octave_idx_type nr = chm.rows (); retval.clear (nr, 1); for (octave_idx_type i = 0; i < nr; i++) retval.xelem (i) = chm.row_as_string (i); return retval; } void octave_char_matrix_str::print_raw (std::ostream& os, bool pr_as_read_syntax) const { octave_print_internal (os, m_matrix, pr_as_read_syntax, current_print_indent_level (), true); } void octave_char_matrix_str::short_disp (std::ostream& os) const { if (m_matrix.ndims () == 2 && numel () > 0) { charMatrix chm (m_matrix); std::string tmp = chm.row_as_string (0); // FIXME: should this be configurable? std::size_t max_len = 100; os << (tmp.length () > max_len ? tmp.substr (0, 100) : tmp); } } std::string octave_char_matrix_str::edit_display (const float_display_format&, octave_idx_type i, octave_idx_type) const { if (i == 0) { if (rows () == 1) { std::string retval = string_value (); if (! is_sq_string ()) retval = octave::undo_string_escapes (retval); return retval; } else if (is_zero_by_zero ()) return ""; } std::string tname = type_name (); dim_vector dv = m_matrix.dims (); std::string dimstr = dv.str (); return "[" + dimstr + " " + tname + "]"; } bool octave_char_matrix_str::save_ascii (std::ostream& os) { dim_vector dv = dims (); if (dv.ndims () > 2) { charNDArray tmp = char_array_value (); os << "# ndims: " << dv.ndims () << "\n"; for (int i=0; i < dv.ndims (); i++) os << ' ' << dv(i); os << "\n"; os.write (tmp.fortran_vec (), dv.numel ()); os << "\n"; } else { // Keep this case, rather than use generic code above for // backward compatibility. Makes load_ascii much more complex!! charMatrix chm = char_matrix_value (); octave_idx_type elements = chm.rows (); os << "# elements: " << elements << "\n"; for (octave_idx_type i = 0; i < elements; i++) { unsigned len = chm.cols (); os << "# length: " << len << "\n"; std::string tstr = chm.row_as_string (i); const char *tmp = tstr.data (); if (tstr.length () > len) panic_impossible (); os.write (tmp, len); os << "\n"; } } return true; } bool octave_char_matrix_str::load_ascii (std::istream& is) { string_vector keywords(3); keywords[0] = "ndims"; keywords[1] = "elements"; keywords[2] = "length"; std::string kw; int val = 0; if (! extract_keyword (is, keywords, kw, val, true)) error ("load: failed to extract number of rows and columns"); if (kw == "ndims") { int mdims = val; if (mdims < 0) error ("load: failed to extract matrix size"); dim_vector dv; dv.resize (mdims); for (int i = 0; i < mdims; i++) is >> dv(i); if (! is) error ("load: failed to read dimensions"); charNDArray tmp(dv); if (tmp.isempty ()) m_matrix = tmp; else { char *ftmp = tmp.fortran_vec (); octave::skip_preceeding_newline (is); if (! is.read (ftmp, dv.numel ()) || ! is) error ("load: failed to load string constant"); m_matrix = tmp; } } else if (kw == "elements") { int elements = val; if (elements < 0) error ("load: failed to extract number of string elements"); // FIXME: need to be able to get max length before doing anything. charMatrix chm (elements, 0); int max_len = 0; for (int i = 0; i < elements; i++) { int len; if (! extract_keyword (is, "length", len) || len < 0) error ("load: failed to extract string length for element %d", i+1); // Use this instead of a C-style character // buffer so that we can properly handle // embedded NUL characters. charMatrix tmp (1, len); char *ptmp = tmp.fortran_vec (); if (len > 0 && ! is.read (ptmp, len)) error ("load: failed to load string constant"); if (len > max_len) { max_len = len; chm.resize (elements, max_len, 0); } chm.insert (tmp, i, 0); } m_matrix = chm; } else if (kw == "length") { int len = val; if (len >= 0) { // This is cruft for backward compatibility, // but relatively harmless. // Use this instead of a C-style character buffer so // that we can properly handle embedded NUL characters. charMatrix tmp (1, len); char *ptmp = tmp.fortran_vec (); if (len > 0 && ! is.read (ptmp, len)) error ("load: failed to load string constant"); if (! is) error ("load: failed to load string constant"); m_matrix = tmp; } } else panic_impossible (); return true; } bool octave_char_matrix_str::save_binary (std::ostream& os, bool /* save_as_floats */) { dim_vector dv = dims (); if (dv.ndims () < 1) return false; // Use negative value for ndims to differentiate with old format!! int32_t tmp = - dv.ndims (); os.write (reinterpret_cast<char *> (&tmp), 4); for (int i=0; i < dv.ndims (); i++) { tmp = dv(i); os.write (reinterpret_cast<char *> (&tmp), 4); } charNDArray m = char_array_value (); os.write (m.fortran_vec (), dv.numel ()); return true; } bool octave_char_matrix_str::load_binary (std::istream& is, bool swap, octave::mach_info::float_format /* fmt */) { int32_t elements; if (! is.read (reinterpret_cast<char *> (&elements), 4)) return false; if (swap) swap_bytes<4> (&elements); if (elements < 0) { int32_t mdims = - elements; int32_t di; dim_vector dv; dv.resize (mdims); for (int i = 0; i < mdims; i++) { if (! is.read (reinterpret_cast<char *> (&di), 4)) return false; if (swap) swap_bytes<4> (&di); dv(i) = di; } // Convert an array with a single dimension to be a row vector. // Octave should never write files like this, other software // might. if (mdims == 1) { mdims = 2; dv.resize (mdims); dv(1) = dv(0); dv(0) = 1; } charNDArray m(dv); char *tmp = m.fortran_vec (); is.read (tmp, dv.numel ()); if (! is) return false; m_matrix = m; } else { charMatrix chm (elements, 0); int max_len = 0; for (int i = 0; i < elements; i++) { int32_t len; if (! is.read (reinterpret_cast<char *> (&len), 4)) return false; if (swap) swap_bytes<4> (&len); charMatrix btmp (1, len); char *pbtmp = btmp.fortran_vec (); if (! is.read (pbtmp, len)) return false; if (len > max_len) { max_len = len; chm.resize (elements, max_len, 0); } chm.insert (btmp, i, 0); } m_matrix = chm; } return true; } bool octave_char_matrix_str::save_hdf5 (octave_hdf5_id loc_id, const char *name, bool /* save_as_floats */) { bool retval = false; #if defined (HAVE_HDF5) dim_vector dv = dims (); int empty = save_hdf5_empty (loc_id, name, dv); if (empty) return (empty > 0); int rank = dv.ndims (); hid_t space_hid, data_hid; space_hid = data_hid = -1; charNDArray m = char_array_value (); OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); // Octave uses column-major, while HDF5 uses row-major ordering for (int i = 0; i < rank; i++) hdims[i] = dv(rank-i-1); space_hid = H5Screate_simple (rank, hdims, nullptr); if (space_hid < 0) return false; #if defined (HAVE_HDF5_18) data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, octave_H5P_DEFAULT, octave_H5P_DEFAULT, octave_H5P_DEFAULT); #else data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_CHAR, space_hid, octave_H5P_DEFAULT); #endif if (data_hid < 0) { H5Sclose (space_hid); return false; } OCTAVE_LOCAL_BUFFER (char, s, dv.numel ()); for (int i = 0; i < dv.numel (); ++i) s[i] = m(i); retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, octave_H5S_ALL, octave_H5S_ALL, octave_H5P_DEFAULT, s) >= 0; H5Dclose (data_hid); H5Sclose (space_hid); #else octave_unused_parameter (loc_id); octave_unused_parameter (name); warn_save ("hdf5"); #endif return retval; } bool octave_char_matrix_str::load_hdf5 (octave_hdf5_id loc_id, const char *name) { bool retval = false; #if defined (HAVE_HDF5) dim_vector dv; int empty = load_hdf5_empty (loc_id, name, dv); if (empty > 0) m_matrix.resize (dv); if (empty) return (empty > 0); #if defined (HAVE_HDF5_18) hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT); #else hid_t data_hid = H5Dopen (loc_id, name); #endif hid_t space_hid = H5Dget_space (data_hid); hsize_t rank = H5Sget_simple_extent_ndims (space_hid); hid_t type_hid = H5Dget_type (data_hid); hid_t type_class_hid = H5Tget_class (type_hid); if (type_class_hid == H5T_INTEGER) { if (rank < 1) { H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return false; } OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); H5Sget_simple_extent_dims (space_hid, hdims, maxdims); // Octave uses column-major, while HDF5 uses row-major ordering if (rank == 1) { dv.resize (2); dv(0) = 1; dv(1) = hdims[0]; } else { dv.resize (rank); for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) dv(j) = hdims[i]; } charNDArray m (dv); char *str = m.fortran_vec (); if (H5Dread (data_hid, H5T_NATIVE_CHAR, octave_H5S_ALL, octave_H5S_ALL, octave_H5P_DEFAULT, str) >= 0) { retval = true; m_matrix = m; } H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return true; } else { // This is cruft for backward compatibility and easy data importation if (rank == 0) { // a single string: int slen = H5Tget_size (type_hid); if (slen < 0) { H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return false; } else { OCTAVE_LOCAL_BUFFER (char, s, slen+1); // create datatype for (null-terminated) string to read into: hid_t st_id = H5Tcopy (H5T_C_S1); H5Tset_size (st_id, slen+1); if (H5Dread (data_hid, st_id, octave_H5S_ALL, octave_H5S_ALL, octave_H5P_DEFAULT, s) < 0) { H5Tclose (st_id); H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return false; } m_matrix = charMatrix (s); H5Tclose (st_id); H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return true; } } else if (rank == 1) { // string vector hsize_t elements, maxdim; H5Sget_simple_extent_dims (space_hid, &elements, &maxdim); int slen = H5Tget_size (type_hid); if (slen < 0) { H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return false; } else { // hdf5 string arrays store strings of all the // same physical length (I think), which is // slightly wasteful, but oh well. OCTAVE_LOCAL_BUFFER (char, s, elements * (slen+1)); // create datatype for (null-terminated) string // to read into: hid_t st_id = H5Tcopy (H5T_C_S1); H5Tset_size (st_id, slen+1); if (H5Dread (data_hid, st_id, octave_H5S_ALL, octave_H5S_ALL, octave_H5P_DEFAULT, s) < 0) { H5Tclose (st_id); H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return false; } charMatrix chm (elements, slen, ' '); for (hsize_t i = 0; i < elements; ++i) { chm.insert (s + i*(slen+1), i, 0); } m_matrix = chm; H5Tclose (st_id); H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return true; } } else { H5Tclose (type_hid); H5Sclose (space_hid); H5Dclose (data_hid); return false; } } #else octave_unused_parameter (loc_id); octave_unused_parameter (name); warn_load ("hdf5"); #endif return retval; }