Mercurial > octave
annotate libinterp/corefcn/ls-mat4.cc @ 21724:aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
* configure.ac, Doxyfile.in, make_int.cc, Backend.cc, BaseControl.cc,
ButtonControl.cc, Canvas.cc, CheckBoxControl.cc, Container.cc,
ContextMenu.cc, EditControl.cc, Figure.cc, FigureWindow.cc,
GLCanvas.cc, KeyMap.cc, ListBoxControl.cc, Logger.cc, Menu.cc,
MouseModeActionGroup.cc, Object.cc, ObjectFactory.cc,
ObjectProxy.cc, Panel.cc, PopupMenuControl.cc, PushButtonControl.cc,
PushTool.cc, QtHandlesUtils.cc, RadioButtonControl.cc,
SliderControl.cc, TextControl.cc, TextEdit.cc,
ToggleButtonControl.cc, ToggleTool.cc, ToolBar.cc, __init_qt__.cc,
annotation-dialog.cc, gl-select.cc, module.mk, color-picker.cc,
dialog.cc, documentation-dock-widget.cc, files-dock-widget.cc,
find-files-dialog.cc, find-files-model.cc, history-dock-widget.cc,
liboctgui-build-info.in.cc, file-editor-tab.cc, file-editor.cc,
find-dialog.cc, marker.cc, marker.h, octave-qscintilla.cc,
octave-txt-lexer.cc, main-window.cc, octave-cmd.cc,
octave-dock-widget.cc, octave-gui.cc, octave-interpreter.cc,
octave-qt-link.cc, parser.cc, webinfo.cc, resource-manager.cc,
settings-dialog.cc, shortcut-manager.cc, terminal-dock-widget.cc,
thread-manager.cc, welcome-wizard.cc, workspace-model.cc,
workspace-view.cc, build-env.in.cc, Cell.cc, __contourc__.cc,
__dispatch__.cc, __dsearchn__.cc, __ichol__.cc, __ilu__.cc,
__lin_interpn__.cc, __luinc__.cc, __magick_read__.cc,
__pchip_deriv__.cc, __qp__.cc, balance.cc, besselj.cc, betainc.cc,
bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h,
cdisplay.c, cdisplay.h, cellfun.cc, coct-hdf5-types.c, colloc.cc,
comment-list.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc,
debug.cc, defaults.cc, defaults.in.h, defun.cc, det.cc, dirfns.cc,
display.cc, dlmread.cc, dot.cc, dynamic-ld.cc, eig.cc, ellipj.cc,
error.cc, errwarn.cc, event-queue.cc, fft.cc, fft2.cc, fftn.cc,
file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, gammainc.cc,
gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc,
gl-render.cc, gl2ps-print.cc, graphics.cc, gripes.cc, hash.cc,
help.cc, hess.cc, hex2num.cc, input.cc, inv.cc, jit-ir.cc, jit-ir.h,
jit-typeinfo.cc, jit-typeinfo.h, jit-util.cc, jit-util.h, kron.cc,
load-path.cc, load-save.cc, lookup.cc, ls-ascii-helper.cc,
ls-hdf5.cc, ls-mat-ascii.cc, ls-mat4.cc, ls-mat5.cc,
ls-oct-binary.cc, ls-oct-text.cc, ls-oct-text.h, ls-utils.cc,
lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex.cc, mex.h,
mexproto.h, mgorth.cc, nproc.cc, oct-errno.in.cc, oct-fstrm.cc,
oct-hdf5-types.cc, oct-hdf5-types.h, oct-hist.cc, oct-iostrm.cc,
oct-lvalue.cc, oct-map.cc, oct-prcstrm.cc, oct-procbuf.cc,
oct-stdstrm.h, oct-stream.cc, oct-strstrm.cc, oct-tex-lexer.in.ll,
oct-tex-parser.in.yy, octave-link.cc, ordschur.cc, pager.cc,
pinv.cc, pr-output.cc, procstream.cc, profiler.cc, psi.cc,
pt-jit.cc, pt-jit.h, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc,
regexp.cc, schur.cc, sighandlers.cc, sighandlers.h, siglist.c,
siglist.h, sparse-xdiv.cc, sparse-xpow.cc, sparse.cc, spparms.cc,
sqrtm.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc,
sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, text-renderer.cc,
time.cc, toplev.cc, tril.cc, tsearch.cc, txt-eng.cc, typecast.cc,
urlwrite.cc, utils.cc, variables.cc, xdiv.cc, xgl2ps.c, xnorm.cc,
xpow.cc, zfstream.cc, zfstream.h, __delaunayn__.cc, __eigs__.cc,
__fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc,
__init_gnuplot__.cc, __osmesa_print__.cc, __voronoi__.cc, amd.cc,
audiodevinfo.cc, audioread.cc, ccolamd.cc, chol.cc, colamd.cc,
convhulln.cc, dmperm.cc, fftw.cc, qr.cc, symbfact.cc, symrcm.cc,
liboctinterp-build-info.in.cc, mkbuiltins, mkops, ov-base.cc,
ov-bool-mat.cc, ov-bool-sparse.cc, ov-bool.cc, ov-builtin.cc,
ov-cell.cc, ov-ch-mat.cc, ov-class.cc, ov-classdef.cc, ov-colon.cc,
ov-complex.cc, ov-cs-list.cc, ov-cx-diag.cc, ov-cx-mat.cc,
ov-cx-sparse.cc, ov-dld-fcn.cc, ov-fcn-handle.cc, ov-fcn-inline.cc,
ov-fcn.cc, 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-int16.cc,
ov-int32.cc, ov-int64.cc, ov-int8.cc, ov-java.cc, ov-lazy-idx.cc,
ov-mex-fcn.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-perm.cc,
ov-range.cc, ov-re-diag.cc, ov-re-mat.cc, ov-re-sparse.cc,
ov-scalar.cc, ov-str-mat.cc, ov-struct.cc, ov-typeinfo.cc,
ov-uint16.cc, ov-uint32.cc, ov-uint64.cc, ov-uint8.cc,
ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ovl.cc, octave.cc, octave.h,
op-b-b.cc, op-b-bm.cc, op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc,
op-bm-sbm.cc, op-cdm-cdm.cc, op-cdm-cm.cc, op-cdm-dm.cc,
op-cdm-m.cc, op-cell.cc, op-chm.cc, op-class.cc, op-cm-cdm.cc,
op-cm-cm.cc, op-cm-cs.cc, op-cm-dm.cc, op-cm-m.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc,
op-cs-s.cc, op-cs-scm.cc, op-cs-sm.cc, op-dm-cdm.cc, op-dm-cm.cc,
op-dm-dm.cc, op-dm-m.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-double-conv.cc,
op-fcdm-fcdm.cc, op-fcdm-fcm.cc, op-fcdm-fdm.cc, op-fcdm-fm.cc,
op-fcm-fcdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fdm.cc,
op-fcm-fm.cc, op-fcm-fs.cc, op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc,
op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fcdm.cc, op-fdm-fcm.cc,
op-fdm-fdm.cc, op-fdm-fm.cc, op-float-conv.cc, op-fm-fcdm.cc,
op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fdm.cc, op-fm-fm.cc, op-fm-fs.cc,
op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc,
op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc,
op-int-conv.cc, op-m-cdm.cc, op-m-cm.cc, op-m-cs.cc, op-m-dm.cc,
op-m-m.cc, op-m-s.cc, op-m-scm.cc, op-m-sm.cc, op-pm-pm.cc,
op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc, op-range.cc,
op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc,
op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc,
op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc,
op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc,
op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc,
op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc,
lex.ll, oct-parse.in.yy, pt-arg-list.cc, pt-array-list.cc,
pt-assign.cc, pt-binop.cc, pt-bp.cc, pt-cbinop.cc, pt-cell.cc,
pt-check.cc, pt-classdef.cc, pt-cmd.cc, pt-colon.cc, pt-const.cc,
pt-decl.cc, pt-eval.cc, pt-except.cc, pt-exp.cc, pt-fcn-handle.cc,
pt-funcall.cc, pt-id.cc, pt-idx.cc, pt-jump.cc, pt-loop.cc,
pt-loop.h, pt-mat.cc, pt-misc.cc, pt-pr-code.cc, pt-select.cc,
pt-stmt.cc, pt-unop.cc, pt.cc, token.cc, Array-jit.cc, Array-tc.cc,
version.cc, Array-C.cc, Array-b.cc, Array-ch.cc, Array-d.cc,
Array-f.cc, Array-fC.cc, Array-i.cc, Array-idx-vec.cc, Array-s.cc,
Array-str.cc, Array-util.cc, Array-voidp.cc, Array.cc,
CColVector.cc, CDiagMatrix.cc, CMatrix.cc, CNDArray.cc,
CRowVector.cc, CSparse.cc, MArray-C.cc, MArray-d.cc, MArray-f.cc,
MArray-fC.cc, MArray-i.cc, MArray-s.cc, MSparse-C.cc, MSparse-d.cc,
MatrixType.cc, PermMatrix.cc, Range.cc, Sparse-C.cc, Sparse-b.cc,
Sparse-d.cc, boolMatrix.cc, boolNDArray.cc, boolSparse.cc,
chMatrix.cc, chNDArray.cc, dColVector.cc, dDiagMatrix.cc,
dMatrix.cc, dNDArray.cc, dRowVector.cc, dSparse.cc, dim-vector.cc,
dim-vector.h, fCColVector.cc, fCDiagMatrix.cc, fCMatrix.cc,
fCNDArray.cc, fCRowVector.cc, fColVector.cc, fDiagMatrix.cc,
fMatrix.cc, fNDArray.cc, fRowVector.cc, idx-vector.cc,
int16NDArray.cc, int32NDArray.cc, int64NDArray.cc, int8NDArray.cc,
uint16NDArray.cc, uint32NDArray.cc, uint64NDArray.cc,
uint8NDArray.cc, Faddeeva.cc, blaswrap.c, cquit.c, f77-extern.cc,
f77-fcn.c, f77-fcn.h, lo-error.c, lo-error.h, quit.cc, quit.h,
liboctave-build-info.in.cc, CollocWt.cc, DASPK.cc, DASRT.cc,
DASSL.cc, EIG.cc, LSODE.cc, ODES.cc, Quad.cc, aepbalance.cc,
chol.cc, eigs-base.cc, fEIG.cc, gepbalance.cc, hess.cc,
lo-mappers.cc, lo-specfun.cc, lu.cc, oct-convn.cc, oct-fftw.cc,
oct-norm.cc, oct-rand.cc, oct-spparms.cc, qr.cc, qrp.cc,
randmtzig.cc, randpoisson.cc, schur.cc, sparse-chol.cc,
sparse-dmsolve.cc, sparse-lu.cc, sparse-qr.cc, svd.cc, mk-ops.awk,
mx-defs.h, dir-ops.cc, file-ops.cc, file-stat.cc, lo-sysdep.cc,
mach-info.cc, oct-env.cc, oct-group.cc, oct-passwd.cc,
oct-syscalls.cc, oct-time.cc, oct-uname.cc, pathlen.h, syswait.h,
cmd-edit.cc, cmd-hist.cc, data-conv.cc, f2c-main.c, glob-match.cc,
kpse.cc, lo-array-errwarn.cc, lo-array-gripes.cc, lo-cutils.c,
lo-cutils.h, lo-ieee.cc, lo-ieee.h, lo-regexp.cc, lo-utils.cc,
oct-base64.cc, oct-glob.cc, oct-inttypes.cc, oct-inttypes.h,
oct-locbuf.cc, oct-mutex.cc, oct-rl-edit.c, oct-rl-edit.h,
oct-rl-hist.c, oct-rl-hist.h, oct-shlib.cc, oct-sort.cc,
pathsearch.cc, singleton-cleanup.cc, sparse-sort.cc, sparse-util.cc,
statdefs.h, str-vec.cc, unwind-prot.cc, url-transfer.cc,
acinclude.m4, display-available.c, display-available.h, main-cli.cc,
main-gui.cc, main.in.cc, mkoctfile.in.cc, octave-build-info.in.cc,
octave-config.in.cc, shared-fcns.h:
Use "#if ..." consistently instead of "#ifdef" and "#ifndef".
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 17 May 2016 12:09:30 -0400 |
parents | 40de9f8f23a6 |
children | c4ab2e54f100 |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18691
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
4634 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
4634 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4634 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
24 # include "config.h" |
4634 | 25 #endif |
26 | |
27 #include <cfloat> | |
28 #include <cstring> | |
29 #include <cctype> | |
30 | |
31 #include <fstream> | |
32 #include <iomanip> | |
33 #include <iostream> | |
34 #include <string> | |
4726 | 35 #include <vector> |
4634 | 36 |
37 #include "byte-swap.h" | |
38 #include "data-conv.h" | |
39 #include "file-ops.h" | |
40 #include "glob-match.h" | |
41 #include "lo-mappers.h" | |
42 #include "mach-info.h" | |
43 #include "oct-env.h" | |
44 #include "oct-time.h" | |
45 #include "quit.h" | |
46 #include "str-vec.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7918
diff
changeset
|
47 #include "oct-locbuf.h" |
4634 | 48 |
49 #include "Cell.h" | |
50 #include "defun.h" | |
51 #include "error.h" | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
52 #include "errwarn.h" |
4634 | 53 #include "load-save.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20893
diff
changeset
|
54 #include "ovl.h" |
4634 | 55 #include "oct-map.h" |
56 #include "ov-cell.h" | |
57 #include "pager.h" | |
58 #include "pt-exp.h" | |
59 #include "sysdep.h" | |
60 #include "unwind-prot.h" | |
61 #include "utils.h" | |
62 #include "variables.h" | |
63 #include "version.h" | |
64 #include "dMatrix.h" | |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
65 #include "dSparse.h" |
4634 | 66 |
67 #include "ls-mat4.h" | |
68 | |
69 // Read LEN elements of data from IS in the format specified by | |
70 // PRECISION, placing the result in DATA. If SWAP is TRUE, swap | |
71 // the bytes of each element before copying to DATA. FLT_FMT | |
72 // specifies the format of the data if we are reading floating point | |
73 // numbers. | |
74 | |
75 static void | |
76 read_mat_binary_data (std::istream& is, double *data, int precision, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
77 int len, bool swap, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
78 oct_mach_info::float_format flt_fmt) |
4634 | 79 { |
80 switch (precision) | |
81 { | |
82 case 0: | |
83 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); | |
84 break; | |
85 | |
86 case 1: | |
87 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); | |
88 break; | |
89 | |
90 case 2: | |
91 read_doubles (is, data, LS_INT, len, swap, flt_fmt); | |
92 break; | |
93 | |
94 case 3: | |
95 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); | |
96 break; | |
97 | |
98 case 4: | |
99 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); | |
100 break; | |
101 | |
102 case 5: | |
103 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); | |
104 break; | |
105 | |
106 default: | |
107 break; | |
108 } | |
109 } | |
110 | |
111 int | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
112 read_mat_file_header (std::istream& is, bool& swap, int32_t& mopt, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
113 int32_t& nr, int32_t& nc, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
114 int32_t& imag, int32_t& len, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
115 int quiet) |
4634 | 116 { |
117 swap = false; | |
118 | |
119 // We expect to fail here, at the beginning of a record, so not | |
120 // being able to read another mopt value should not result in an | |
121 // error. | |
122 | |
5760 | 123 is.read (reinterpret_cast<char *> (&mopt), 4); |
4634 | 124 if (! is) |
125 return 1; | |
126 | |
5760 | 127 if (! is.read (reinterpret_cast<char *> (&nr), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
128 return -1; |
4634 | 129 |
5760 | 130 if (! is.read (reinterpret_cast<char *> (&nc), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
131 return -1; |
4634 | 132 |
5760 | 133 if (! is.read (reinterpret_cast<char *> (&imag), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
134 return -1; |
4634 | 135 |
5760 | 136 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
137 return -1; |
4634 | 138 |
139 // If mopt is nonzero and the byte order is swapped, mopt will be | |
140 // bigger than we expect, so we swap bytes. | |
141 // | |
142 // If mopt is zero, it means the file was written on a little endian | |
143 // machine, and we only need to swap if we are running on a big endian | |
144 // machine. | |
145 // | |
146 // Gag me. | |
147 | |
148 if (oct_mach_info::words_big_endian () && mopt == 0) | |
149 swap = true; | |
150 | |
151 // mopt is signed, therefore byte swap may result in negative value. | |
152 | |
153 if (mopt > 9999 || mopt < 0) | |
154 swap = true; | |
155 | |
156 if (swap) | |
157 { | |
4944 | 158 swap_bytes<4> (&mopt); |
159 swap_bytes<4> (&nr); | |
160 swap_bytes<4> (&nc); | |
161 swap_bytes<4> (&imag); | |
162 swap_bytes<4> (&len); | |
4634 | 163 } |
164 | |
165 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) | |
166 { | |
167 if (! quiet) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
168 error ("load: can't read binary file"); |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
169 |
4634 | 170 return -1; |
171 } | |
172 | |
173 return 0; | |
174 } | |
175 | |
176 // We don't just use a cast here, because we need to be able to detect | |
177 // possible errors. | |
178 | |
179 oct_mach_info::float_format | |
180 mopt_digit_to_float_format (int mach) | |
181 { | |
182 oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; | |
183 | |
184 switch (mach) | |
185 { | |
186 case 0: | |
187 flt_fmt = oct_mach_info::flt_fmt_ieee_little_endian; | |
188 break; | |
189 | |
190 case 1: | |
191 flt_fmt = oct_mach_info::flt_fmt_ieee_big_endian; | |
192 break; | |
193 | |
194 case 2: | |
195 case 3: | |
196 case 4: | |
197 default: | |
198 flt_fmt = oct_mach_info::flt_fmt_unknown; | |
199 break; | |
200 } | |
201 | |
202 return flt_fmt; | |
203 } | |
204 | |
205 int | |
206 float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) | |
207 { | |
208 int retval = -1; | |
209 | |
210 switch (flt_fmt) | |
211 { | |
212 case oct_mach_info::flt_fmt_ieee_little_endian: | |
213 retval = 0; | |
214 break; | |
215 | |
216 case oct_mach_info::flt_fmt_ieee_big_endian: | |
217 retval = 1; | |
218 break; | |
219 | |
220 default: | |
221 break; | |
222 } | |
223 | |
224 return retval; | |
225 } | |
226 | |
227 // Extract one value (scalar, matrix, string, etc.) from stream IS and | |
228 // place it in TC, returning the name of the variable. | |
229 // | |
230 // The data is expected to be in Matlab version 4 .mat format, though | |
231 // not all the features of that format are supported. | |
232 // | |
233 // FILENAME is used for error messages. | |
234 // | |
235 // This format provides no way to tag the data as global. | |
236 | |
237 std::string | |
238 read_mat_binary_data (std::istream& is, const std::string& filename, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
239 octave_value& tc) |
4634 | 240 { |
241 std::string retval; | |
242 | |
243 bool swap = false; | |
5828 | 244 int32_t mopt, nr, nc, imag, len; |
4634 | 245 |
246 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); | |
247 if (err) | |
248 { | |
249 if (err < 0) | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
250 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
251 |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
252 return retval; |
4634 | 253 } |
254 | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
255 int type = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
256 int prec = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
257 int order = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
258 int mach = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
259 |
4634 | 260 type = mopt % 10; // Full, sparse, etc. |
261 mopt /= 10; // Eliminate first digit. | |
262 prec = mopt % 10; // double, float, int, etc. | |
263 mopt /= 10; // Eliminate second digit. | |
264 order = mopt % 10; // Row or column major ordering. | |
265 mopt /= 10; // Eliminate third digit. | |
266 mach = mopt % 10; // IEEE, VAX, etc. | |
267 | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
268 oct_mach_info::float_format flt_fmt; |
4634 | 269 flt_fmt = mopt_digit_to_float_format (mach); |
270 | |
271 if (flt_fmt == oct_mach_info::flt_fmt_unknown) | |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
272 error ("load: unrecognized binary format!"); |
4634 | 273 |
274 if (imag && type == 1) | |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
275 error ("load: encountered complex matrix with string flag set!"); |
4634 | 276 |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
277 int dlen = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
278 |
4634 | 279 // LEN includes the terminating character, and the file is also |
280 // supposed to include it, but apparently not all files do. Either | |
281 // way, I think this should work. | |
282 | |
283 { | |
284 OCTAVE_LOCAL_BUFFER (char, name, len+1); | |
285 name[len] = '\0'; | |
5760 | 286 if (! is.read (name, len)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
287 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 288 retval = name; |
289 | |
290 dlen = nr * nc; | |
291 if (dlen < 0) | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
292 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 293 |
294 if (order) | |
295 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
296 octave_idx_type tmp = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
297 nr = nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
298 nc = tmp; |
4634 | 299 } |
300 | |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
301 if (type == 2) |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
302 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
303 if (nc == 4) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
304 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
305 octave_idx_type nr_new, nc_new; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
306 Array<Complex> data (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
307 Array<octave_idx_type> c (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
308 Array<octave_idx_type> r (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
309 OCTAVE_LOCAL_BUFFER (double, dtmp, nr); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
310 OCTAVE_LOCAL_BUFFER (double, ctmp, nr); |
4634 | 311 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
312 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
313 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
314 r.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
315 nr_new = dtmp[nr - 1]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
316 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
317 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
318 c.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
319 nc_new = dtmp[nr - 1]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
320 read_mat_binary_data (is, dtmp, prec, nr - 1, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
321 read_mat_binary_data (is, ctmp, prec, 1, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
322 read_mat_binary_data (is, ctmp, prec, nr - 1, swap, flt_fmt); |
4634 | 323 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
324 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
325 data.xelem (i) = Complex (dtmp[i], ctmp[i]); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
326 read_mat_binary_data (is, ctmp, prec, 1, swap, flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
327 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
328 SparseComplexMatrix smc = SparseComplexMatrix (data, r, c, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
329 nr_new, nc_new); |
4634 | 330 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
331 tc = order ? smc.transpose () : smc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
332 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
333 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
334 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
335 octave_idx_type nr_new, nc_new; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
336 Array<double> data (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
337 Array<octave_idx_type> c (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
338 Array<octave_idx_type> r (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
339 OCTAVE_LOCAL_BUFFER (double, dtmp, nr); |
4634 | 340 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
341 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
342 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
343 r.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
344 nr_new = dtmp[nr - 1]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
345 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
346 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
347 c.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
348 nc_new = dtmp[nr - 1]; |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
349 read_mat_binary_data (is, data.fortran_vec (), prec, nr - 1, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
350 swap, flt_fmt); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
351 read_mat_binary_data (is, dtmp, prec, 1, swap, flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
352 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
353 SparseMatrix sm = SparseMatrix (data, r, c, nr_new, nc_new); |
4634 | 354 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
355 tc = order ? sm.transpose () : sm; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
356 } |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
357 } |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
358 else |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
359 { |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
360 Matrix re (nr, nc); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
361 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
362 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
363 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20228
diff
changeset
|
364 if (! is) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
365 error ("load: reading matrix data for '%s'", name); |
4634 | 366 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
367 if (imag) |
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 Matrix im (nr, nc); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
370 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
371 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
372 flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
373 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20228
diff
changeset
|
374 if (! is) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
375 error ("load: reading imaginary matrix data for '%s'", name); |
4634 | 376 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
377 ComplexMatrix ctmp (nr, nc); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
378 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
379 for (octave_idx_type j = 0; j < nc; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
380 for (octave_idx_type i = 0; i < nr; i++) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
381 ctmp (i,j) = Complex (re(i,j), im(i,j)); |
4634 | 382 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
383 tc = order ? ctmp.transpose () : ctmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
384 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
385 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
386 tc = order ? re.transpose () : re; |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
387 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
388 if (type == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
389 tc = tc.convert_to_str (false, true, '\''); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
390 } |
4634 | 391 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
392 return retval; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
393 } |
4634 | 394 } |
395 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
396 // Save the data from TC along with the corresponding NAME on stream OS |
4634 | 397 // in the MatLab version 4 binary format. |
398 | |
399 bool | |
400 save_mat_binary_data (std::ostream& os, const octave_value& tc, | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
401 const std::string& name) |
4634 | 402 { |
5828 | 403 int32_t mopt = 0; |
4634 | 404 |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
405 mopt += tc.is_sparse_type () ? 2 : tc.is_string () ? 1 : 0; |
4634 | 406 |
407 oct_mach_info::float_format flt_fmt = | |
408 oct_mach_info::native_float_format ();; | |
409 | |
410 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); | |
411 | |
5760 | 412 os.write (reinterpret_cast<char *> (&mopt), 4); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
413 |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
414 octave_idx_type len; |
5828 | 415 int32_t nr = tc.rows (); |
4634 | 416 |
5828 | 417 int32_t nc = tc.columns (); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
418 |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
419 if (tc.is_sparse_type ()) |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
420 { |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
421 len = tc.nnz (); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
422 uint32_t nnz = len + 1; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
423 os.write (reinterpret_cast<char *> (&nnz), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
424 |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
425 uint32_t iscmplx = tc.is_complex_type () ? 4 : 3; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
426 os.write (reinterpret_cast<char *> (&iscmplx), 4); |
4634 | 427 |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
428 uint32_t tmp = 0; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
429 os.write (reinterpret_cast<char *> (&tmp), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
430 } |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
431 else |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
432 { |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
433 os.write (reinterpret_cast<char *> (&nr), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
434 os.write (reinterpret_cast<char *> (&nc), 4); |
4634 | 435 |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
436 int32_t imag = tc.is_complex_type () ? 1 : 0; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
437 os.write (reinterpret_cast<char *> (&imag), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
438 |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
439 len = nr * nc; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
440 } |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
441 |
4634 | 442 // LEN includes the terminating character, and the file is also |
443 // supposed to include it. | |
444 | |
5828 | 445 int32_t name_len = name.length () + 1; |
4634 | 446 |
5760 | 447 os.write (reinterpret_cast<char *> (&name_len), 4); |
4634 | 448 os << name << '\0'; |
449 | |
450 if (tc.is_string ()) | |
451 { | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
452 unwind_protect frame; |
4634 | 453 |
454 charMatrix chm = tc.char_matrix_value (); | |
455 | |
5275 | 456 octave_idx_type nrow = chm.rows (); |
457 octave_idx_type ncol = chm.cols (); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
458 |
4634 | 459 OCTAVE_LOCAL_BUFFER (double, buf, ncol*nrow); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
460 |
5275 | 461 for (octave_idx_type i = 0; i < nrow; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
462 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
463 std::string tstr = chm.row_as_string (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
464 const char *s = tstr.data (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
465 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
466 for (octave_idx_type j = 0; j < ncol; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
467 buf[j*nrow+i] = static_cast<double> (*s++ & 0x00FF); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
468 } |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
469 std::streamsize n_bytes = static_cast<std::streamsize> (nrow) * |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
470 static_cast<std::streamsize> (ncol) * |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
471 sizeof (double); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
472 os.write (reinterpret_cast<char *> (buf), n_bytes); |
4634 | 473 } |
474 else if (tc.is_range ()) | |
475 { | |
476 Range r = tc.range_value (); | |
477 double base = r.base (); | |
478 double inc = r.inc (); | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
479 octave_idx_type nel = r.numel (); |
5275 | 480 for (octave_idx_type i = 0; i < nel; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
481 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
482 double x = base + i * inc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
483 os.write (reinterpret_cast<char *> (&x), 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
484 } |
4634 | 485 } |
486 else if (tc.is_real_scalar ()) | |
487 { | |
488 double tmp = tc.double_value (); | |
5760 | 489 os.write (reinterpret_cast<char *> (&tmp), 8); |
4634 | 490 } |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
491 else if (tc.is_sparse_type ()) |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
492 { |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
493 double ds; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
494 OCTAVE_LOCAL_BUFFER (double, dtmp, len); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
495 if (tc.is_complex_matrix ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
496 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
497 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
498 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
499 for (octave_idx_type i = 0; i < len; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
500 dtmp[i] = m.ridx (i) + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
501 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
502 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
503 ds = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
504 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
505 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
506 octave_idx_type ii = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
507 for (octave_idx_type j = 0; j < nc; j++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
508 for (octave_idx_type i = m.cidx (j); i < m.cidx (j+1); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
509 dtmp[ii++] = j + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
510 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
511 ds = nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
512 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
513 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
514 for (octave_idx_type i = 0; i < len; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
515 dtmp[i] = std::real (m.data (i)); |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
516 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
517 ds = 0.; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
518 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
519 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
520 for (octave_idx_type i = 0; i < len; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
521 dtmp[i] = std::imag (m.data (i)); |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
522 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
523 os.write (reinterpret_cast<const char *> (&ds), 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
524 } |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
525 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
526 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
527 SparseMatrix m = tc.sparse_matrix_value (); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
528 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
529 for (octave_idx_type i = 0; i < len; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
530 dtmp[i] = m.ridx (i) + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
531 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
532 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
533 ds = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
534 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
535 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
536 octave_idx_type ii = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
537 for (octave_idx_type j = 0; j < nc; j++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
538 for (octave_idx_type i = m.cidx (j); i < m.cidx (j+1); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
539 dtmp[ii++] = j + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
540 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
541 ds = nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
542 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
543 |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
544 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
545 ds = 0.; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
546 os.write (reinterpret_cast<const char *> (&ds), 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
547 } |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
548 } |
4634 | 549 else if (tc.is_real_matrix ()) |
550 { | |
551 Matrix m = tc.matrix_value (); | |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
552 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
553 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
4634 | 554 } |
555 else if (tc.is_complex_scalar ()) | |
556 { | |
557 Complex tmp = tc.complex_value (); | |
5760 | 558 os.write (reinterpret_cast<char *> (&tmp), 16); |
4634 | 559 } |
560 else if (tc.is_complex_matrix ()) | |
561 { | |
562 ComplexMatrix m_cmplx = tc.complex_matrix_value (); | |
563 Matrix m = ::real (m_cmplx); | |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
564 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
565 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
4634 | 566 m = ::imag (m_cmplx); |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
567 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
4634 | 568 } |
569 else | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
570 // FIXME: Should this just error out rather than warn? |
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
571 warn_wrong_type_arg ("save", tc); |
4634 | 572 |
18384
bd9d34f28b0f
Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
573 return ! os.fail (); |
4634 | 574 } |