Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[8dc580]: io / src / dlmread.cc Maximize Restore History

Download this file

dlmread.cc    431 lines (365 with data), 9.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
Copyright (C) 2008 Jonathan Stickel
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with ; see s programthe file COPYING. If not, see
<http://www.gnu.org/licenses/>.
*/
// Adapted from previous version of dlmread.occ as authored by Kai
// Habel, but core code has been completely re-written.
#include "config.h"
#include <cctype>
#include <fstream>
#include <octave/lo-ieee.h>
#include <octave/defun-dld.h>
#include <octave/error.h>
#include <octave/oct-obj.h>
#include <octave/utils.h>
static bool
read_cell_spec (std::istream& is, unsigned long& row, unsigned long& col)
{
bool stat = false;
if (is.peek () == std::istream::traits_type::eof ())
stat = true;
else
{
if (::isalpha (is.peek ()))
{
col = 0;
while (is && ::isalpha (is.peek ()))
{
char ch = is.get ();
col *= 26;
if (ch >= 'a')
col += ch - 'a';
else
col += ch - 'A';
}
if (is)
{
is >> row;
row --;
if (is)
stat = true;
}
}
}
return stat;
}
static bool
parse_range_spec (const octave_value& range_spec,
unsigned long& rlo, unsigned long& clo,
unsigned long& rup, unsigned long& cup)
{
bool stat = true;
if (range_spec.is_string ())
{
std::istringstream is (range_spec.string_value ());
char ch = is.peek ();
if (ch == '.' || ch == ':')
{
rlo = 0;
clo = 0;
ch = is.get ();
if (ch == '.')
{
ch = is.get ();
if (ch != '.')
stat = false;
}
}
else
{
stat = read_cell_spec (is, rlo, clo);
if (stat)
{
ch = is.peek ();
if (ch == '.' || ch == ':')
{
ch = is.get ();
if (ch == '.')
{
ch = is.get ();
if (!is || ch != '.')
stat = false;
}
rup = ULONG_MAX - 1;
cup = ULONG_MAX - 1;
}
else
{
rup = rlo;
cup = clo;
if (!is || !is.eof ())
stat = false;
}
}
}
if (stat && is && !is.eof ())
stat = read_cell_spec (is, rup, cup);
if (!is || !is.eof ())
stat = false;
}
else if (range_spec.is_real_matrix () && range_spec.numel () == 4)
{
ColumnVector range(range_spec.vector_value ());
// double --> unsigned int
rlo = static_cast<unsigned long> (range(0));
clo = static_cast<unsigned long> (range(1));
rup = static_cast<unsigned long> (range(2));
cup = static_cast<unsigned long> (range(3));
}
else
stat = false;
return stat;
}
DEFUN_DLD (dlmread, args, ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{data} =} dlmread (@var{file})\n\
@deftypefnx {Loadable Function} {@var{data} =} dlmread (@var{file}, @var{sep})\n\
@deftypefnx {Loadable Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{r0}, @var{c0})\n\
@deftypefnx {Loadable Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{range})\n\
Read the matrix @var{data} from a text file. If not defined the separator\n\
between fields is determined from the file itself. Otherwise the\n\
separation character is defined by @var{sep}.\n\
\n\
Given two scalar arguments @var{r0} and @var{c0}, these define the starting\n\
row and column of the data to be read. These values are indexed from zero,\n\
such that the first row corresponds to an index of zero.\n\
\n\
The @var{range} parameter must be a 4 element vector containing the upper\n\
left and lower right corner @code{[@var{R0},@var{C0},@var{R1},@var{C1}]} or\n\
a spreadsheet style range such as 'A2..Q15'. The lowest index value is zero.\n\
@end deftypefn")
{
octave_value_list retval;
int nargin = args.length ();
if (nargin < 1 || nargin > 4)
{
print_usage ();
return retval;
}
if (!args(0).is_string ())
{
error ("dlmread: 1st argument must be a string");
return retval;
}
std::string fname (args(0).string_value ());
if (error_state)
return retval;
std::ifstream file (fname.c_str ());
if (!file)
{
error ("dlmread: unable to open file `%s'", fname.c_str ());
return retval;
}
// Set default separator.
std::string sep;
if (nargin > 1)
{
if (args(1).is_sq_string ())
sep = do_string_escapes (args(1).string_value ());
else
sep = args(1).string_value ();
if (error_state)
return retval;
}
// Take a subset if a range was given.
unsigned long r0 = 0, c0 = 0, r1 = ULONG_MAX-1, c1 = ULONG_MAX-1;
if (nargin > 2)
{
if (nargin == 3)
{
if (!parse_range_spec (args (2), r0, c0, r1, c1))
error ("dlmread: error parsing range");
}
else if (nargin == 4)
{
r0 = args(2).ulong_value ();
c0 = args(3).ulong_value ();
if (error_state)
return retval;
}
}
if (!error_state)
{
unsigned long i = 0, j = 0, r = 1, c = 1, rmax = 0, cmax = 0;
Matrix rdata;
ComplexMatrix cdata;
bool iscmplx = false;
bool sepflag = false;
unsigned long maxrows = r1 - r0;
std::string line;
// Skip the r0 leading lines as these might be a header.
for (unsigned long m = 0; m < r0; m++)
getline (file, line);
r1 -= r0;
// Read in the data one field at a time, growing the data matrix
// as needed.
while (getline (file, line))
{
// Skip blank lines for compatibility.
if (line.find_first_not_of (" \t") == NPOS)
continue;
// To be compatible with matlab, blank separator should
// correspond to whitespace as delimter.
if (!sep.length ())
{
size_t n = line.find_first_of (",:; \t",
line.find_first_of ("0123456789"));
if (n == NPOS)
{
sep = " \t";
sepflag = true;
}
else
{
char ch = line.at (n);
switch (line.at (n))
{
case ' ':
case '\t':
sepflag = true;
sep = " \t";
break;
default:
sep = ch;
break;
}
}
}
r = (r > i + 1 ? r : i + 1);
j = 0;
size_t pos1 = 0;
do
{
size_t pos2 = line.find_first_of (sep, pos1);
std::string str = line.substr (pos1, pos2 - pos1);
if (sepflag && pos2 != NPOS)
// Treat consecutive separators as one.
pos2 = line.find_first_not_of (sep, pos2) - 1;
c = (c > j + 1 ? c : j + 1);
if (r > rmax || c > cmax)
{
// Use resize_and_fill for the case of not-equal
// length rows.
if (iscmplx)
cdata.resize_and_fill (r, c, 0);
else
rdata.resize_and_fill (r, c, 0);
rmax = r;
cmax = c;
}
std::istringstream tmp_stream (str);
double x = octave_read_double (tmp_stream);
if (tmp_stream)
{
if (tmp_stream.eof ())
if (iscmplx)
cdata(i,j++) = x;
else
rdata(i,j++) = x;
else
{
double y = octave_read_double (tmp_stream);
if (!iscmplx && y != 0.)
{
iscmplx = true;
cdata = ComplexMatrix (rdata);
}
if (iscmplx)
cdata(i,j++) = Complex (x, y);
else
rdata(i,j++) = x;
}
}
else if (iscmplx)
cdata(i,j++) = 0.;
else
rdata(i,j++) = 0.;
if (pos2 != NPOS)
pos1 = pos2 + 1;
else
pos1 = NPOS;
}
while (pos1 != NPOS);
if (nargin == 3 && i == maxrows)
break;
i++;
}
if (nargin > 2)
{
if (nargin == 3)
{
if (r1 >= r)
r1 = r - 1;
if (c1 >= c)
c1 = c - 1;
}
else if (nargin == 4)
{
// If r1 and c1 are not given, use what was found to be
// the maximum.
r1 = r - 1;
c1 = c - 1;
}
// Now take the subset of the matrix.
if (iscmplx)
{
cdata = cdata.extract (0, c0, r1, c1);
cdata.resize (r1 + 1, c1 - c0 + 1);
}
else
{
rdata = rdata.extract (0, c0, r1, c1);
rdata.resize (r1 + 1, c1 - c0 + 1);
}
}
if (iscmplx)
retval(0) = cdata;
else
retval(0) = rdata;
}
return retval;
}
/*
%!shared file
%! file = tmpnam ();
%! fid = fopen (file, "wt");
%! fwrite (fid, "1, 2, 3\n4, 5, 6\n7, 8, 9\n10, 11, 12");
%! fclose (fid);
%!assert (dlmread (file), [1, 2, 3; 4, 5, 6; 7, 8, 9;10, 11, 12]);
%!assert (dlmread (file, ","), [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]);
%!assert (dlmread (file, ",", [1, 0, 2, 1]), [4, 5; 7, 8]);
%!assert (dlmread (file, ",", "B1..C2"), [2, 3; 5, 6]);
%!assert (dlmread (file, ",", "B1:C2"), [2, 3; 5, 6]);
%!assert (dlmread (file, ",", "..C2"), [1, 2, 3; 4, 5, 6]);
%!assert (dlmread (file, ",", 0, 1), [2, 3; 5, 6; 8, 9; 11, 12]);
%!assert (dlmread (file, ",", "B1.."), [2, 3; 5, 6; 8, 9; 11, 12]);
%!error (dlmread (file, ",", [0 1]))
%!test
%! unlink (file);
%!shared file
%! file = tmpnam ();
%! fid = fopen (file, "wt");
%! fwrite (fid, "1, 2, 3\n4+4i, 5, 6\n7, 8, 9\n10, 11, 12");
%! fclose (fid);
%!assert (dlmread (file), [1, 2, 3; 4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
%!assert (dlmread (file, ","), [1, 2, 3; 4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
%!assert (dlmread (file, ",", [1, 0, 2, 1]), [4 + 4i, 5; 7, 8]);
%!assert (dlmread (file, ",", "A2..B3"), [4 + 4i, 5; 7, 8]);
%!assert (dlmread (file, ",", "A2:B3"), [4 + 4i, 5; 7, 8]);
%!assert (dlmread (file, ",", "..B3"), [1, 2; 4 + 4i, 5; 7, 8]);
%!assert (dlmread (file, ",", 1, 0), [4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
%!assert (dlmread (file, ",", "A2.."), [4 + 4i, 5, 6; 7, 8, 9; 10, 11, 12]);
%!error (dlmread (file, ",", [0 1]))
%!test
%! unlink (file);
*/