annotate libinterp/corefcn/dlmread.cc @ 22666:68d5c4759783

dlmread: Return correct number of columns when line ends with separator (bug #42025). * dlmread.cc (Fdlmread): Initialize rmax to 32 so that output matrix is not constantly resized for small datasets. Detect separator followed by EOL and break out of while loop over columns. When re-sizing matrix, maintain rmax as a power of 2 which may be more efficient for underlying heap allocation routines. Use std::max to maintain cmax at the largest value seen. Adjust BIST tests to pass for new behavior.
author Rik <rik@octave.org>
date Tue, 25 Oct 2016 10:20:13 -0700
parents a93887d7f0da
children d318bc8e83ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
1 /*
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
2
22323
bac0d6f07a3e maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents: 21966
diff changeset
3 Copyright (C) 2008-2016 Jonathan Stickel
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
4 Copyright (C) 2010 Jaroslav Hajek
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
5
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 This file is part of Octave.
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
7
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 option) any later version.
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
12
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 for more details.
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
17
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 along with Octave; see the file COPYING. If not, see
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>.
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
21
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
22 */
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
23
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
24 // Adapted from previous version of dlmread.occ as authored by Kai
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
25 // Habel, but core code has been completely re-written.
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
26
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21593
diff changeset
27 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
28 # include "config.h"
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
29 #endif
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
30
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
31 #include <cctype>
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
32 #include <fstream>
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
33 #include <limits>
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
34
9512
56e850e3b06f dlmread: perform tilde expansion to filename argument
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
35 #include "file-ops.h"
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 #include "lo-ieee.h"
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
37
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
38 #include "defun.h"
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
39 #include "oct-stream.h"
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 #include "error.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20918
diff changeset
41 #include "ovl.h"
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
42 #include "utils.h"
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
43
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
44 static const octave_idx_type idx_max =
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
45 std::numeric_limits<octave_idx_type>::max ();
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
46
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
47 static bool
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
48 read_cell_spec (std::istream& is, octave_idx_type& row, octave_idx_type& col)
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
49 {
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
50 bool stat = false;
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
51
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
52 if (is.peek () == std::istream::traits_type::eof ())
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
53 stat = true;
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
54 else
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 {
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
56 if (::isalpha (is.peek ()))
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
57 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
58 col = 0;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
59 while (is && ::isalpha (is.peek ()))
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
60 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
61 char ch = is.get ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
62 col *= 26;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
63 if (ch >= 'a')
10718
b8d76f4be94a Fix spreadsheet style range option (bug #30183).
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
64 col += ch - 'a' + 1;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
65 else
10718
b8d76f4be94a Fix spreadsheet style range option (bug #30183).
Rik <octave@nomad.inbox5.com>
parents: 10350
diff changeset
66 col += ch - 'A' + 1;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
67 }
21062
d9c1884d1aaa maint: Eliminate space between variable and decrement '--' operator.
Rik <rik@octave.org>
parents: 20955
diff changeset
68 col--;
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
69
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
70 if (is)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
71 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
72 is >> row;
21062
d9c1884d1aaa maint: Eliminate space between variable and decrement '--' operator.
Rik <rik@octave.org>
parents: 20955
diff changeset
73 row--;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
74 if (is)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
75 stat = true;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
76 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
77 }
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
78 }
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
79
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
80 return stat;
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
81 }
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
82
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
83 static bool
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
84 parse_range_spec (const octave_value& range_spec,
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
85 octave_idx_type& rlo, octave_idx_type& clo,
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
86 octave_idx_type& rup, octave_idx_type& cup)
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
87 {
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
88 bool stat = true;
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
89
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
90 if (range_spec.is_string ())
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
91 {
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
92 std::istringstream is (range_spec.string_value ());
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
93 char ch = is.peek ();
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
94
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
95 if (ch == '.' || ch == ':')
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
96 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
97 rlo = 0;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
98 clo = 0;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
99 ch = is.get ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
100 if (ch == '.')
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
101 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
102 ch = is.get ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
103 if (ch != '.')
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
104 stat = false;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
105 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
106 }
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
107 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
108 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
109 stat = read_cell_spec (is, rlo, clo);
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
110
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
111 if (stat)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
112 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
113 ch = is.peek ();
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
114
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
115 if (ch == '.' || ch == ':')
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
116 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
117 ch = is.get ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
118 if (ch == '.')
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
119 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
120 ch = is.get ();
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20940
diff changeset
121 if (! is || ch != '.')
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
122 stat = false;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
123 }
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
124
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
125 rup = idx_max - 1;
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
126 cup = idx_max - 1;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
127 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
128 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
129 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
130 rup = rlo;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
131 cup = clo;
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20940
diff changeset
132 if (! is || ! is.eof ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
133 stat = false;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
134 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
135 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
136 }
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
137
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20940
diff changeset
138 if (stat && is && ! is.eof ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
139 stat = read_cell_spec (is, rup, cup);
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
140
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20940
diff changeset
141 if (! is || ! is.eof ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
142 stat = false;
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
143 }
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
144 else if (range_spec.is_real_matrix () && range_spec.numel () == 4)
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
145 {
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
146 ColumnVector range(range_spec.vector_value ());
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
147 // double --> unsigned int
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
148 rlo = static_cast<octave_idx_type> (range(0));
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
149 clo = static_cast<octave_idx_type> (range(1));
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
150 rup = static_cast<octave_idx_type> (range(2));
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
151 cup = static_cast<octave_idx_type> (range(3));
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
152 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
153 else
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
154 stat = false;
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
155
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
156 return stat;
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
157 }
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
158
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
159 DEFUN (dlmread, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
160 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
161 @deftypefn {} {@var{data} =} dlmread (@var{file})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
162 @deftypefnx {} {@var{data} =} dlmread (@var{file}, @var{sep})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
163 @deftypefnx {} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{r0}, @var{c0})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
164 @deftypefnx {} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{range})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
165 @deftypefnx {} {@var{data} =} dlmread (@dots{}, "emptyvalue", @var{EMPTYVAL})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
166 Read numeric data from the text file @var{file} which uses the delimiter
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
167 @var{sep} between data values.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
168
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
169 If @var{sep} is not defined the separator between fields is determined from
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
170 the file itself.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
171
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
172 The optional scalar arguments @var{r0} and @var{c0} define the starting row
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
173 and column of the data to be read. These values are indexed from zero,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
174 i.e., the first data row corresponds to an index of zero.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
175
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
176 The @var{range} parameter specifies exactly which data elements are read.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
177 The first form of the parameter is a 4-element vector containing the upper
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
178 left and lower right corners @code{[@var{R0},@var{C0},@var{R1},@var{C1}]}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
179 where the indices are zero-based. Alternatively, a spreadsheet style
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
180 form such as @qcode{"A2..Q15"} or @qcode{"T1:AA5"} can be used. The
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
181 lowest alphabetical index @qcode{'A'} refers to the first column. The
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
182 lowest row index is 1.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
183
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
184 @var{file} should be a filename or a file id given by @code{fopen}. In the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
185 latter case, the file is read until end of file is reached.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
186
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
187 The @qcode{"emptyvalue"} option may be used to specify the value used to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
188 fill empty fields. The default is zero. Note that any non-numeric values,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
189 such as text, are also replaced by the @qcode{"emptyvalue"}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
190 @seealso{csvread, textscan, textread, dlmwrite}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
191 @end deftypefn */)
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
192 {
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
193 int nargin = args.length ();
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
194
10935
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
195 double empty_value = 0.0;
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
196
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
197 if (nargin > 2 && args(nargin-2).is_string ()
10935
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
198 && args(nargin-2).string_value () == "emptyvalue")
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
199 {
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
200 empty_value = args(nargin-1).double_value ();
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
201
10935
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
202 nargin -= 2;
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
203 }
3cb4889dd6f7 support specific empty value in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10927
diff changeset
204
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
205 if (nargin < 1 || nargin > 4)
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
206 print_usage ();
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
207
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
208 std::istream *input = 0;
11389
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
209 std::ifstream input_file;
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
210
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
211 if (args(0).is_string ())
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
212 {
20713
2469d78a1d8b Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents: 20555
diff changeset
213 // Filename.
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
214 std::string fname (args(0).string_value ());
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
215
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
216 std::string tname = octave::sys::file_ops::tilde_expand (fname);
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
217
21593
e3888aa9a4d1 Restore tilde expansion for dlmread lost in fe5ed6d7fb10.
Rik <rik@octave.org>
parents: 21592
diff changeset
218 tname = find_data_file_in_load_path ("dlmread", tname);
21592
fe5ed6d7fb10 * dlmread.cc (Fdlmread): Search loadpath for file (bug #47628).
John W. Eaton <jwe@octave.org>
parents: 21362
diff changeset
219
11389
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
220 input_file.open (tname.c_str (), std::ios::in);
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
221
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
222 if (! input_file)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
223 error ("dlmread: unable to open file '%s'", fname.c_str ());
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
224
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
225 input = &input_file;
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
226 }
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
227 else if (args(0).is_scalar_type ())
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
228 {
11389
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
229 octave_stream is = octave_stream_list::lookup (args(0), "dlmread");
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
230
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
231 input = is.input_stream ();
934ed3e07542 dlmread: simplify file name/id logic
John W. Eaton <jwe@octave.org>
parents: 10935
diff changeset
232
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
233 if (! input)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
234 error ("dlmread: stream FILE not open for input");
10927
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
235 }
a9e9659e1dca support file IDs in dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10806
diff changeset
236 else
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
237 error ("dlmread: FILE argument must be a string or file id");
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
238
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
239 // Set default separator.
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
240 std::string sep;
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
241 if (nargin > 1)
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
242 {
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
243 if (args(1).is_sq_string ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
244 sep = do_string_escapes (args(1).string_value ());
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
245 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
246 sep = args(1).string_value ();
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
247 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
248
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
249 // Take a subset if a range was given.
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
250 octave_idx_type r0 = 0;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
251 octave_idx_type c0 = 0;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
252 octave_idx_type r1 = idx_max-1;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
253 octave_idx_type c1 = idx_max-1;
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
254 if (nargin > 2)
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
255 {
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
256 if (nargin == 3)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
257 {
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20940
diff changeset
258 if (! parse_range_spec (args(2), r0, c0, r1, c1))
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
259 error ("dlmread: error parsing RANGE");
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
260 }
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
261 else if (nargin == 4)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
262 {
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
263 r0 = args(2).idx_type_value ();
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
264 c0 = args(3).idx_type_value ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
265 }
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
266
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
267 if (r0 < 0 || c0 < 0)
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
268 error ("dlmread: left & top must be positive");
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
269 }
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
270
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
271 octave_idx_type i = 0;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
272 octave_idx_type j = 0;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
273 octave_idx_type r = 1;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
274 octave_idx_type c = 1;
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
275 // Start with a reasonable size to avoid constant resizing of matrix.
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
276 octave_idx_type rmax = 32;
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
277 octave_idx_type cmax = 0;
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
278
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
279 Matrix rdata (rmax, cmax, empty_value);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
280 ComplexMatrix cdata;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
281
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
282 bool iscmplx = false;
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
283 bool sep_is_wspace = (sep.find_first_of (" \t") != std::string::npos);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
284 bool auto_sep_is_wspace = false;
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
285
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
286 std::string line;
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
287
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
288 // Skip the r0 leading lines (header)
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
289 for (octave_idx_type m = 0; m < r0; m++)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
290 getline (*input, line);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
291 r1 -= r0;
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
292
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
293 std::istringstream tmp_stream;
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
294
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
295 // Read the data one field at a time, growing the data matrix as needed.
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
296 while (getline (*input, line))
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
297 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
298 // Skip blank lines for compatibility.
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
299 if ((! sep_is_wspace || auto_sep_is_wspace)
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
300 && line.find_first_not_of (" \t") == std::string::npos)
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
301 continue;
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
302
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
303 // Infer separator from file if delimiter is blank.
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
304 if (sep.empty ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
305 {
22621
e7699adac1d0 Do a better job of delimiter auto-dedection in dlmread (bug #42022).
Rik <rik@octave.org>
parents: 22407
diff changeset
306 // Skip leading whitespace.
e7699adac1d0 Do a better job of delimiter auto-dedection in dlmread (bug #42022).
Rik <rik@octave.org>
parents: 22407
diff changeset
307 size_t pos1 = line.find_first_not_of (" \t");
e7699adac1d0 Do a better job of delimiter auto-dedection in dlmread (bug #42022).
Rik <rik@octave.org>
parents: 22407
diff changeset
308
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
309 // For Matlab compatibility, blank delimiter should
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
310 // correspond to whitespace (space and tab).
22621
e7699adac1d0 Do a better job of delimiter auto-dedection in dlmread (bug #42022).
Rik <rik@octave.org>
parents: 22407
diff changeset
311 size_t n = line.find_first_of (",:; \t", pos1);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
312 if (n == std::string::npos)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
313 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
314 sep = " \t";
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
315 auto_sep_is_wspace = true;
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
316 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
317 else
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
318 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
319 char ch = line.at (n);
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
320
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
321 switch (line.at (n))
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
322 {
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
323 case ' ':
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
324 case '\t':
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
325 sep = " \t";
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
326 auto_sep_is_wspace = true;
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
327 break;
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
328
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
329 default:
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
330 sep = ch;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
331 break;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
332 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
333 }
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
334 }
10804
3d5c6b84ddaf speed-up fixes to dlmread
Jaroslav Hajek <highegg@gmail.com>
parents: 10718
diff changeset
335
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
336 // Estimate the number of columns from first line of data.
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
337 if (cmax == 0)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
338 {
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
339 size_t pos1, pos2;
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
340 if (auto_sep_is_wspace)
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
341 pos1 = line.find_first_not_of (" \t");
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
342 else
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
343 pos1 = 0;
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
344
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
345 do
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
346 {
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
347 pos2 = line.find_first_of (sep, pos1);
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
348
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
349 if (auto_sep_is_wspace && pos2 != std::string::npos)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
350 {
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
351 // Treat consecutive separators as one.
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
352 pos2 = line.find_first_not_of (sep, pos2);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
353 if (pos2 != std::string::npos)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
354 pos2 -= 1;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
355 }
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
356
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
357 // Separator followed by EOL doesn't generate extra column
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
358 if (pos2 != std::string::npos)
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
359 cmax++;
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
360
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
361 pos1 = pos2 + 1;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
362 }
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
363 while (pos2 != std::string::npos);
7581
e0d7bb35a3ec style fixes
John W. Eaton <jwe@octave.org>
parents: 7580
diff changeset
364
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
365 // FIXME: Should always be the case that iscmplx == false.
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
366 // Flag is initialized that way and no data has been read.
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
367 if (iscmplx)
22657
a93887d7f0da Use EmptyValue option correctly in all instances (bug #42023).
Rik <rik@octave.org>
parents: 22656
diff changeset
368 cdata.resize (rmax, cmax, empty_value);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
369 else
22657
a93887d7f0da Use EmptyValue option correctly in all instances (bug #42023).
Rik <rik@octave.org>
parents: 22656
diff changeset
370 rdata.resize (rmax, cmax, empty_value);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9512
diff changeset
371 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
372
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
373 r = (r > i + 1 ? r : i + 1);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
374 j = 0;
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
375
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
376 size_t pos1, pos2;
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
377 if (auto_sep_is_wspace)
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
378 pos1 = line.find_first_not_of (" \t"); // Skip leading whitespace.
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
379 else
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
380 pos1 = 0;
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
381
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
382 do
20213
ca2da088eada Return an empty matrix, not error, from dlmread when file is empty (bug #45156).
Rik <rik@octave.org>
parents: 20172
diff changeset
383 {
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
384 octave_quit ();
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
385
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
386 pos2 = line.find_first_of (sep, pos1);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
387 std::string str = line.substr (pos1, pos2 - pos1);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
388
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
389 if (auto_sep_is_wspace && pos2 != std::string::npos)
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
390 {
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
391 // Treat consecutive separators as one.
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
392 pos2 = line.find_first_not_of (sep, pos2);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
393 if (pos2 != std::string::npos)
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
394 pos2 -= 1;
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
395 else
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
396 pos2 = line.length () - 1;
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
397 }
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
398
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
399 // Separator followed by EOL doesn't generate extra column
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
400 if (pos2 == std::string::npos && str.empty ())
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
401 break;
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
402
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
403 c = (c > j + 1 ? c : j + 1);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
404 if (r > rmax || c > cmax)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
405 {
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
406 // Use resize_and_fill for the case of unequal length rows.
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
407 rmax = 2 * std::max (r-1, 1); // keep rmax a power of 2
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
408 cmax = std::max (c, cmax);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
409 if (iscmplx)
22657
a93887d7f0da Use EmptyValue option correctly in all instances (bug #42023).
Rik <rik@octave.org>
parents: 22656
diff changeset
410 cdata.resize (rmax, cmax, empty_value);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
411 else
22657
a93887d7f0da Use EmptyValue option correctly in all instances (bug #42023).
Rik <rik@octave.org>
parents: 22656
diff changeset
412 rdata.resize (rmax, cmax, empty_value);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
413 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
414
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
415 tmp_stream.str (str);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
416 tmp_stream.clear ();
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
417
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
418 double x = octave_read_double (tmp_stream);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
419 if (tmp_stream)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
420 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
421 if (tmp_stream.eof ())
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
422 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
423 if (iscmplx)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
424 cdata(i,j++) = x;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
425 else
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
426 rdata(i,j++) = x;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
427 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
428 else if (std::toupper (tmp_stream.peek ()) == 'I')
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
429 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
430 // This is to allow pure imaginary numbers.
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
431 if (iscmplx)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
432 cdata(i,j++) = x;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
433 else
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
434 rdata(i,j++) = x;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
435 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
436 else
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
437 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
438 double y = octave_read_double (tmp_stream);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
439
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20940
diff changeset
440 if (! iscmplx && y != 0.)
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
441 {
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
442 iscmplx = true;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
443 cdata = ComplexMatrix (rdata);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
444 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
445
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
446 if (iscmplx)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
447 cdata(i,j++) = Complex (x, y);
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
448 else
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
449 rdata(i,j++) = x;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
450 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
451 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
452 else if (iscmplx)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
453 cdata(i,j++) = empty_value;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
454 else
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
455 rdata(i,j++) = empty_value;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
456
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
457 pos1 = pos2 + 1;
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
458 }
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
459 while (pos2 != std::string::npos);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
460
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
461 if (i == r1)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
462 break;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
463
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
464 i++;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
465 }
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
466
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
467 if (r1 >= r)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
468 r1 = r - 1;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
469 if (c1 >= c)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
470 c1 = c - 1;
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
471
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
472 // Now take the subset of the matrix if there are any values.
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
473 if (i > 0 || j > 0)
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
474 {
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
475 if (iscmplx)
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
476 cdata = cdata.extract (0, c0, r1, c1);
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
477 else
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
478 rdata = rdata.extract (0, c0, r1, c1);
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
479 }
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
480
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
481 if (iscmplx)
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21062
diff changeset
482 return ovl (cdata);
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20213
diff changeset
483 else
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21062
diff changeset
484 return ovl (rdata);
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
485 }
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
486
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
487 /*
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
488 %!test
19278
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 18112
diff changeset
489 %! file = tempname ();
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
490 %! unwind_protect
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
491 %! fid = fopen (file, "wt");
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
492 %! fwrite (fid, "1, 2, 3\n4, 5, 6\n7, 8, 9\n10, 11, 12");
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
493 %! fclose (fid);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
494 %!
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
495 %! assert (dlmread (file), [1, 2, 3; 4, 5, 6; 7, 8, 9;10, 11, 12]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
496 %! assert (dlmread (file, ","), [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
497 %! assert (dlmread (file, ",", [1, 0, 2, 1]), [4, 5; 7, 8]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
498 %! assert (dlmread (file, ",", "B1..C2"), [2, 3; 5, 6]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
499 %! assert (dlmread (file, ",", "B1:C2"), [2, 3; 5, 6]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
500 %! assert (dlmread (file, ",", "..C2"), [1, 2, 3; 4, 5, 6]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
501 %! assert (dlmread (file, ",", 0, 1), [2, 3; 5, 6; 8, 9; 11, 12]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
502 %! assert (dlmread (file, ",", "B1.."), [2, 3; 5, 6; 8, 9; 11, 12]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
503 %! fail ('dlmread (file, ",", [0 1])');
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
504 %! unwind_protect_cleanup
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
505 %! unlink (file);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
506 %! end_unwind_protect
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
507
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
508 %!test
19278
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 18112
diff changeset
509 %! file = tempname ();
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
510 %! unwind_protect
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
511 %! fid = fopen (file, "wt");
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
512 %! fwrite (fid, "1, 2, 3\n4+4i, 5, 6\n7, 8, 9\n10, 11, 12");
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
513 %! fclose (fid);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
514 %!
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
515 %! assert (dlmread (file), [1, 2, 3; 4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
22657
a93887d7f0da Use EmptyValue option correctly in all instances (bug #42023).
Rik <rik@octave.org>
parents: 22656
diff changeset
516 %! assert (dlmread (file, ","), [1,2,3; 4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
517 %! assert (dlmread (file, ",", [1, 0, 2, 1]), [4 + 4i, 5; 7, 8]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
518 %! assert (dlmread (file, ",", "A2..B3"), [4 + 4i, 5; 7, 8]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
519 %! assert (dlmread (file, ",", "A2:B3"), [4 + 4i, 5; 7, 8]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
520 %! assert (dlmread (file, ",", "..B3"), [1, 2; 4 + 4i, 5; 7, 8]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
521 %! assert (dlmread (file, ",", 1, 0), [4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
522 %! assert (dlmread (file, ",", "A2.."), [4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
523 %! fail ('dlmread (file, ",", [0 1])');
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
524 %! unwind_protect_cleanup
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
525 %! unlink (file);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
526 %! end_unwind_protect
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
527
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
528 %!test <42025>
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
529 %! file = tempname ();
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
530 %! unwind_protect
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
531 %! fid = fopen (file, "wt");
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
532 %! fwrite (fid, " \n 1 2\n11 22\n ");
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
533 %! fclose (fid);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
534 %!
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
535 %! assert (dlmread (file), [1, 2; 11, 22]);
22666
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
536 %! assert (dlmread (file, " "), [ 0, 0, 0, 0
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
537 %! 0, 1, 2, 0
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
538 %! 11, 22, 0, 0
68d5c4759783 dlmread: Return correct number of columns when line ends with separator (bug #42025).
Rik <rik@octave.org>
parents: 22657
diff changeset
539 %! 0, 0, 0, 0]);
22656
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
540 %! unwind_protect_cleanup
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
541 %! unlink (file);
471cc4268677 Overhaul dlmread function (bug #42023, bug #42025).
Rik <rik@octave.org>
parents: 22621
diff changeset
542 %! end_unwind_protect
7580
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
543
b4aa9ef3d3ef Port dlmread, dlmwrite, csvread and csvwrite from octave-forge
David Bateman <dbateman@free.fr>
parents:
diff changeset
544 */
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22323
diff changeset
545