annotate libinterp/corefcn/textscan.cc @ 21500:7a6589e2867a

more style fixes for textscan * textscan.h, textscan.cc (textscan::scan): New arg, COUNT. Compute number of characters consumed here instead of in Ftextscan. Use ovl to return results. Return Octave stream error message. (Ftextscan): Use octave_istrstream for reading character strings.
author John W. Eaton <jwe@octave.org>
date Sat, 19 Mar 2016 08:53:24 -0400
parents 9cb37fede0b6
children 24aab5c342bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1 /*
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3 Copyright (C) 2015-2016 Lachlan Andrew, Monash University
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
4
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
5 This file is part of Octave.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
6
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
10 option) any later version.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
11
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
15 for more details.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
16
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
20
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
21 */
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
22
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
23 #ifdef HAVE_CONFIG_H
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
24 # include <config.h>
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
25 #endif
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
26
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
27 #include <deque>
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
28 #include <list>
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
29
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
30 #include "Cell.h"
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
31 #include "defun.h"
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
32 #include "oct-stream.h"
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
33 #include "oct-strstrm.h"
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
34 #include "ov.h"
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
35 #include "ovl.h"
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
36 #include "textscan.h"
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
37 #include "utils.h"
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
38
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
39 // Delimited stream, optimised to read strings of characters separated
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
40 // by single-character delimiters.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
41 //
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
42 // The reason behind this class is that octstream doesn't provide
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
43 // seek/tell, but the opportunity has been taken to optimise for the
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
44 // textscan workload.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
45 //
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
46 // The function reads chunks into a 4kiB buffer, and marks where the
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
47 // last delimiter occurs. Reads up to this delimiter can be fast.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
48 // After that last delimiter, the remaining text is moved to the front
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
49 // of the buffer and the buffer is refilled. This also allows cheap
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
50 // seek and tell operations within a "fast read" block.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
51
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
52 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
53 delimited_stream
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
54 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
55 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
56
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
57 delimited_stream (std::istream& is, const std::string& delimiters,
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
58 int longest_lookahead, octave_idx_type bsize = 4096);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
59
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
60 delimited_stream (std::istream& is, const delimited_stream& ds);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
61
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
62 ~delimited_stream (void);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
63
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
64 // Called when optimised sequence of get is finished. Ensures that
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
65 // there is a remaining delimiter in buf, or loads more data in.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
66 void field_done (void)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
67 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
68 if (idx >= last)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
69 refresh_buf ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
70 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
71
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
72 // Load new data into buffer, and set eob, last, idx.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
73 // Return EOF at end of file, 0 otherwise.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
74 int refresh_buf (void);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
75
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
76 // Get a character, relying on caller to call field_done if
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
77 // a delimiter has been reached.
21492
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
78 int get (void)
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
79 {
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
80 if (delimited)
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
81 return eof () ? std::istream::traits_type::eof () : *idx++;
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
82 else
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
83 return get_undelim ();
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
84 }
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
85
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
86 // Get a character, checking for underrun of the buffer.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
87 int get_undelim (void);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
88
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
89 // Read character that will be got by the next get.
21492
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
90 int peek (void) { return eof () ? std::istream::traits_type::eof () : *idx; }
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
91
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
92 // Read character that will be got by the next get.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
93 int peek_undelim (void);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
94
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
95 // Undo a 'get' or 'get_undelim'. It is the caller's responsibility
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
96 // to avoid overflow by calling putbacks only for a character got by
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
97 // get() or get_undelim(), with no intervening
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
98 // get, get_delim, field_done, refresh_buf, getline, read or seekg.
21492
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
99 void putback (char /*ch*/ = 0) { if (! eof ()) --idx; }
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
100
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
101 int getline (std::string& dest, char delim);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
102
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
103 // int skipline (char delim);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
104
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
105 char *read (char *buffer, int size, char* &new_start);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
106
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
107 // Return a position suitable to "seekg", valid only within this
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
108 // block between calls to field_done.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
109 char *tellg (void) { return idx; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
110
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
111 void seekg (char *old_idx) { idx = old_idx; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
112
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
113 bool eof (void)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
114 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
115 return (eob == buf && i_stream.eof ()) || (flags & std::ios_base::eofbit);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
116 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
117
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
118 operator const void* (void) { return (!eof () && !flags) ? this : 0; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
119
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
120 bool fail (void) { return flags & std::ios_base::failbit; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
121
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
122 std::ios_base::iostate rdstate (void) { return flags; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
123
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
124 void setstate (std::ios_base::iostate m) { flags = flags | m; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
125
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
126 void clear (std::ios_base::iostate m
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
127 = (std::ios_base::eofbit & ~std::ios_base::eofbit))
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
128 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
129 flags = flags & m;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
130 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
131
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
132 // Report if any characters have been consumed.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
133 // (get, read etc. not cancelled by putback or seekg)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
134
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
135 void progress_benchmark (void) { progress_marker = idx; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
136
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
137 bool no_progress (void) { return progress_marker == idx; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
138
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
139 private:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
140
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
141 // Number of characters to read from the file at once.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
142 int bufsize;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
143
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
144 // Stream to read from.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
145 std::istream& i_stream;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
146
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
147 // Temporary storage for a "chunk" of data.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
148 char *buf;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
149
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
150 // Current read pointer.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
151 char *idx;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
152
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
153 // Location of last delimiter in the buffer at buf (undefined if
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
154 // delimited is false).
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
155 char *last;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
156
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
157 // Position after last character in buffer.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
158 char *eob;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
159
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
160 // True if there is delimiter in the bufer after idx.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
161 bool delimited;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
162
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
163 // Longest lookahead required.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
164 int longest;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
165
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
166 // Sequence of single-character delimiters.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
167 const std::string delims;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
168
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
169 // Position of start of buf in original stream.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
170 std::streampos buf_in_file;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
171
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
172 // Marker to see if a read consumes any characters.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
173 char *progress_marker;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
174
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
175 std::ios_base::iostate flags;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
176
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
177 // No copying!
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
178
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
179 delimited_stream (const delimited_stream&);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
180
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
181 delimited_stream& operator = (const delimited_stream&);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
182 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
183
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
184 // Create a delimited stream, reading from is, with delimiters delims,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
185 // and allowing reading of up to tellg + longest_lookeahead. When is
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
186 // is at EOF, lookahead may be padded by ASCII nuls.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
187
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
188 delimited_stream::delimited_stream (std::istream& is,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
189 const std::string& delimiters,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
190 int longest_lookahead,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
191 octave_idx_type bsize)
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
192 : bufsize (bsize), i_stream (is), longest (longest_lookahead),
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
193 delims (delimiters),
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
194 flags (std::ios::failbit & ~std::ios::failbit) // can't cast 0
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
195 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
196 buf = new char[bufsize];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
197 eob = buf + bufsize;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
198 idx = eob; // refresh_buf shouldn't try to copy old data
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
199 progress_marker = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
200 refresh_buf (); // load the first batch of data
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
201 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
202
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
203 // Used to create a stream from a strstream from data read from a dstr.
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
204 // FIXME: Find a more efficient approach. Perhaps derived dstr
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
205 delimited_stream::delimited_stream (std::istream& is,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
206 const delimited_stream& ds)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
207 : bufsize (ds.bufsize), i_stream (is), longest (ds.longest),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
208 delims (ds.delims),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
209 flags (std::ios::failbit & ~std::ios::failbit) // can't cast 0
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
210 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
211 buf = new char[bufsize];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
212 eob = buf + bufsize;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
213 idx = eob; // refresh_buf shouldn't try to copy old data
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
214 progress_marker = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
215 refresh_buf (); // load the first batch of data
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
216 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
217
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
218 delimited_stream::~delimited_stream (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
219 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
220 // Seek to the correct position in i_stream.
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
221 if (! eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
222 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
223 i_stream.clear ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
224 i_stream.seekg (buf_in_file);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
225 i_stream.read (buf, idx - buf);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
226 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
227
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
228 delete [] buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
229 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
230
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
231 // Read a character from the buffer, refilling the buffer from the file
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
232 // if necessary.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
233
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
234 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
235 delimited_stream::get_undelim (void)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
236 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
237 int retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
238 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
239 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
240 setstate (std::ios_base::failbit);
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
241 return std::istream::traits_type::eof ();
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
242 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
243
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
244 if (idx < eob)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
245 retval = *idx++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
246 else
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
247 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
248 refresh_buf ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
249
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
250 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
251 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
252 setstate (std::ios_base::eofbit);
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
253 retval = std::istream::traits_type::eof ();
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
254 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
255 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
256 retval = *idx++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
257 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
258
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
259 if (idx >= last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
260 delimited = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
261
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
262 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
263 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
264
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
265 // Return the next character to be read without incrementing the
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
266 // pointer, refilling the buffer from the file if necessary.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
267
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
268 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
269 delimited_stream::peek_undelim (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
270 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
271 int retval = get_undelim ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
272 putback ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
273
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
274 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
275 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
276
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
277 // Copy remaining unprocessed data to the start of the buffer and load
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
278 // new data to fill it. Return EOF if the file is at EOF before
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
279 // reading any data and all of the data that has been read has been
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
280 // processed.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
281
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
282 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
283 delimited_stream::refresh_buf (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
284 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
285 if (eof ())
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
286 return std::istream::traits_type::eof ();
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
287
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
288 int retval;
21498
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
289
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
290 if (eob < idx)
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
291 idx = eob;
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
292
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
293 size_t old_remaining = eob - idx;
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
294
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
295 octave_quit (); // allow ctrl-C
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
296
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
297 if (old_remaining > 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
298 memmove (buf, idx, old_remaining);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
299
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
300 progress_marker -= idx - buf; // where original idx would have been
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
301 idx = buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
302
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
303 int gcount; // chars read
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
304 if (! i_stream.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
305 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
306 buf_in_file = i_stream.tellg (); // record for destructor
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
307 i_stream.read (buf + old_remaining, bufsize - old_remaining);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
308 gcount = i_stream.gcount ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
309 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
310 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
311 gcount = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
312
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
313 eob = buf + old_remaining + gcount;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
314 last = eob;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
315 if (gcount == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
316 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
317 delimited = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
318
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
319 if (eob != buf) // no more data in file, but still some to go
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
320 retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
321 else
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
322 // file and buffer are both done.
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
323 retval = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
324 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
325 else
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
326 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
327 delimited = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
328
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
329 for (last = eob - longest; last - buf >= 0; last--)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
330 {
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
331 if (delims.find (*last) != std::string::npos)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
332 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
333 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
334
21498
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
335 if (last < buf)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
336 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
337
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
338 retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
339 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
340
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
341 // Ensure fast peek doesn't give valid char
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
342 if (retval == std::istream::traits_type::eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
343 *idx = '\0'; // FIXME - check that no TreatAsEmpty etc starts w. \0?
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
344
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
345 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
346 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
347
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
348 // Return a pointer to a block of data of size size, assuming that a
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
349 // sufficiently large buffer is available in buffer, if required.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
350 // If called when delimited == true, and size is no greater than
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
351 // longest_lookahead then this will not call refresh_buf, so seekg
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
352 // still works. Otherwise, seekg may be invalidated.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
353
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
354 char *
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
355 delimited_stream::read (char *buffer, int size, char* &prior_tell)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
356 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
357 char *retval;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
358
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
359 if (eob - idx > size)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
360 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
361 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
362 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
363 if (idx > last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
364 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
365 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
366 else
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
367 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
368 // If there was a tellg pointing to an earlier point than the current
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
369 // read position, try to keep it in the active buffer.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
370 // In the current code, prior_tell==idx for each call,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
371 // so this is not necessary, just a precaution.
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
372
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
373 if (eob - prior_tell + size < bufsize)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
374 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
375 octave_idx_type gap = idx - prior_tell;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
376 idx = prior_tell;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
377 refresh_buf ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
378 idx += gap;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
379 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
380 else // can't keep the tellg in range. May skip some data.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
381 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
382 refresh_buf ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
383 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
384
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
385 prior_tell = buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
386
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
387 if (eob - idx > size)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
388 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
389 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
390 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
391 if (idx > last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
392 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
393 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
394 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
395 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
396 if (size <= bufsize) // small read, but reached EOF
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
397 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
398 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
399 memset (eob, 0, size + (idx - buf));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
400 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
401 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
402 else // Reading more than the whole buf; return it in buffer
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
403 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
404 retval = buffer;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
405 // FIXME -- read bufsize at a time
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
406 int i;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
407 for (i = 0; i < size && ! eof (); i++)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
408 *buffer++ = get_undelim ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
409 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
410 memset (buffer, 0, size - i);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
411 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
412 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
413 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
414
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
415 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
416 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
417
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
418 // Return in OUT an entire line, terminated by delim. On input, OUT
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
419 // must have length at least 1.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
420
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
421 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
422 delimited_stream::getline (std::string& out, char delim)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
423 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
424 int len = out.length (), used = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
425 int ch;
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
426 while ((ch = get_undelim ()) != delim
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
427 && ch != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
428 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
429 out[used++] = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
430 if (used == len)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
431 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
432 len <<= 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
433 out.resize (len);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
434 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
435 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
436 out.resize (used);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
437 field_done ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
438
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
439 return ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
440 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
441
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
442 // A single conversion specifier, such as %f or %c.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
443
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
444 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
445 textscan_format_elt
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
446 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
447 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
448
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
449 enum special_conversion
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
450 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
451 whitespace_conversion = 1,
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
452 literal_conversion = 2
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
453 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
454
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
455 textscan_format_elt (const std::string& txt, int w = 0, int p = -1,
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
456 int bw = 0, bool dis = false, char typ = '\0',
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
457 const std::string& ch_class = std::string ())
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
458 : text (txt), width (w), prec (p), bitwidth (bw),
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
459 char_class (ch_class), type (typ), discard (dis),
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
460 numeric (typ == 'd' || typ == 'u' || type == 'f' || type == 'n')
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
461 { }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
462
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
463 textscan_format_elt (const textscan_format_elt& e)
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
464 : text (e.text), width (e.width), prec (e.prec),
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
465 bitwidth (e.bitwidth), char_class (e.char_class), type (e.type),
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
466 discard (e.discard), numeric (e.numeric)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
467 { }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
468
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
469 textscan_format_elt& operator = (const textscan_format_elt& e)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
470 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
471 if (this != &e)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
472 {
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
473 text = e.text;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
474 width = e.width;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
475 prec = e.prec;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
476 bitwidth = e.bitwidth;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
477 discard = e.discard;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
478 type = e.type;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
479 numeric = e.numeric;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
480 char_class = e.char_class;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
481 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
482
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
483 return *this;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
484 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
485
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
486 // The C-style format string.
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
487 std::string text;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
488
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
489 // The maximum field width.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
490 unsigned int width;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
491
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
492 // The maximum number of digits to read after the decimal in a
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
493 // floating point conversion.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
494 int prec;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
495
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
496 // The size of the result. For integers, bitwidth may be 8, 16, 34,
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
497 // or 64. For floating point values, bitwidth may be 32 or 64.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
498 int bitwidth;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
499
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
500 // The class of characters in a `[' or `^' format.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
501 std::string char_class;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
502
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
503 // Type of conversion
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
504 // -- `d', `u', `f', `n', `s', `q', `c', `%', `C', `D', `[' or `^'.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
505 char type;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
506
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
507 // TRUE if we are not storing the result of this conversion.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
508 bool discard;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
509
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
510 // TRUE if the type is 'd', 'u', 'f', 'n'
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
511 bool numeric;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
512 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
513
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
514 class textscan;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
515
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
516 // The (parsed) sequence of format specifiers.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
517
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
518 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
519 textscan_format_list
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
520 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
521 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
522
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
523 textscan_format_list (const std::string& fmt = std::string ());
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
524
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
525 ~textscan_format_list (void);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
526
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
527 octave_idx_type num_conversions (void) const { return nconv; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
528
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
529 // The length can be different than the number of conversions.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
530 // For example, "x %d y %d z" has 2 conversions but the length of
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
531 // the list is 3 because of the characters that appear after the
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
532 // last conversion.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
533
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
534 size_t numel (void) const { return fmt_elts.size (); }
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
535
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
536 const textscan_format_elt *first (void)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
537 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
538 curr_idx = 0;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
539 return current ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
540 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
541
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
542 const textscan_format_elt *current (void) const
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
543 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
544 return numel () > 0 ? fmt_elts[curr_idx] : 0;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
545 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
546
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
547 const textscan_format_elt *next (bool cycle = true)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
548 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
549 curr_idx++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
550
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
551 if (curr_idx >= numel ())
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
552 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
553 if (cycle)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
554 curr_idx = 0;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
555 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
556 return 0;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
557 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
558
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
559 return current ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
560 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
561
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
562 void printme (void) const;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
563
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
564 bool ok (void) const { return (nconv >= 0); }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
565
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
566 operator const void* (void) const { return ok () ? this : 0; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
567
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
568 // True if number of %f to be set from data file.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
569 bool set_from_first;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
570
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
571 // At least one conversion specifier is s,q,c, or [...].
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
572 bool has_string;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
573
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
574 int read_first_row (delimited_stream& is, textscan& ts);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
575
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
576 std::list<octave_value> out_buf (void) const { return (output_container); }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
577
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
578 private:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
579
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
580 // Number of conversions specified by this format string, or -1 if
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
581 // invalid conversions have been found.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
582 octave_idx_type nconv;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
583
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
584 // Index to current element;
21486
4581d0d52948 avoid signed/unsigned comparison warnings introduced in previous change
John W. Eaton <jwe@octave.org>
parents: 21485
diff changeset
585 size_t curr_idx;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
586
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
587 // List of format elements.
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
588 std::deque<textscan_format_elt*> fmt_elts;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
589
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
590 // list holding column arrays of types specified by conversions
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
591 std::list<octave_value> output_container;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
592
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
593 // Temporary buffer.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
594 std::ostringstream *buf;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
595
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
596 void add_elt_to_list (unsigned int width, int prec, int bitwidth,
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
597 octave_value val_type, bool discard,
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
598 char type,
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
599 const std::string& char_class = std::string ());
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
600
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
601 void process_conversion (const std::string& s, size_t& i, size_t n);
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
602
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
603 int finish_conversion (const std::string& s, size_t& i, size_t n,
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
604 unsigned int& width, int& prec, int& bitwidth,
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
605 octave_value& val_type,
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
606 bool discard, char& type);
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
607 // No copying!
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
608
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
609 textscan_format_list (const textscan_format_list&);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
610
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
611 textscan_format_list& operator = (const textscan_format_list&);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
612 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
613
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
614
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
615 textscan_format_list::textscan_format_list (const std::string& s)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
616 : set_from_first (false), has_string (false), nconv (0), curr_idx (0),
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
617 fmt_elts (), buf (0)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
618 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
619 size_t n = s.length ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
620
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
621 size_t i = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
622
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
623 unsigned int width = -1; // Unspecified width = max (except %c)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
624 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
625 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
626 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
627 char type = '\0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
628
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
629 bool have_more = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
630
21497
2d71bb0011a0 textscan: Use empty () rather than 'length () == 0' on strings
Mike Miller <mtmiller@octave.org>
parents: 21496
diff changeset
631 if (s.empty ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
632 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
633 buf = new std::ostringstream ("%f");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
634 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
635 type = 'f';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
636 add_elt_to_list (width, prec, bitwidth, octave_value (NDArray ()),
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
637 discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
638 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
639 set_from_first = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
640 nconv = 1;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
641 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
642 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
643 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
644 set_from_first = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
645
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
646 while (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
647 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
648 have_more = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
649
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
650 if (! buf)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
651 buf = new std::ostringstream ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
652
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
653 if (s[i] == '%' && (i+1 == n || s[i+1] != '%'))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
654 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
655 // Process percent-escape conversion type.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
656
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
657 process_conversion (s, i, n);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
658
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
659 have_more = (buf != 0);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
660 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
661 else if (isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
662 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
663 while (++i < n && isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
664 /* skip whitespace */;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
665
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
666 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
667 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
668 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
669 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
670 type = textscan_format_elt::literal_conversion;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
671
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
672 width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
673 prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
674 bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
675 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
676
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
677 while (i < n && ! isspace (s[i])
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
678 && (s[i] != '%' || (i+1 < n && s[i+1] == '%')))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
679 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
680 if (s[i] == '%') // if double %, skip one
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
681 i++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
682 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
683 width++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
684 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
685
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
686 add_elt_to_list (width, prec, bitwidth, octave_value (),
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
687 discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
688
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
689 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
690 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
691
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
692 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
693 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
694 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
695 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
696 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
697 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
698 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
699
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
700 if (have_more)
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
701 add_elt_to_list (width, prec, bitwidth, octave_value (), discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
702
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
703 delete buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
704 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
705
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
706 textscan_format_list::~textscan_format_list (void)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
707 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
708 size_t n = numel ();
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
709
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
710 for (size_t i = 0; i < n; i++)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
711 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
712 textscan_format_elt *elt = fmt_elts[i];
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
713 delete elt;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
714 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
715 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
716
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
717 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
718 textscan_format_list::add_elt_to_list (unsigned int width, int prec,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
719 int bitwidth, octave_value val_type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
720 bool discard, char type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
721 const std::string& char_class)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
722 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
723 if (buf)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
724 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
725 std::string text = buf->str ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
726
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
727 if (! text.empty ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
728 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
729 textscan_format_elt *elt
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
730 = new textscan_format_elt (text, width, prec, bitwidth, discard, type, char_class);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
731
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
732 if (! discard)
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
733 output_container.push_back (val_type);
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
734
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
735 fmt_elts.push_back (elt);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
736 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
737
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
738 delete buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
739 buf = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
740 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
741 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
742
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
743 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
744 textscan_format_list::process_conversion (const std::string& s, size_t& i,
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
745 size_t n)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
746 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
747 unsigned width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
748 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
749 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
750 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
751 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
752 char type = '\0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
753
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
754 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
755
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
756 bool have_width = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
757
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
758 while (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
759 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
760 switch (s[i])
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
761 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
762 case '*':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
763 if (discard)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
764 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
765 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
766 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
767 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
768 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
769 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
770 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
771
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
772 case '0': case '1': case '2': case '3': case '4':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
773 case '5': case '6': case '7': case '8': case '9':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
774 if (have_width)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
775 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
776 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
777 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
778 char c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
779 width = width * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
780 have_width = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
781 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
782 while (i < n && isdigit (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
783 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
784 c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
785 width = width * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
786 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
787 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
788
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
789 if (i < n && s[i] == '.')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
790 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
791 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
792 prec = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
793 while (i < n && isdigit (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
794 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
795 c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
796 prec = prec * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
797 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
798 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
799 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
800 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
801 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
802
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
803 case 'd': case 'u':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
804 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
805 bool done = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
806 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
807 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
808 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
809 if (s[i] == '8')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
810 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
811 bitwidth = 8;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
812 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
813 val_type = octave_value (int8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
814 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
815 val_type = octave_value (uint8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
816 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
817 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
818 else if (s[i] == '1' && i+1 < n && s[i+1] == '6')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
819 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
820 bitwidth = 16;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
821 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
822 val_type = octave_value (int16NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
823 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
824 val_type = octave_value (uint16NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
825 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
826 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
827 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
828 else if (s[i] == '3' && i+1 < n && s[i+1] == '2')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
829 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
830 done = false; // use default size below
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
831 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
832 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
833 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
834 else if (s[i] == '6' && i+1 < n && s[i+1] == '4')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
835 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
836 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
837 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
838 val_type = octave_value (int64NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
839 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
840 val_type = octave_value (uint64NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
841 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
842 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
843 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
844 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
845 done = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
846 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
847 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
848 done = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
849
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
850 if (! done)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
851 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
852 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
853 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
854 val_type = octave_value (int32NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
855 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
856 val_type = octave_value (uint32NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
857 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
858 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
859 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
860
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
861 case 'f':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
862 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
863 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
864 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
865 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
866 if (s[i] == '3' && i+1 < n && s[i+1] == '2')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
867 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
868 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
869 val_type = octave_value (FloatNDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
870 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
871 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
872 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
873 else if (s[i] == '6' && i+1 < n && s[i+1] == '4')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
874 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
875 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
876 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
877 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
878 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
879 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
880 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
881 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
882 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
883 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
884 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
885
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
886 case 'n':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
887 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
888 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
889 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
890 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
891
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
892 case 's': case 'q': case '[': case 'c':
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
893 if (! discard)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
894 val_type = octave_value (Cell ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
895 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
896 has_string = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
897 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
898
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
899 fini:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
900 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
901 if (! have_width)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
902 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
903 if (type == 'c') // %c defaults to one character
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
904 width = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
905 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
906 width = static_cast<unsigned int> (-1); // others: unlimited
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
907 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
908
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
909 if (finish_conversion (s, i, n, width, prec, bitwidth, val_type,
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
910 discard, type) == 0)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
911 return;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
912 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
913 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
914
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
915 default:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
916 error ("textscan: '%%%c' is not a valid format specifier", s[i]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
917 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
918
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
919 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
920 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
921 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
922
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
923 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
924 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
925
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
926 // Parse [...] and [^...]
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
927 //
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
928 // Matlab does not expand expressions like A-Z, but they are useful, and
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
929 // so we parse them "carefully". We treat '-' as a usual character
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
930 // unless both start and end characters are from the same class (upper
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
931 // case, lower case, numeric), or this is not the first '-' in the
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
932 // pattern.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
933 //
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
934 // Keep both a running list of characters and a mask of which chars have
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
935 // occurred. The first is efficient for patterns with few characters.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
936 // The latter is efficient for [^...] patterns.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
937
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
938 static std::string
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
939 textscan_char_class (const std::string& pattern)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
940 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
941 int len = pattern.length ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
942 if (len == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
943 return "";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
944
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
945 std::string retval (256, '\0');
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
946 std::string mask (256, '\0'); // number of times chr has been seen
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
947
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
948 int in = 0, out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
949 unsigned char ch, prev = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
950 bool flip = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
951
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
952 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
953 if (ch == '^')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
954 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
955 in++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
956 flip = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
957 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
958 mask[pattern[in]] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
959 retval[out++] = pattern[in++]; // even copy ']' if it is first
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
960
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
961 bool prev_was_range = false; // disallow "a-m-z" as a pattern
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
962 bool prev_prev_was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
963 for (; in < len; in++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
964 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
965 bool was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
966 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
967 if (ch == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
968 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
969
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
970 if (prev == '-' && in > 1 && isalnum (ch) && ! prev_prev_was_range)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
971 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
972 unsigned char start_of_range = pattern[in-2];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
973 if (start_of_range < ch
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
974 && ((isupper (ch) && isupper (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
975 || (islower (ch) && islower (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
976 || (isdigit (ch) && isdigit (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
977 || mask['-'] > 1)) // not the first '-'
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
978 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
979 was_range = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
980 out--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
981 mask['-']--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
982 for (int i = start_of_range; i <= ch; i++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
983 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
984 if (mask[i] == '\0')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
985 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
986 mask[i] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
987 retval[out++] = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
988 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
989 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
990 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
991 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
992 if (! was_range)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
993 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
994 if (mask[ch]++ == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
995 retval[out++] = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
996 else if (ch != '-')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
997 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
998 "textscan: [...] contains two '%c's", ch);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
999
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1000 if (prev == '-' && mask['-'] >= 2)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1001 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1002 "textscan: [...] contains two '-'s "
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1003 "outside range expressions");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1004 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1005 prev = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1006 prev_prev_was_range = prev_was_range;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1007 prev_was_range = was_range;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1008 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1009
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1010 if (flip) // [^...]
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1011 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1012 out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1013 for (int i = 0; i < 256; i++)
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1014 if (! mask[i])
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1015 retval[out++] = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1016 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1017
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1018 retval.resize (out);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1019
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1020 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1021 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1022
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1023 int
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1024 textscan_format_list::finish_conversion (const std::string& s, size_t& i,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1025 size_t n, unsigned int& width,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1026 int& prec, int& bitwidth,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1027 octave_value& val_type, bool discard,
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1028 char& type)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1029 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1030 int retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1031
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1032 std::string char_class;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1033
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1034 size_t beg_idx = std::string::npos;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1035 size_t end_idx = std::string::npos;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1036
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1037 if (type != '%')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1038 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1039 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1040 if (type == '[')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1041 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1042 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1043 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1044 beg_idx = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1045
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1046 if (s[i] == '^')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1047 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1048 type = '^';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1049 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1050
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1051 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1052 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1053 beg_idx = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1054
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1055 if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1056 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1057 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1058 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1059 else if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1060 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1061 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1062
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1063 while (i < n && s[i] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1064 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1065
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1066 if (i < n && s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1067 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1068 end_idx = i-1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1069 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1070 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1071
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1072 if (s[i-1] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1073 retval = nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1074 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1075 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1076
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1077 if (nconv >= 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1078 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1079 if (beg_idx != std::string::npos && end_idx != std::string::npos)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1080 char_class = textscan_char_class (s.substr (beg_idx,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1081 end_idx - beg_idx + 1));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1082
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1083 add_elt_to_list (width, prec, bitwidth, val_type, discard, type,
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1084 char_class);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1085 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1086
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1087 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1088 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1089
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1090 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1091 textscan_format_list::printme (void) const
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1092 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1093 size_t n = numel ();
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1094
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1095 for (size_t i = 0; i < n; i++)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1096 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1097 textscan_format_elt *elt = fmt_elts[i];
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1098
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1099 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1100 << "width: " << elt->width << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1101 << "digits " << elt->prec << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1102 << "bitwidth: " << elt->bitwidth << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1103 << "discard: " << elt->discard << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1104 << "type: ";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1105
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1106 if (elt->type == textscan_format_elt::literal_conversion)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1107 std::cerr << "literal text\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1108 else if (elt->type == textscan_format_elt::whitespace_conversion)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1109 std::cerr << "whitespace\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1110 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1111 std::cerr << elt->type << "\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1112
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1113 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1114 << "char_class: `" << undo_string_escapes (elt->char_class) << "'\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1115 << "text: `" << undo_string_escapes (elt->text) << "'\n\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1116 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1117 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1118
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1119 // If FORMAT is explicitly "", it is assumed to be "%f" repeated enough
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1120 // times to read the first row of the file. Set it now.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1121
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1122 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1123 textscan_format_list::read_first_row (delimited_stream& is, textscan& ts)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1124 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1125 // Read first line and strip end-of-line, which may be two characters
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1126 std::string first_line (20, ' ');
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1127
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1128 is.getline (first_line, static_cast<char> (ts.eol2));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1129
21497
2d71bb0011a0 textscan: Use empty () rather than 'length () == 0' on strings
Mike Miller <mtmiller@octave.org>
parents: 21496
diff changeset
1130 if (! first_line.empty ()
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1131 && first_line[first_line.length () - 1] == ts.eol1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1132 first_line.resize (first_line.length () - 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1133
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1134 std::istringstream strstr (first_line);
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1135 delimited_stream ds (strstr, is);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1136
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1137 dim_vector dv (1,1); // initial size of each output_container
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1138 Complex val;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1139 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1140 nconv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1141 int max_empty = 1000; // failsafe, if ds fails but not with eof
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1142 int retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1143
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1144 // read line, creating output_container as we go
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1145 while (! ds.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1146 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1147 bool already_skipped_delim = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1148 ts.skip_whitespace (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1149 ds.progress_benchmark ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1150 bool progress = false;
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1151 ts.scan_complex (ds, *fmt_elts[0], val);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1152 if (ds.fail ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1153 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1154 ds.clear (ds.rdstate () & ~std::ios::failbit);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1155
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1156 if (ds.eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1157 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1158
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1159 // If we don't continue after a conversion error, then
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1160 // unless this was a missing value (i.e., followed by a delimiter),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1161 // return with an error status.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1162 if (ts.return_on_error < 2)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1163 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1164 ts.skip_delim (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1165 if (ds.no_progress ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1166 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1167 retval = 4;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1168 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1169 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1170 already_skipped_delim = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1171 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1172 else // skip offending field
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1173 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1174 std::ios::iostate state = ds.rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1175 ds.clear (); // clear to allow read pointer to advance
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1176
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1177 std::string dummy;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1178 textscan_format_elt fe ("", first_line.length ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1179 ts.scan_string (ds, fe, dummy);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1180
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1181 progress = (dummy.length ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1182 ds.setstate (state);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1183 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1184
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1185 val = ts.empty_value.scalar_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1186
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1187 if (! --max_empty)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1188 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1189 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1190
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1191 if (val.imag () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1192 val_type = octave_value (NDArray (dv, val.real ()));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1193 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1194 val_type = octave_value (ComplexNDArray (dv, val));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1195
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1196 output_container.push_back (val_type);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1197
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1198 if (! already_skipped_delim)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1199 ts.skip_delim (ds);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1200
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1201 if (! progress && ds.no_progress ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1202 break;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1203
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1204 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1205 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1206
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1207 output_container.pop_front (); // discard empty element from constructor
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1208
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1209 // Create fmt_list now that the size is known
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1210 for (octave_idx_type i = 1; i < nconv; i++)
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1211 fmt_elts.push_back (new textscan_format_elt (*fmt_elts[0]));
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1212
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1213 return retval; // May have returned 4 above.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1214 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1215
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1216 // Perform actual textscan: read data from stream, and create cell array.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1217
21488
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1218 static Cell
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1219 init_inf_nan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1220 {
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1221 Cell retval (dim_vector (1, 2));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1222
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1223 retval(0) = Cell (octave_value ("inf"));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1224 retval(1) = Cell (octave_value ("nan"));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1225
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1226 return retval;
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1227 }
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1228
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1229 textscan::textscan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1230 : buf (), whitespace_table (), delim_table (), delims (),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1231 comment_style (), comment_len (0), comment_char (-2),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1232 buffer_size (0), date_locale (), inf_nan (init_inf_nan ()),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1233 empty_value (octave_NaN), exp_chars ("edED"),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1234 header_lines (0), treat_as_empty (), treat_as_empty_len (0),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1235 whitespace (" \b\t"), eol1 ('\r'), eol2 ('\n'),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1236 return_on_error (2), collect_output (false),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1237 multiple_delims_as_one (false), default_exp (true),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1238 numeric_delim (false), lines (0)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1239 { }
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1240
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1241 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1242 textscan::scan (std::istream& isp, const octave_value_list& args)
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1243 {
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1244 octave_idx_type count = 0;
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1245
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1246 return scan (isp, args, count);
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1247 }
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1248
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1249 octave_value
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1250 textscan::scan (std::istream& isp, const octave_value_list& args,
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1251 octave_idx_type& count)
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1252 {
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1253 std::string format;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1254 int params = 0;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1255
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1256 if (args.length () == 0)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1257 format = "%f"; // ommited format = %f. explicit "" = width from file
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1258 else if (args(0).is_string ())
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1259 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1260 format = args(0).string_value ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1261
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1262 if (args(0).is_sq_string ())
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1263 format = do_string_escapes (format);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1264
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1265 params++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1266 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1267 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1268 error ("textscan: FORMAT must be a string, not <%s>",
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1269 args(0).class_name ().c_str ());
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1270
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1271 octave_idx_type ntimes = -1;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1272
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1273 if (args.length () > 1)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1274 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1275 if (args(1).is_numeric_type ())
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1276 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1277 ntimes = args(1).idx_type_value ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1278
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1279 if (ntimes < args(1).double_value ())
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1280 error ("textscan: REPEAT = %g is too large",
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1281 args(1).double_value ());
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1282
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1283 params++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1284 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1285 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1286
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1287 octave_value_list tmp_args = args.splice (0, params);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1288
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1289 textscan_format_list fmt_list (format);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1290
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1291 parse_options (tmp_args, fmt_list);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1292
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1293 octave_value result = do_scan (isp, fmt_list, ntimes);
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1294
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1295 // FIXME: this is probably not the best way to get count. The
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1296 // position could easily be larger than octave_idx_type when using
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1297 // 32-bit indexing.
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1298
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1299 std::ios::iostate state = isp.rdstate ();
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1300 isp.clear ();
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1301 count = static_cast<octave_idx_type> (isp.tellg ());
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1302 isp.setstate (state);
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1303
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
1304 return result;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1305 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1306
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1307 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1308 textscan::do_scan (std::istream& isp, textscan_format_list& fmt_list,
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1309 octave_idx_type ntimes)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1310 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1311 octave_value retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1312
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1313 if (fmt_list.num_conversions () == -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1314 error ("textscan: invalid format specified");
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1315
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1316 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1317 error ("textscan: no valid format conversion specifiers\n");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1318
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1319 // skip the first header_lines
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1320 std::string dummy;
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1321 for (int i = 0; i < header_lines && isp; i++)
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1322 getline (isp, dummy, static_cast<char> (eol2));
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1323
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1324 // Create our own buffered stream, for fast get/putback/tell/seek.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1325
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1326 // First, see how far ahead it should let us look.
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1327 int max_lookahead = std::max (std::max (comment_len, treat_as_empty_len),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1328 std::max (delim_len, 3)); // 3 for NaN and Inf
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1329
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1330 // Next, choose a buffer size to avoid reading too much, or too often.
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1331 octave_idx_type buf_size = 4096;
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1332 if (buffer_size)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1333 buf_size = buffer_size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1334 else if (ntimes > 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1335 {
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1336 // Avoid overflow of 80*ntimes...
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1337 buf_size = std::min (buf_size, std::max (ntimes, 80 * ntimes));
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1338 buf_size = std::max (buf_size, ntimes);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1339 }
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1340 // Finally, create the stream.
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1341 delimited_stream is (isp, whitespace + delims, max_lookahead, buf_size);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1342
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1343 // Grow retval dynamically. "size" is half the initial size
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1344 // (FIXME -- Should we start smaller if ntimes is large?)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1345 octave_idx_type size = ((ntimes < 8 && ntimes >= 0) ? ntimes : 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1346 Array<octave_idx_type> row_idx (dim_vector (1,2));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1347 row_idx(1) = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1348
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1349 int err = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1350 octave_idx_type row = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1351
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1352 if (multiple_delims_as_one) // bug #44750?
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1353 skip_delim (is);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1354
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1355 int done_after; // Number of columns read when EOF seen.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1356
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1357 // If FORMAT explicitly "", read first line and see how many "%f" match
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1358 if (fmt_list.set_from_first)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1359 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1360 err = fmt_list.read_first_row (is, *this);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1361 lines = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1362
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1363 done_after = fmt_list.numel () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1364 if (! err)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1365 row = 1; // the above puts the first line into fmt_list.out_buf ()
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1366 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1367 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1368 done_after = fmt_list.out_buf ().size () + 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1369
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1370 std::list<octave_value> out = fmt_list.out_buf ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1371
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1372 // We will later merge adjacent columns of the same type.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1373 // Check now which columns to merge.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1374 // Reals may become complex, and so we can't trust types
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1375 // after reading in data.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1376 // If the format was "", that conversion may already have happened,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1377 // so force all to be merged (as all are %f).
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1378 bool merge_with_prev[fmt_list.numel ()];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1379 int conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1380 if (collect_output)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1381 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1382 int prev_type = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1383 for (std::list<octave_value>::iterator col = out.begin ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1384 col != out.end (); col++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1385 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1386 if (col->type_id () == prev_type
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1387 || (fmt_list.set_from_first && prev_type != -1))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1388 merge_with_prev [conv++] = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1389 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1390 merge_with_prev [conv++] = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1391
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1392 prev_type = col->type_id ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1393 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1394 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1395
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1396 // This should be caught by earlier code, but this avoids a possible
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1397 // infinite loop below.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1398 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1399 error ("textscan: No conversions specified");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1400
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1401 // Read the data. This is the main loop.
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1402 if (! err)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1403 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1404 for (/* row set ~30 lines above */; row < ntimes || ntimes == -1; row++)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1405 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1406 if (row == 0 || row >= size)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1407 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1408 size += size+1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1409 for (std::list<octave_value>::iterator col = out.begin ();
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1410 col != out.end (); col++)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1411 *col = (*col).resize (dim_vector (size, 1), 0);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1412 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1413
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1414 row_idx(0) = row;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1415 err = read_format_once (is, fmt_list, out, row_idx, done_after);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1416
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1417 if (err > 0 || ! is || (lines >= ntimes && ntimes > -1))
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1418 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1419 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1420 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1421
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1422 if ((err & 4) && ! return_on_error)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1423 error ("textscan: Read error in field %d of row %d",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1424 done_after + 1, row + 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1425
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1426 // If file does not end in EOL, do not pad columns with NaN.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1427 bool uneven_columns = false;
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1428 if (err & 4)
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1429 uneven_columns = true;
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1430 else if (isp.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1431 {
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1432 isp.clear ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1433 isp.seekg (-1, std::ios_base::end);
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1434 int last_char = isp.get ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1435 isp.setstate (isp.eofbit);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1436 uneven_columns = (last_char != eol1 && last_char != eol2);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1437 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1438
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1439 // convert return value to Cell array
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1440 Array<octave_idx_type> ra_idx (dim_vector (1,2));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1441
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1442 // (err & 1) means "error, and no columns read this row
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1443 // FIXME -- This may redundant now that done_after=0 says the same
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1444 if (err & 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1445 done_after = out.size () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1446
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1447 int valid_rows = (row == ntimes) ? ntimes : ((err & 1) ? row : row+1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1448 dim_vector dv (valid_rows, 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1449
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1450 ra_idx(0) = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1451 int i = 0;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1452 if (! collect_output)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1453 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1454 retval = Cell (dim_vector (1, out.size ()));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1455 for (std::list<octave_value>::iterator col = out.begin ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1456 col != out.end (); col++, i++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1457 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1458 // trim last columns if that was requested
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1459 if (i == done_after && uneven_columns)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1460 dv = dim_vector (std::max (valid_rows - 1, 0), 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1461
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1462 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1463 retval = do_cat_op (retval, octave_value (Cell (col->resize (dv,0))),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1464 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1465 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1466 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1467 else // group adjacent cells of the same type into a single cell
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1468 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1469 octave_value cur; // current cell, accumulating columns
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1470 octave_idx_type group_size = 0; // columns in this cell
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1471 int prev_type = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1472
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1473 conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1474 retval = Cell ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1475 for (std::list<octave_value>::iterator col = out.begin ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1476 col != out.end (); col++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1477 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1478 if (! merge_with_prev [conv++]) // including first time
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1479 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1480 if (prev_type != -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1481 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1482 ra_idx(1) = i++;
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
1483 retval = do_cat_op (retval, octave_value (Cell (cur)),
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1484 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1485 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1486 cur = octave_value (col->resize (dv,0));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1487 group_size = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1488 prev_type = col->type_id ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1489 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1490 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1491 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1492 ra_idx(1) = group_size++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1493 cur = do_cat_op (cur, octave_value (col->resize (dv,0)),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1494 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1495 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1496 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1497 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1498 retval = do_cat_op (retval, octave_value (Cell (cur)), ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1499 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1500
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1501 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1502 }
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1503
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1504 // Calculate x^n. Used for ...e+nn so that, for example, 1e2 is
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1505 // exactly 100 and 5e-1 is 1/2
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1506
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1507 static double
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1508 pown (double x, unsigned int n)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1509 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1510 double retval = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1511
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1512 for (unsigned int d = n; d; d >>= 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1513 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1514 if (d & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1515 retval *= x;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1516 x *= x;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1517 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1518
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1519 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1520 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1521
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1522 // Read a double considering the "precision" field of fmt and the
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1523 // exp_chars option of options.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1524
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1525 double
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1526 textscan::read_double (delimited_stream& is,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1527 const textscan_format_elt& fmt) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1528 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1529 int sign = 1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1530 unsigned int width_left = fmt.width;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1531 double retval = 0;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1532 bool valid = false; // syntactically correct double?
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1533
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1534 int ch = is.peek ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1535
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1536 if (ch == '+')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1537 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1538 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1539 ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1540 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1541 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1542 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1543 else if (ch == '-')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1544 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1545 sign = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1546 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1547 ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1548 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1549 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1550 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1551
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1552 // Read integer part
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1553 if (ch != '.')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1554 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1555 if (ch >= '0' && ch <= '9') // valid if at least one digit
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1556 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1557 while (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1558 retval = retval * 10 + (ch - '0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1559 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1560 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1561
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1562 // Read fractional part, up to specified precision
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1563 if (ch == '.' && width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1564 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1565 double multiplier = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1566 int precision = fmt.prec;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1567 int i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1568
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1569 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1570 width_left--; // Consider width of '.'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1571
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1572 if (precision == -1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1573 precision = 1<<30; // FIXME Should be MAXINT
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1574
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1575 if (! valid) // if there was nothing before '.'...
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1576 is.get (); // ...ch was a "peek", not "get".
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1577
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1578 for (i = 0; i < precision; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1579 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1580 if (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1581 retval += (ch - '0') * (multiplier *= 0.1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1582 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1583 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1584 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1585 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1586 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1587 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1588
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1589 // round up if we truncated and the next digit is >= 5
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1590 if ((i == precision || ! width_left) && (ch = is.get ()) >= '5'
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1591 && ch <= '9')
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1592 retval += multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1593
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1594 if (i > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1595 valid = true; // valid if at least one digit after '.'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1596
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1597 // skip remainder after '.', to field width, to look for exponent
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1598 if (i == precision)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1599 while (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1600 ; // discard
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1601
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1602 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1603 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1604
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1605 // look for exponent part in, e.g., 6.023E+23
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1606 bool used_exp = false;
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1607 if (valid && width_left > 1 && exp_chars.find (ch) != std::string::npos)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1608 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1609 int ch1 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1610 if (ch1 == '-' || ch1 == '+' || (ch1 >= '0' && ch1 <= '9'))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1611 { // if 1.0e+$ or some such, this will set failbit, as we want
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1612 width_left--; // count "E"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1613 int exp = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1614 int exp_sign = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1615 if (ch1 == '+')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1616 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1617 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1618 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1619 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1620 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1621 else if (ch1 == '-')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1622 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1623 exp_sign = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1624 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1625 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1626 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1627 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1628 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1629 while (width_left-- && is && (ch = is.get ()) >= '0' && ch1 <= '9')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1630 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1631 exp = exp*10 + ch - '0';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1632 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1633 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1634 width_left++;
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1635 if (ch != std::istream::traits_type::eof () && width_left)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1636 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1637
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1638 double multiplier = pown (10, exp);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1639 if (exp_sign > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1640 retval *= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1641 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1642 retval /= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1643
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1644 used_exp = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1645 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1646 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1647 is.clear ();
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1648 if (! used_exp && ch != std::istream::traits_type::eof () && width_left)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1649 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1650
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1651 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1652 if (! valid && width_left >= 3)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1653 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1654 int i = lookahead (is, inf_nan, 3, false); // false -> case insensitive
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1655 if (i == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1656 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1657 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1658 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1659 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1660 else if (i == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1661 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1662 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1663 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1664 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1665 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1666
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1667 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1668 if (! valid && width_left >= 3)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1669 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1670 int i = lookahead (is, inf_nan, 3, false); // false -> case insensitive
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1671 if (i == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1672 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1673 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1674 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1675 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1676 else if (i == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1677 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1678 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1679 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1680 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1681 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1682
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1683 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1684 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1685 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1686 is.setstate (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1687
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1688 return retval * sign;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1689 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1690
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1691 // Read a single number: real, complex, inf, NaN, possibly with limited
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1692 // precision. Calls to this should be preceded by skip_whitespace.
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1693 // Calling that inside scan_complex would violate its const declaration.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1694
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1695 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1696 textscan::scan_complex (delimited_stream& is, const textscan_format_elt& fmt,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1697 Complex& val) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1698 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1699 double im = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1700 double re = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1701 bool as_empty = false; // did we fail but match a "treat_as_empty" string?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1702 bool inf = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1703
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1704 int ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1705 if (ch == '+' || ch == '-') // check for [+-][ij] with no coefficients
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1706 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1707 ch = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1708 int ch2 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1709 if (ch2 == 'i' || ch2 == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1710 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1711 double value = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1712 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1713 // Check not -inf
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1714 if (is.peek () == 'n')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1715 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1716 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1717 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1718
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1719 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1720 ch2 = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1721 if (ch2 == 'f')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1722 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1723 inf = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1724 re = (ch == '+') ? octave_Inf : -octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1725 value = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1726 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1727 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1728 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1729 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1730 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1731 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1732 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1733
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1734 im = (ch == '+') ? value : -value;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1735 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1736 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1737 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1738 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1739
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1740 if (! im && ! inf) // if not [+-][ij] or [+-]inf, read real normally
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1741 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1742 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1743 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1744 //re = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1745 re = read_double (is, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1746
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1747 // check for "treat as empty" string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1748 if (treat_as_empty.numel ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1749 && (is.fail () || octave_is_NaN_or_NA (Complex (re))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1750 || re == octave_Inf))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1751 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1752
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1753 for (int i = 0; i < treat_as_empty.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1754 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1755 if (ch == treat_as_empty (i).string_value ()[0])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1756 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1757 as_empty = true; // first char matches, so read the lot
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1758 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1759 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1760 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1761 if (as_empty) // if first char matched...
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1762 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1763 as_empty = false; // ...look for the whole string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1764
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1765 is.clear (state); // treat_as_empty "-" causes partial read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1766 is.seekg (pos); // reset to position before failed read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1767
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1768 // treat_as_empty strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1769 // Read ahead longest, put it all back, then re-read the string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1770 // that matches.
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
1771 std::string look_buf (treat_as_empty_len, '\0');
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
1772 char *look = is.read (&look_buf[0], look_buf.size (), pos);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1773
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1774 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1775 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1776 // FIXME -- is.read could invalidate pos
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1777
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1778 for (int i = 0; i < treat_as_empty.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1779 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1780 std::string s = treat_as_empty (i).string_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1781 if (! strncmp (s.c_str (), look, s.size ()))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1782 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1783 as_empty = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1784 // read just the right amount
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
1785 is.read (&look_buf[0], s.size (), pos);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1786 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1787 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1788 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1789 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1790 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1791
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1792 if (! is.eof () && ! as_empty)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1793 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1794 state = is.rdstate (); // before tellg, since that fails at EOF
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1795 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1796 ch = is.peek (); // ch == EOF if read failed; no need to chk fail
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1797 if (ch == 'i' || ch == 'j') // pure imaginary
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1798 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1799 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1800 im = re;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1801 re = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1802 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1803 else if (ch == '+' || ch == '-') // see if it is real+imag[ij]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1804 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1805 // save stream state in case we have to restore it
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1806 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1807 state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1808
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1809 //im = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1810 im = read_double (is, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1811 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1812 im = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1813
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1814 if (is.peek () == 'i' || is.peek () == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1815 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1816 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1817 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1818 im = 0; // no valid imaginary part. Restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1819 is.clear (state); // eof shouldn't cause fail.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1820 is.seekg (pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1821 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1822 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1823 else if (is.eof ()) // we've read enough to be a "valid" read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1824 is.clear (state); // failed peek shouldn't cause fail
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1825 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1826 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1827 if (as_empty)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1828 val = empty_value.scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1829 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1830 val = Complex (re, im);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1831 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1832
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1833 // Return in VAL the run of characters from IS NOT contained in PATTERN.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1834
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1835 int
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1836 textscan::scan_caret (delimited_stream& is, const std::string& pattern,
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1837 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1838 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1839 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1840 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1841
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1842 while (is && ((c1 = (is && ! is.eof ())
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1843 ? is.get_undelim ()
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1844 : std::istream::traits_type::eof ())
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1845 != std::istream::traits_type::eof ())
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1846 && pattern.find (c1) == std::string::npos)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1847 obuf << static_cast<char> (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1848
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1849 val = obuf.str ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1850
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1851 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1852 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1853
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1854 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1855 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1856
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1857 // Read until one of the strings in DELIMITERS is found. For
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1858 // efficiency, ENDS is a list of the last character of each delimiter.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1859
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1860 std::string
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1861 textscan::read_until (delimited_stream& is, const Cell& delimiters,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1862 const std::string& ends) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1863 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1864 std::string retval ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1865 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1866 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1867 { // find sequence ending with an ending char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1868 std::string next;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1869 scan_caret (is, ends.c_str (), next);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1870 retval = retval + next; // FIXME -- could use repeated doubling of size
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1871
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1872 int last = (! is.eof ()
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1873 ? is.get_undelim () : std::istream::traits_type::eof ());
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1874
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1875 if (last != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1876 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1877 retval = retval + static_cast<char> (last);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1878 for (int i = 0; i < delimiters.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1879 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1880 std::string delim = delimiters(i).string_value ();
21498
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
1881 size_t start = (retval.length () > delim.length ()
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
1882 ? retval.length () - delim.length ()
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
1883 : 0);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1884 std::string may_match = retval.substr (start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1885 if (may_match == delim)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1886 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1887 done = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1888 retval = retval.substr (0, start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1889 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1890 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1891 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1892 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1893 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1894 while (! done && is && ! is.eof ());
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1895
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1896 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1897 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1898
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1899
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1900 // Read stream until either fmt.width chars have been read, or
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1901 // options.delimiter has been found. Does *not* rely on fmt being 's'.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1902 // Used by formats like %6f to limit to 6.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1903
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1904 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1905 textscan::scan_string (delimited_stream& is, const textscan_format_elt& fmt,
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1906 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1907 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1908 if (delim_list.numel () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1909 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1910 unsigned int i = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1911 unsigned int width = fmt.width;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1912
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1913 for (i = 0; i < width; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1914 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1915 if (i+1 > val.length ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1916 val = val + val + ' '; // grow even if empty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1917 int ch = is.get ();
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1918 if (is_delim (ch) || ch == std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1919 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1920 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1921 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1922 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1923 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1924 val[i] = ch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1925 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1926 val = val.substr (0, i); // trim pre-allocation
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1927 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1928 else // Cell array of multi-character delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1929 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1930 std::string ends ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1931 for (int i = 0; i < delim_list.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1932 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1933 std::string tmp = delim_list(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1934 ends += tmp.substr (tmp.length () - 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1935 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1936 val = textscan::read_until (is, delim_list, ends);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1937 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1938 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1939
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1940 // Return in VAL the run of characters from IS contained in PATTERN.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1941
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1942 int
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1943 textscan::scan_bracket (delimited_stream& is, const std::string& pattern,
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1944 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1945 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1946 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1947 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1948
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1949 while (is && pattern.find (c1 = is.get_undelim ()) != std::string::npos)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1950 obuf << static_cast<char> (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1951
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1952 val = obuf.str ();
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1953 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1954 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1955 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1956 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1957
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1958 // Return in VAL a string, either delimited by whitespace/delimiters, or
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1959 // enclosed in a pair of double quotes ("..."). Enclosing quotes are
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1960 // removed. A consecutive pair "" is inserted into VAL as a single ".
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1961
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1962 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1963 textscan::scan_qstring (delimited_stream& is, const textscan_format_elt& fmt,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1964 std::string& val)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1965 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1966 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1967
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1968 if (is.peek () != '\"')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1969 scan_string (is, fmt, val);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1970 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1971 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1972 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1973 scan_caret (is, "\"", val); // read everything until "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1974 is.get (); // swallow "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1975
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1976 while (is && is.peek () == '\"') // if double ", insert one in stream,
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1977 { // and keep looking for single "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1978 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1979 std::string val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1980 scan_caret (is, "\"", val1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1981 val = val + "\"" + val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1982 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1983 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1984 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1985 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1986
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1987 // Read from IS into VAL a string of the next fmt.width characters,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1988 // including any whitespace or delimiters.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1989
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1990 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1991 textscan::scan_cstring (delimited_stream& is, const textscan_format_elt& fmt,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1992 std::string& val) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1993 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1994 val.resize (fmt.width);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1995
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1996 for (unsigned int i = 0; is && i < fmt.width; i++)
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1997 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1998 int ch = is.get_undelim ();
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1999 if (ch != std::istream::traits_type::eof ())
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2000 val[i] = ch;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2001 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2002 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2003 val.resize (i);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2004 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2005 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2006 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2007 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2008
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2009
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2010 // Read a single '%...' conversion and place it in position ROW of OV.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2011
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2012 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2013 textscan::scan_one (delimited_stream& is, const textscan_format_elt& fmt,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2014 octave_value& ov, Array<octave_idx_type> row)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2015 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2016 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2017
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2018 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2019
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2020 octave_value val;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2021 if (fmt.numeric)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2022 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2023 if (fmt.type == 'f' || fmt.type == 'n')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2024 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2025 Complex v;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2026 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2027 scan_complex (is, fmt, v);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2028
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2029 if (! fmt.discard && ! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2030 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2031 if (fmt.bitwidth == 64)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2032 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2033 if (ov.is_real_type () && v.imag () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2034 ov.internal_rep ()->fast_elem_insert (row(0), v.real ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2035 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2036 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2037 if (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2038 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2039 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2040 ov.internal_rep ()->fast_elem_insert (row(0), v);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2041 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2042 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2043 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2044 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2045 if (ov.is_real_type () && v.imag () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2046 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2047 float (v.real ()));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2048 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2049 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2050 if (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2051 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2052 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2053 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2054 FloatComplex (v));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2055 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2056 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2057 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2058 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2059 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2060 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2061 double v; // Matlab docs say 1e30 etc should be valid for %d and
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2062 // 1000 as a %d8 should be 127, so read as double.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2063 // Some loss of precision for d64 and u64.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2064 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2065 v = read_double (is, fmt);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2066 if (! fmt.discard && ! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2067 switch (fmt.bitwidth)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2068 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2069 case 64:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2070 switch (fmt.type)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2071 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2072 case 'd':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2073 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2074 octave_int64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2075 ov.internal_rep ()->fast_elem_insert (row(0), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2076 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2077 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2078
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2079 case 'u':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2080 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2081 octave_uint64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2082 ov.internal_rep ()->fast_elem_insert (row(0), vv);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2083 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2084 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2085 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2086 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2087
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2088 case 32:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2089 switch (fmt.type)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2090 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2091 case 'd':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2092 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2093 octave_int32 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2094 ov.internal_rep ()->fast_elem_insert (row(0), vv);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2095 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2096 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2097
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2098 case 'u':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2099 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2100 octave_uint32 vv = v;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2101 ov.internal_rep ()->fast_elem_insert (row(0), vv);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2102 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2103 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2104 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2105 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2106
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2107 case 16:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2108 if (fmt.type == 'd')
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2109 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2110 octave_int16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2111 ov.internal_rep ()->fast_elem_insert (row(0), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2112 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2113 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2114 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2115 octave_uint16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2116 ov.internal_rep ()->fast_elem_insert (row(0), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2117 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2118 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2119
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2120 case 8:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2121 if (fmt.type == 'd')
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2122 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2123 octave_int8 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2124 ov.internal_rep ()->fast_elem_insert (row(0), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2125 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2126 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2127 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2128 octave_uint8 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2129 ov.internal_rep ()->fast_elem_insert (row(0), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2130 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2131 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2132 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2133 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2134
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2135 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2136 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2137 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2138 ov = do_cat_op (ov, empty_value, row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2139
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2140 // If we are continuing after errors, skip over this field
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2141 if (return_on_error == 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2142 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2143 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2144 is.clear (); // clear to allow read pointer to advance
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2145
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2146 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2147 scan_string (is, fmt, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2148
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2149 is.setstate (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2150 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2151 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2152
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2153 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2154 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2155 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2156 std::string vv (" "); // initial buffer. Grows as needed
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2157 switch (fmt.type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2158 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2159 case 's':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2160 scan_string (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2161 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2162
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2163 case 'q':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2164 scan_qstring (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2165 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2166
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2167 case 'c':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2168 scan_cstring (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2169 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2170
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2171 case '[':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2172 scan_bracket (is, fmt.char_class.c_str (), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2173 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2174
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2175 case '^':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2176 scan_caret (is, fmt.char_class.c_str (), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2177 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2178 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2179
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2180 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2181 ov.internal_rep ()->fast_elem_insert (row (0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2182 Cell (octave_value (vv)));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2183
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2184 // FIXME -- why does failbit get set at EOF, instead of eofbit?
21497
2d71bb0011a0 textscan: Use empty () rather than 'length () == 0' on strings
Mike Miller <mtmiller@octave.org>
parents: 21496
diff changeset
2185 if (! vv.empty ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2186 is.clear (is.rdstate () & ~std::ios_base::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2187 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2188
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2189 is.field_done ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2190 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2191
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2192 // Read data corresponding to the entire format string once, placing the
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2193 // values in row ROW of retval.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2194
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2195 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2196 textscan::read_format_once (delimited_stream& is,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2197 textscan_format_list& fmt_list,
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
2198 std::list<octave_value>& retval,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2199 Array<octave_idx_type> row, int& done_after)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2200 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2201 const textscan_format_elt *elem = fmt_list.first ();
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2202 std::list<octave_value>::iterator out = retval.begin ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2203 bool no_conversions = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2204 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2205 bool conversion_failed = false; // Record for ReturnOnError
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2206
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2207 octave_quit ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2208
21486
4581d0d52948 avoid signed/unsigned comparison warnings introduced in previous change
John W. Eaton <jwe@octave.org>
parents: 21485
diff changeset
2209 for (size_t i = 0; i < fmt_list.numel (); i++)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2210 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2211 bool this_conversion_failed = false;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2212
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2213 // Clear fail of previous numeric conversions.
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2214 is.clear ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2215
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2216 switch (elem->type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2217 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2218 case 'C':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2219 case 'D':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2220 std::cerr << "textscan: Conversion %" << elem->type
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2221 << " not yet implemented\n";
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2222 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2223
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2224 case 'u':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2225 case 'd':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2226 case 'f':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2227 case 'n':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2228 case 's':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2229 case '[':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2230 case '^':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2231 case 'q':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2232 case 'c':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2233 scan_one (is, *elem, *out, row);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2234 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2235
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2236 case textscan_format_elt::literal_conversion :
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2237 match_literal (is, *elem);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2238 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2239
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2240 default:
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2241 error ("Unknown format element '%c'", elem->type);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2242 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2243
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2244 if (! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2245 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2246 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2247 no_conversions = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2248 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2249 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2250 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2251 if (return_on_error < 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2252 this_conversion_failed = true;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2253
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2254 is.clear (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2255 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2256
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2257 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2258 out++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2259
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2260 elem = fmt_list.next ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2261 char *pos = is.tellg ();
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2262
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2263 // FIXME -- these conversions "ignore delimiters". Should they include
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2264 // delimiters at the start of the conversion, or can those be skipped?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2265 if (elem->type != textscan_format_elt::literal_conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2266 // && elem->type != '[' && elem->type != '^' && elem->type != 'c'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2267 )
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2268 skip_delim (is);
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2269
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2270 if (this_conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2271 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2272 if (is.tellg () == pos && ! conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2273 { // done_after = first failure
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2274 done_after = i; // note fail, but parse others to get empty_val
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2275 conversion_failed = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2276 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2277 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2278 this_conversion_failed = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2279 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2280
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2281 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2282 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2283 if (! done)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2284 done_after = i+1;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2285
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2286 // note EOF, but process others to get empty_val.
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2287 done = true;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2288 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2289 }
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2290
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2291 if (done)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2292 is.setstate (std::ios::eofbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2293
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2294 // Returning -3 means "error, and no columns read this row".
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2295 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2296 return (2 + no_conversions);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2297
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2298 if (conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2299 return (4 + no_conversions);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2300
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2301 return 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2302 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2303
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2304 void
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2305 textscan::parse_options (const octave_value_list& args,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2306 textscan_format_list& fmt_list)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2307 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2308 int last = args.length ();
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2309 int n = last;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2310
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2311 if (n & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2312 error ("textscan: %d parameters given, but only %d values", n-n/2, n/2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2313
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2314 delim_len = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2315 bool have_delims = false;
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2316 for (int i = 0; i < last; i += 2)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2317 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2318 if (! args(i).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2319 error ("textscan: Invalid paramter type <%s> for parameter %d",
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2320 args(i).type_name ().c_str (), i/2 + 1);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2321
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2322 std::string param = args(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2323 std::transform (param.begin (), param.end (),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2324 param.begin (), ::tolower);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2325 if (param == "delimiter")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2326 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2327 bool invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2328 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2329 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2330 invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2331 have_delims = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2332 delims = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2333 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2334 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2335 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2336 invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2337 delim_list = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2338 delim_table = " "; // non-empty, to flag non-default delim
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2339
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2340 // Check that all elements are strings, and find max length
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2341 for (int j = 0; j < delim_list.numel (); j++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2342 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2343 if (! delim_list(j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2344 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2345 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2346 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2347 octave_idx_type len = delim_list(j).string_value ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2348 .length ();
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
2349 delim_len = std::max (static_cast<int> (len), delim_len);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2350 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2351 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2352 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2353 if (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2354 error ("textscan: Delimiters must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2355 "cell array of strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2356 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2357 else if (param == "commentstyle")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2358 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2359 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2360 { // check here for names like "C++", "C", "shell", ...?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2361 comment_style = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2362 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2363 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2364 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2365 comment_style = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2366 int len = comment_style.numel ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2367 if ((len >= 1 && ! comment_style (0).is_string ())
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2368 || (len >= 2 && ! comment_style (1).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2369 || (len >= 3))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2370 error ("textscan: CommentStyle must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2371 "cell array of one or two strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2372 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2373 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2374 error ("textscan: CommentStyle must be either a string"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2375 " or cell array of one or two strings, not <%s>",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2376 args(i+1).class_name ().c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2377
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2378 // How far ahead do we need to look to detect an open comment
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2379 // and which character do we look for?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2380 if (comment_style.numel () >= 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2381 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2382 comment_len = comment_style (0).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2383 comment_char = comment_style (0).string_value ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2384 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2385 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2386 else if (param == "treatasempty")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2387 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2388 bool invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2389 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2390 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2391 treat_as_empty = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2392 treat_as_empty_len = args(i+1).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2393 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2394 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2395 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2396 treat_as_empty = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2397 for (int j = 0; j < treat_as_empty.numel (); j++)
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2398 if (! treat_as_empty (j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2399 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2400 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2401 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2402 int k = treat_as_empty (j).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2403 if (k > treat_as_empty_len)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2404 treat_as_empty_len = k;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2405 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2406 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2407 if (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2408 error ("textscan: TreatAsEmpty must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2409 "cell array of one or two strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2410
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2411 // FIXME Ensure none is a prefix of a later one. Sort by length?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2412 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2413 else if (param == "collectoutput")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2414 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2415 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2416 collect_output = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2417 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2418 error ("textscan: CollectOutput must be logical or numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2419 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2420 else if (param == "emptyvalue")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2421 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2422 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2423 empty_value = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2424 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2425 error ("textscan: EmptyValue must be numeric, not <%s>",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2426 args(i+1).class_name ().c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2427 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2428 else if (param == "headerlines")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2429 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2430 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2431 header_lines = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2432 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2433 error ("textscan: HeaderLines must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2434 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2435 else if (param == "bufsize")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2436 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2437 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2438 buffer_size = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2439 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2440 error ("textscan: BufSize must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2441 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2442 else if (param == "multipledelimsasone")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2443 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2444 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2445 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2446 if (args(i+1).bool_value ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2447 multiple_delims_as_one = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2448 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2449 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2450 error ("textscan: MultipleDimsAsOne must be logical or numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2451 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2452 else if (param == "returnonerror")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2453 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2454 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2455 return_on_error = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2456 else if (args(i+1).is_string ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2457 && args(i+1).string_value () == "continue")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2458 return_on_error = 2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2459 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2460 error ("textscan: ReturnOnError must be logical or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2461 "numeric, or 'continue'");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2462 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2463 else if (param == "whitespace")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2464 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2465 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2466 whitespace = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2467 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2468 error ("textscan: Whitespace must be a character string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2469 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2470 else if (param == "expchars")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2471 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2472 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2473 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2474 exp_chars = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2475 default_exp = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2476 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2477 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2478 error ("textscan: ExpChars must be a character string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2479 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2480 else if (param == "endofline")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2481 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2482 bool valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2483 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2484 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2485 std::string s = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2486 if (args(i+1).is_sq_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2487 s = do_string_escapes (s);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2488 int l = s.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2489 if (l == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2490 eol1 = eol2 = -2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2491 else if (l == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2492 eol1 = eol2 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2493 else if (l == 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2494 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2495 eol1 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2496 eol2 = s.c_str ()[1];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2497 if (eol1 != '\r' || eol2 != '\n') // Why limit it?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2498 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2499 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2500 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2501 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2502 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2503 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2504 valid = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2505 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2506 error ("textscan: EndOfLine must be at most one character "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2507 "or '\\r\\n'");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2508 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2509 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2510 error ("textscan: Unrecognised option '%s'", param.c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2511 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2512
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2513 whitespace_table = std::string (256, '\0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2514 for (unsigned int i = 0; i < whitespace.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2515 whitespace_table[whitespace[i]] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2516
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2517 // For Matlab compatibility, add 0x20 to whitespace, unless
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2518 // whitespace is explicitly ignored.
21497
2d71bb0011a0 textscan: Use empty () rather than 'length () == 0' on strings
Mike Miller <mtmiller@octave.org>
parents: 21496
diff changeset
2519 if (! (whitespace.empty () && fmt_list.has_string))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2520 whitespace_table[' '] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2521
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2522 // Create look-up table of delimiters, based on 'delimiter'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2523 delim_table = std::string (256, '\0');
21463
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2524 if (eol1 >= 0 && eol1 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2525 delim_table[eol1] = '1'; // EOL is always a delimiter
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2526 if (eol2 >= 0 && eol2 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2527 delim_table[eol2] = '1'; // EOL is always a delimiter
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2528 if (! have_delims)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2529 for (unsigned int i = 0; i < 256; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2530 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2531 if (isspace (i))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2532 delim_table[i] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2533 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2534 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2535 for (unsigned int i = 0; i < delims.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2536 delim_table[delims[i]] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2537 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2538
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2539 // Skip comments, and characters specified by the "Whitespace" option.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2540 // If EOLstop == true, don't skip end of line.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2541
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2542 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2543 textscan::skip_whitespace (delimited_stream& is, bool EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2544 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2545 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2546 bool found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2547
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2548 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2549 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2550 found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2551 int prev = -1;
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2552 while (is && (c1 = is.get_undelim ()) != std::istream::traits_type::eof ()
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2553 && ( ( (c1 == eol1 || c1 == eol2) && ++lines && ! EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2554 || isspace (c1)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2555 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2556 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2557 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2558 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2559 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2560
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2561 if (c1 == comment_char) // see if we match an open comment
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2562 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2563 // save stream state in case we have to restore it
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2564 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2565 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2566
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2567 std::string tmp (comment_len, '\0');
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2568 char *look = is.read (&tmp[0], comment_len-1, pos); // already read first char
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2569 if (is && ! strncmp (comment_style(0).string_value ().substr (1)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2570 .c_str (), look, comment_len-1))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2571 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2572 found_comment = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2573
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2574 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2575 if (comment_style.numel () == 1) // skip to end of line
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2576 {
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2577 std::string eol (3, '\0');
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2578 eol[0] = eol1;
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2579 eol[1] = eol2;
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2580
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2581 scan_caret (is, eol, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2582 c1 = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2583 if (c1 == eol1 && eol1 != eol2 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2584 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2585 lines++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2586 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2587 else // matching pair
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2588 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2589 std::string end_c = comment_style(1).string_value ();
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2590 // last char of end-comment sequence
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2591 std::string last = end_c.substr (end_c.size () - 1);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2592 std::string may_match ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2593 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2594 { // find sequence ending with last char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2595 scan_caret (is, last, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2596 is.get_undelim (); // (read last itself)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2597
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2598 may_match = may_match + dummy + last;
21498
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
2599 if (may_match.length () > end_c.length ())
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
2600 {
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
2601 size_t start = may_match.length () - end_c.length ();
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
2602 may_match = may_match.substr (start);
9cb37fede0b6 textscan: Compare pointers or sizes rather than their arithmetic difference
Mike Miller <mtmiller@octave.org>
parents: 21497
diff changeset
2603 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2604 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2605 while (may_match != end_c && is && ! is.eof ());
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2606 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2607 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2608 else // wasn't really a comment; restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2609 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2610 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2611 is.seekg (pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2612 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2613 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2614 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2615 while (found_comment);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2616
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2617 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2618 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2619 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2620 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2621
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2622 // See if the next few characters match one of the strings in target.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2623 // For efficiency, MAX_LEN is the cached longest length of any target.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2624 // Return -1 if none is found, or the index of the match.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2625
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2626 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2627 textscan::lookahead (delimited_stream& is, const Cell& targets, int max_len,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2628 bool case_sensitive) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2629 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2630 // target strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2631 // Read ahead longest, put it all back, then re-read the string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2632 // that matches.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2633
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2634 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2635
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2636 std::string tmp (max_len, '\0');
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2637 char *look = is.read (&tmp[0], tmp.size (), pos);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2638
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2639 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2640 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2641 // FIXME pos may be corrupted by is.read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2642
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2643 int i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2644 int (*compare)(const char *, const char *, size_t);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2645 compare = case_sensitive ? strncmp : strncasecmp;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2646
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2647 for (i = 0; i < targets.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2648 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2649 std::string s = targets (i).string_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2650 if (! (*compare) (s.c_str (), look, s.size ()))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2651 {
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2652 is.read (&tmp[0], s.size (), pos); // read just the right amount
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2653 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2654 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2655 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2656
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2657 if (i == targets.numel ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2658 i = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2659
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2660 return i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2661 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2662
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2663 // Skip delimiters -- multiple if MultipleDelimsAsOne specified.
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2664 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2665 textscan::skip_delim (delimited_stream& is)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2666 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2667 int c1 = skip_whitespace (is, true); // 'true': stop once EOL is read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2668 if (delim_list.numel () == 0) // single character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2669 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2670 if (is_delim (c1) || c1 == eol1 || c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2671 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2672 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2673 if (c1 == eol1 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2674 is.get_undelim (); // if \r\n, skip the \n too.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2675
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2676 if (multiple_delims_as_one)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2677 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2678 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2679 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2680 // Increment lines for each end-of-line seen; for \r\n, decrement
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2681 while (is && ((c1 = is.get_undelim ())
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2682 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2683 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2684 || isspace (c1) || is_delim (c1)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2685 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2686 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2687 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2688 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2689 }
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2690 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2691 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2692 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2693 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2694 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2695 else // multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2696 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2697 int first_match;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2698
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2699 if (c1 == eol1 || c1 == eol2
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2700 || (-1 != (first_match = lookahead (is, delim_list, delim_len))))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2701 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2702 if (c1 == eol1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2703 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2704 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2705 if (is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2706 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2707 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2708 else if (c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2709 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2710 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2711 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2712
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2713 if (multiple_delims_as_one)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2714 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2715 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2716 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2717 // Increment lines for each end-of-line seen; for \r\n, decrement
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2718 while (is && ((c1 = skip_whitespace (is, true))
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2719 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2720 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2721 || -1 != lookahead (is, delim_list, delim_len)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2722 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2723 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2724 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2725 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2726 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2727 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2728 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2729 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2730
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2731 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2732 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2733
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2734 // Read in as much of the input as coincides with the literal in the
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2735 // format string. Return "true" if the entire literal is matched, else
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2736 // false (and set failbit).
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2737
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2738 bool
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2739 textscan::match_literal (delimited_stream& is, const textscan_format_elt& fmt)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2740 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2741 // "false" -> treat EOL as normal space
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2742 // since a delimiter at the start of a line is a mismatch, not empty field
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2743 skip_whitespace (is, false);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2744
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2745 for (unsigned int i = 0; i < fmt.width; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2746 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2747 int ch = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2748 if (ch != fmt.text[i])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2749 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2750 if (ch != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2751 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2752 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2753 return false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2754 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2755 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2756 return true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2757 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2758
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2759 DEFUN (textscan, args, ,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2760 "-*- texinfo -*-\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2761 @deftypefn {} {@var{C} =} textscan (@var{fid}, @var{format})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2762 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2763 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{param}, @var{value}, @dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2764 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat}, @var{param}, @var{value}, @dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2765 @deftypefnx {} {@var{C} =} textscan (@var{str}, @dots{})\n\
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
2766 @deftypefnx {} {[@var{C}, @var{position}, @var{errmsg}] =} textscan (@dots{})\n\
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2767 Read data from a text file or string.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2768 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2769 The string @var{str} or file associated with @var{fid} is read from and\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2770 parsed according to @var{format}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2771 The function is an extension of @code{strread} and @code{textread}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2772 Differences include: the ability to read from either a file or a string,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2773 additional options, and additional format specifiers.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2774 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2775 The input is interpreted as a sequence of \"words\", delimiters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2776 (such as whitespace) and literals.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2777 The characters that form delimiters and whitespace are determined\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2778 by the options.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2779 The format consists of format specifiers interspersed between literals.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2780 In the format, whitespace forms a delimiter between consecutive literals,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2781 but is otherwise ignored.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2782 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2783 The output @var{C} is a cell array whose second dimension is determined\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2784 by the number of format specifiers.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2785 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2786 The first word of the input is matched to the first specifier of the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2787 format and placed in the first column of the output;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2788 the second is matched to the second specifier and placed in the second column\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2789 and so forth.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2790 If there are more words than specifiers, the process is repeated until all\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2791 words have been processed or the limit imposed by @var{repeat} has been met\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2792 (see below).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2793 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2794 The string @var{format} describes how the words in @var{str} should be\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2795 parsed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2796 As in @var{fscanf}, any (non-whitespace) text in the format that is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2797 not one of these specifiers is considered a literal;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2798 if there is a literal between two format specifiers then that same literal\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2799 must appear in the input stream between the matching words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2800 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2801 The following specifiers are valid:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2802 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2803 @table @code\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2804 @item %f\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2805 @itemx %f64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2806 @itemx %n\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2807 The word is parsed as a number and converted to double.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2808 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2809 @item %f32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2810 The word is parsed as a number and converted to single (float).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2811 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2812 @item %d\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2813 @itemx %d8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2814 @itemx %d16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2815 @itemx %d32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2816 @itemx %d64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2817 The word is parsed as a number and converted to int8, int16, int32 or int64.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2818 If not size is specified, int32 is used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2819 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2820 @item %u\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2821 @itemx %u8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2822 @itemx %u16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2823 @itemx %u32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2824 @itemx %u64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2825 The word is parsed as a number and converted to uint8, uint16, uint32 or\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2826 uint64. If not size is specified, uint32 is used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2827 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2828 @item %s\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2829 The word is parsed as a string, ending at the last character before\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2830 whitespace, an end-of-line or a delimiter specified in the options.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2831 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2832 @item %q\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2833 The word is parsed as a \"quoted string\".\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2834 If the first character of the string is a double quote (\") then the string\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2835 includes everything until a matching double quote, including whitespace,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2836 delimiters and end of line characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2837 If a pair of consecutive double quotes appears in the input,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2838 it is replaced in the output by a single double quote.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2839 That is, the input \"He said \"\"Hello\"\"\" would return the value\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2840 'He said \"Hello\"'.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2841 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2842 @item %c\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2843 The next character of the input is read.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2844 This includes delimiters, whitespace and end of line characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2845 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2846 @item %[...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2847 @itemx %[^...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2848 In the first form, the word consists of the longest run consisting of only\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2849 characters between the brackets.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2850 Ranges of characters can be specified by a hyphen;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2851 for example, %[0-9a-zA-Z] matches all alphanumeric characters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2852 (if the underlying character set is ASCII).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2853 Since Matlab treats hyphens literally, this expansion only applies to\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2854 alphanumeric characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2855 To include '-' in the set, it should appear first or last in the brackets;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2856 to include ']', it should be the first character.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2857 If the first character is '^' then the word consists of characters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2858 NOT listed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2859 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2860 @item %N...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2861 For %s, %c %d, %f, %n, %u, an optional width can be specified as %Ns etc.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2862 where N is an integer > 1.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2863 For %c, this causes exactly the next N characters to be read instead of\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2864 a single character.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2865 For the other specifiers, it is an upper bound on the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2866 number of characters read;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2867 normal delimiters can cause fewer characters to be read.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2868 For complex numbers, this limit applies to the real and imaginary\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2869 components individually.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2870 For %f and %n, format specifiers like %N.Mf are allowed, where M is an upper\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2871 bound on number of characters after the decimal point to be considered;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2872 subsequent digits are skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2873 For example, the specifier %8.2f would read 12.345e6 as 1.234e7.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2874 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2875 @item %*...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2876 The word specified by the remainder of the conversion specifier is skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2877 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2878 @item literals\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2879 In addition the format may contain literal character strings;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2880 these will be skipped during reading.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2881 If the input string does not match this literal, the processing terminates,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2882 unless \"ReturnOnError\" is set to \"continue\".\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2883 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2884 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2885 Parsed words corresponding to the first specifier are returned in the first\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2886 output argument and likewise for the rest of the specifiers.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2887 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2888 By default, if there is only one input argument, @var{format} is @t{\"%f\"}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2889 This means that numbers are read from @var{str} into a single column vector.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2890 If @var{format} is explicitly empty, \"\", then textscan will return data\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2891 in a number of columns matching the number of fields on the first data\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2892 line of the input.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2893 Either of these is suitable only if @var{str} contains only numeric fields.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2894 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2895 For example, the string\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2896 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2897 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2898 @group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2899 @var{str} = \"\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2900 Bunny Bugs 5.5\\n\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2901 Duck Daffy -7.5e-5\\n\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2902 Penguin Tux 6\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2903 @end group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2904 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2905 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2906 @noindent\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2907 can be read using\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2908 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2909 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2910 @var{a} = textscan (@var{str}, \"%s %s %f\");\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2911 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2912 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2913 The optional numeric argument @var{repeat} can be used for limiting the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2914 number of items read:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2915 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2916 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2917 @item -1\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2918 (default) read all of the string or file until the end.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2919 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2920 @item N\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2921 Read until the first of two conditions occurs: the format has been processed\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2922 N times, or N lines of the input have been processed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2923 Zero (0) is an acceptable value for @var{repeat}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2924 Currently, end-of-line characters inside %q, %c, and %[...]$ conversions\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2925 do not contribute to the line count.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2926 This is incompatible with Matlab and may change in future.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2927 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2928 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2929 The behavior of @code{textscan} can be changed via property-value pairs.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2930 The following properties are recognized:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2931 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2932 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2933 @item @qcode{\"BufSize\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2934 This specifies the number of bytes to use for the internal buffer.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2935 A modest speed improvement is obtained by setting this to a large value\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2936 when reading a large file, especially the input contains long strings.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2937 The default is 4096, or a value dependent on @var{n} is that is specified.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2938 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2939 @item @qcode{\"CollectOutput\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2940 A value of 1 or true instructs textscan to concatenate consecutive columns\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2941 of the same class in the output cell array.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2942 A value of 0 or false (default) leaves output in distinct columns.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2943 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2944 @item @qcode{\"CommentStyle\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2945 Parts of @var{str} are considered comments and will be skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2946 @var{value} is the comment style and can be either\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2947 (1) One string, or 1x1 cell string, to skip everything to the right of it;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2948 (2) A cell array of two strings, to skip everything between the first and\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2949 second strings.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2950 Comments are only parsed where whitespace is accepted, and do not act as\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2951 delimiters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2952 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2953 @item @qcode{\"Delimiter\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2954 If @var{value} is a string, any character in @var{value} will be used to\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2955 split @var{str} into words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2956 If @var{value} is a cell array of strings,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2957 any string in the array will be used to split @var{str} into words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2958 (default value = any whitespace.)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2959 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2960 @item @qcode{\"EmptyValue\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2961 Value to return for empty numeric values in non-whitespace delimited data.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2962 The default is NaN@.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2963 When the data type does not support NaN (int32 for example),\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2964 then default is zero.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2965 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2966 @item @qcode{\"EndOfLine\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2967 @var{value} can be either a emtpy or one character specifying the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2968 end of line character, or the pair\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2969 @qcode{\"@xbackslashchar{}r@xbackslashchar{}n\"} (CRLF).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2970 In the latter case, any of\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2971 @qcode{\"@xbackslashchar{}r\"}, @qcode{\"@xbackslashchar{}n\"} or\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2972 @qcode{\"@xbackslashchar{}r@xbackslashchar{}n\"} is counted as a (single)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2973 newline.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2974 If no value is given, @qcode{\"@xbackslashchar{}r@xbackslashchar{}n\"} is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2975 used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2976 @c If set to \"\" (empty string) EOLs are ignored as delimiters and added\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2977 @c to whitespace.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2978 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2979 @c When reading from a character string, optional input argument @var{n}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2980 @c specifies the number of times @var{format} should be used (i.e., to limit\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2981 @c the amount of data read).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2982 @c When reading from file, @var{n} specifies the number of data lines to read;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2983 @c in this sense it differs slightly from the format repeat count in strread.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2984 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2985 @item @qcode{\"HeaderLines\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2986 The first @var{value} number of lines of @var{fid} are skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2987 Note that this does not refer to the first non-comment lines, but the first\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2988 lines of any type.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2989 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2990 @item @qcode{\"MultipleDelimsAsOne\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2991 If @var{value} is non-zero,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2992 treat a series of consecutive delimiters, without whitespace in between,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2993 as a single delimiter.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2994 Consecutive delimiter series need not be vertically @qcode{\"aligned\"}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2995 Without this option, a single delimiter before the end of the line does\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2996 not cause the line to be considered to end with an empty value,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2997 but a single delimiter at the start of a line causes the line\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2998 to be considered to start with an empty value.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2999 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3000 @item @qcode{\"TreatAsEmpty\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3001 Treat single occurrences (surrounded by delimiters or whitespace) of the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3002 string(s) in @var{value} as missing values.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3003 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3004 @item @qcode{\"ReturnOnError\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3005 If set to numerical 1 or true, return normally as soon as an error\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3006 is encountered, such as trying to read a string using @qcode{%f}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3007 If set to 0 or false, return an error and no data.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3008 If set to \"continue\" (default), textscan attempts to continue reading\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3009 beyond the location; however, this may cause the parsing to get out of sync.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3010 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3011 @item @qcode{\"Whitespace\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3012 Any character in @var{value} will be interpreted as whitespace and trimmed;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3013 The default value for whitespace is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3014 @c Note: the next line specifically has a newline which generates a space\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3015 @c in the output of qcode, but keeps the next line < 80 characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3016 @qcode{\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3017 @xbackslashchar{}b@xbackslashchar{}r@xbackslashchar{}n@xbackslashchar{}t\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3018 (note the space). Unless whitespace is set to @qcode{\"\"} (empty) AND at\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3019 least one @qcode{\"%s\"} format conversion specifier is supplied, a space is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3020 always part of whitespace.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3021 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3022 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3023 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3024 When the number of words in @var{str} or @var{fid} doesn't match an exact\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3025 multiple of the number of format conversion specifiers,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3026 textscan's behavior depends on\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3027 whether the last character of the string or file is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3028 an end-of-line as specified by the EndOfLine option:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3029 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3030 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3031 @item last character = end-of-line\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3032 Data columns are padded with empty fields, NaN or 0 (for integer fields)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3033 so that all columns have equal length\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3034 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3035 @item last character is not end-of-line\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3036 Data columns are not padded; textscan returns columns of unequal length\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3037 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3038 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3039 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3040 The second output, @var{position}, provides the position, in characters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3041 from the beginning of the file or string, at which the processing stopped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3042 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3043 @seealso{dlmread, fscanf, load, strread, textread}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3044 @end deftypefn")
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3045 {
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3046 static std::string who = "textscan";
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3047
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3048 octave_value_list retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3049
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3050 if (args.length () < 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3051 print_usage ();
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3052
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3053 octave_value_list tmp_args = args.splice (0, 1);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3054
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3055 octave_idx_type count = 0;
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3056
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3057 textscan tscanner;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3058
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3059 if (args(0).is_string ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3060 {
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3061 std::string data = args(0).string_value ();
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3062
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3063 octave_stream os = octave_istrstream::create (data);
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3064
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3065 if (! os.is_valid ())
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3066 error ("%s: unable to create temporary input buffer", who.c_str ());
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3067
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3068 std::istream *isp = os.input_stream ();
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3069
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3070 octave_value result = tscanner.scan (*isp, tmp_args, count);
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3071
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3072 std::string errmsg = os.error ();
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3073
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3074 return ovl (result, count, errmsg);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3075 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3076 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3077 {
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3078 octave_stream os = octave_stream_list::lookup (args(0), who);
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3079
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3080 std::istream *isp = os.input_stream ();
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3081
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3082 if (! isp)
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3083 error ("internal error: textscan called with invalid istream");
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3084
21500
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3085 octave_value result = tscanner.scan (*isp, tmp_args, count);
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3086
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3087 std::string errmsg = os.error ();
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3088
7a6589e2867a more style fixes for textscan
John W. Eaton <jwe@octave.org>
parents: 21498
diff changeset
3089 return ovl (result, count, errmsg);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3090 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3091
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3092 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3093 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3094
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3095 /*
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3096 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3097 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3098 %! fmtstr = "%f %d %f %s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3099 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3100 %! assert (isequal (c{1}, [1;5]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3101 %! assert (length (c{1}), 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3102 %! assert (iscellstr (c{4}));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3103 %! assert (isequal (c{3}, [3; -Inf]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3104
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3105 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3106 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3107 %! b = [b; 8*b/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3108 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3109 %! fmt = "%f miles/hr = %f kilometers/hr";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3110 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3111 %! assert (c{1}, b(1,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3112 %! assert (c{2}, b(2,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3113
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3114 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3115 %! str = "13, -, NA, str1, -25\r\n// Middle line\r\n36, na, 05, str3, 6";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3116 %! a = textscan (str, "%d %n %f %s %n", "delimiter", ",",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3117 %! "treatAsEmpty", {"NA", "na", "-"},"commentStyle", "//");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3118 %! assert (a{1}, int32 ([13; 36]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3119 %! assert (a{2}, [NaN; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3120 %! assert (a{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3121 %! assert (a{4}, {"str1"; "str3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3122 %! assert (a{5}, [-25; 6]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3123
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3124 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3125 %! str = "Km:10 = hhhBjjj miles16hour\r\n";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3126 %! str = [str "Km:15 = hhhJjjj miles241hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3127 %! str = [str "Km:2 = hhhRjjj miles3hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3128 %! str = [str "Km:25 = hhhZ\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3129 %! fmt = "Km:%d = hhh%1sjjj miles%dhour";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3130 %! a = textscan (str, fmt, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3131 %! assert (a{1}', int32 ([10 15 2 25]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3132 %! assert (a{2}', {'B' 'J' 'R' 'Z'});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3133 %! assert (a{3}', int32 ([16 241 3 0]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3134
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3135 ## Test with default endofline parameter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3136 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3137 %! c = textscan ("L1\nL2", "%s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3138 %! assert (c{:}, {"L1"; "L2"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3139
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3140 ## Test with endofline parameter set to "" (empty) - newline should be in word
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3141 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3142 %! c = textscan ("L1\nL2", "%s", "endofline", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3143 %! assert (int8 ([c{:}{:}]), int8 ([ 76, 49, 10, 76, 50 ]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3144
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3145 ### Matlab fails this test. A literal after a conversion is not a delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3146 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3147 #%! ## No delimiters at all besides EOL. Skip fields, even empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3148 #%! str = "Text1Text2Text\nTextText4Text\nText57Text";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3149 #%! c = textscan (str, "Text%*dText%dText");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3150 #%! assert (c{1}, int32 ([2; 4; 0]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3151
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3152 ## CollectOutput test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3153 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3154 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3155 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3156 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3157 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3158 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3159 %! assert (size (c{3}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3160 %! assert (size (c{2}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3161
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3162 ## CollectOutput test with uneven column length files
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3163 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3164 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3165 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3166 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3167 %! str = [str "110 miles/hr"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3168 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3169 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3170 %! assert (size (c{1}), [11, 1]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3171 %! assert (size (c{3}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3172 %! assert (size (c{2}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3173 %! assert (c{3}(end), NaN);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3174 %! assert (c{2}{11, 1}, "/hr");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3175 %! assert (isempty (c{2}{11, 2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3176
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3177 ## Double quoted string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3178 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3179 %! str = 'First "the second called ""the middle""" third';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3180 %! fmt = "%q";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3181 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3182 %! assert (c{1}, {"First"; 'the second called "the middle"'; "third"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3183
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3184 ## Arbitrary character
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3185 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3186 %! c = textscan ("a first, \n second, third", "%s %c %11c", 'delimiter', ' ,');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3187 %! assert (c{1}, {"a"; "ond"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3188 %! assert (c{2}, {"f"; "t"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3189 %! assert (c{3}, {"irst, \n sec"; "hird"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3190
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3191 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3192 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3193 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3194 %! c = textscan (str, "%4d %4d", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3195 %! assert (c, {[12 34; 1234 5678; 9 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3196
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3197 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3198 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3199 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3200 %! c = textscan (str, "%4f %f", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3201 %! assert (c, {[12 34; 1234 56789; 7 NaN]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3202
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3203 ## Ignore trailing delimiter, but use leading one
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3204 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3205 %! str = "12.234e+2,34, \n12345.789-9876j,78\n,10|3";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3206 %! c = textscan (str, "%10.2f %f", "delimiter", ",", "collectOutput", 1,
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3207 %! "expChars", "e|");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3208 %! assert (c, {[1223 34; 12345.79-9876j 78; NaN 10000]}, 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3209
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3210 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3211 %! ## Multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3212 %! str = "99end2 space88gap 4564";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3213 %! c = textscan (str, "%d %s", "delimiter", {"end", "gap", "space"});
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3214 %! assert (c{1}, int32 ([99; 88]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3215 %! assert (c{2}, {"2 "; "4564"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3216
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3217 ### Delimiters as part of literals, and following literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3218 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3219 #%! str = "12 R&D & 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3220 #%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1, "EmptyValue", -99);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3221 #%! assert (c, {[12 -99; 7 -99]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3222 #
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3223 ### Delimiters as part of literals, and before literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3224 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3225 #%! str = "12 & R&D 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3226 #%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3227 #%! assert (c, {[12 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3228
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3229 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3230 %! ## Check number of lines read, not number of passes through format string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3231 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3232 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3233 %! fprintf (fid, "1\n2\n3\n4\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3234 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3235 %! c = textscan (fid, "%f %f", 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3236 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3237 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3238 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3239 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3240 %! assert (!E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3241
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3242 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3243 %! ## Check number of lines read, not number of passes through format string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3244 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3245 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3246 %! fprintf (fid, "1\r\n2\r3\n4\r\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3247 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3248 %! c = textscan (fid, "%f %f", 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3249 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3250 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3251 %! assert (c, {[1;3], [2;4]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3252
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3253 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3254 %! ## Check number of lines read, with multiple delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3255 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3256 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3257 %! fprintf (fid, "1-\r\n-2\r3-\n-4\r\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3258 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3259 %! c = textscan (fid, "%f %f", 4, "delimiter", "-", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3260 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3261 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3262 %! assert (c, {[1;3], [2;4]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3263
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3264 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3265 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3266 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3267 %! fid = fopen (f, "w+");
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3268 %! str = "1 2 3\n4 s 6";
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3269 %! fprintf (fid, str);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3270 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3271 %! c = textscan (fid, "%f %f %f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3272 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3273 %! d = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3274 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3275 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3276 %! unlink (f);
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3277 %! u = textscan (str, "%f %f %f");
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3278 %! v = textscan (str, "%f %f %f", "ReturnOnError", 1);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3279 %! assert (c, {[1;4], [2;NaN], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3280 %! assert (d, {[1;4], [2], [3]})
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3281 %! assert (u, {[1;4], [2;NaN], [3;6]})
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3282 %! assert (v, {[1;4], [2], [3]})
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3283
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3284 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3285 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3286 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3287 %! fid = fopen (f, "w+");
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3288 %! str = "1 2 3\n4 s 6\n";
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3289 %! fprintf (fid, str);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3290 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3291 %! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3292 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3293 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3294 %! unlink (f);
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3295 %! u = textscan (str, "%f %f %f", "ReturnOnError", 1);
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3296 %! assert (c, {[1;4], 2, 3})
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3297 %! assert (u, {[1;4], 2, 3})
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3298
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3299 %!error <Read error in field 2 of row 2> textscan ("1 2 3\n4 s 6", "%f %f %f", "ReturnOnError", 0);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3300
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3301 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3302 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3303 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3304 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3305 %! fprintf (fid, "1 s 3\n4 5 6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3306 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3307 %! c = textscan (fid, "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3308 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3309 %! d = textscan (fid, "", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3310 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3311 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3312 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3313 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3314 %! assert (d, {1})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3315
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3316 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3317 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3318 %! c = textscan ("1,,3\n4,5,6", "", "Delimiter", ",", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3319 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3320
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3321 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3322 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3323 %! c = textscan ("1,,3\n4,5,6", "%f %f %f", "Delimiter", ",", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3324 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3325
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3326 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3327 %! ## Check ReturnOnError in first column
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3328 %! c = textscan ("1 2 3\ns 5 6", "", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3329 %! assert (c, {1, 2, 3})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3330
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3331 ## Test input validation
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3332 %!error textscan ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3333 %!error textscan (single (40))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3334 %!error textscan ({40})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3335 %!error <must be a string> textscan ("Hello World", 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3336 #%!error <cannot provide position information> [C, pos] = textscan ("Hello World")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3337 %!error <at most one character or> textscan ("Hello World", '%s', 'EndOfLine', 3)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3338 %!error <'%z' is not a valid format specifier> textscan ("1.0", "%z");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3339 %!error <no valid format conversion specifiers> textscan ("1.0", "foo");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3340
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3341 ## Test incomplete first data line
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3342 %! R = textscan (['Empty1' char(10)], 'Empty%d %f');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3343 %! assert (R{1}, int32 (1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3344 %! assert (isempty (R{2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3345
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3346 ## bug #37023
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3347 %!test
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3348 %! data = textscan (" 1. 1 \n 2 3\n", '%f %f');
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3349 %! assert (data{1}, [1; 2], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3350 %! assert (data{2}, [1; 3], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3351
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3352 ## Whitespace test (bug #37333) using delimiter ";"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3353 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3354 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3355 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3356 %! tc{1, end+1} = "C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3357 %! tc{1, end+1} = " C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3358 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3359 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3360 %! c = textscan (string, "%s", "delimiter", ";");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3361 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3362 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3363 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3364 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3365 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3366 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3367 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3368
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3369 ## Whitespace test (bug #37333), adding multipleDelimsAsOne true arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3370 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3371 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3372 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3373 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3374 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3375 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3376 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3377 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3378 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3379 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3380 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3381 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3382 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3383 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3384 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3385
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3386 ## Whitespace test (bug #37333), adding multipleDelimsAsOne false arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3387 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3388 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3389 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3390 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3391 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3392 %! tc{1, end+1} = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3393 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3394 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3395 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 0);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3396 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3397 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3398 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3399 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3400 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3401 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3402 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3403
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3404 ## Whitespace test (bug #37333) whitespace "" arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3405 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3406 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3407 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3408 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3409 %! tc{1, end+1} = "C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3410 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3411 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3412 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3413 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3414 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3415 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3416 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3417 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3418 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3419
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3420 ## Whitespace test (bug #37333), whitespace " " arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3421 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3422 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3423 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3424 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3425 %! tc{1, end+1} = "C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3426 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3427 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3428 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3429 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3430 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3431 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3432 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3433 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3434 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3435 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3436
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3437 ## Tests reading with empty format, should return proper nr of columns
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3438 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3439 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3440 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3441 %! fprintf (fid, " 1 2 3 4\n5 6 7 8");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3442 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3443 %! A = textscan (fid, "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3444 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3445 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3446 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3447 %! assert (A{1}, [1 ; 5], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3448 %! assert (A{2}, [2 ; 6], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3449 %! assert (A{3}, [3 ; 7], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3450 %! assert (A{4}, [4 ; 8], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3451 %! assert (E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3452
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3453 ## Tests reading with empty format; empty fields & incomplete lower row
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3454 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3455 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3456 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3457 %! fprintf (fid, " ,2,,4\n5,6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3458 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3459 %! A = textscan (fid, "", "delimiter", ",", "EmptyValue", 999, "CollectOutput" , 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3460 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3461 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3462 %! assert (A{1}, [999, 2, 999, 4; 5, 6, 999, 999], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3463
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3464 ## Error message tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3465
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3466 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3467 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3468 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3469 %! msg1 = "textscan: 1 parameters given, but only 0 values";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3470 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3471 %! A = textscan (fid, "", "headerlines");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3472 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3473 %! assert (!feof (fid));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3474 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3475 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3476 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3477
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3478 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3479 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3480 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3481 %! msg1 = "textscan: HeaderLines must be numeric";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3482 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3483 %! A = textscan (fid, "", "headerlines", "hh");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3484 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3485 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3486 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3487 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3488
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3489 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3490 %! ## Skip headerlines
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3491 %! A = textscan ("field 1 field2\n 1 2\n3 4", "", "headerlines", 1, "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3492 %! assert (A, {[1 2; 3 4]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3493
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3494 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3495 %! ## Skip headerlines with non-default EOL
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3496 %! A = textscan ("field 1 field2\r 1 2\r3 4", "", "headerlines", 2, "collectOutput", 1, "EndOfLine", '\r');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3497 %! assert (A, {[3 4]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3498
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3499 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3500 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3501 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3502 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3503 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3504 %! msg1 = "textscan: EndOfLine must be at most one character or '\\r\\n'";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3505 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3506 %! A = textscan (fid, "%f", "EndOfLine", "\n\r");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3507 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3508 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3509 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3510 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3511
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3512 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3513 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3514 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3515 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3516 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3517 %! msg1 = "textscan: EndOfLine must be at most one character or '\\r\\n'";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3518 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3519 %! A = textscan (fid, "%f", "EndOfLine", 33);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3520 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3521 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3522 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3523 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3524
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3525 ## Bug #41824
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3526 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3527 %! assert (textscan ("123", "", "whitespace", " "){:}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3528
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3529 ## Bug #42343-1, just test supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3530 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3531 %! assert (textscan (",NaN", "", "delimiter", "," ,"emptyValue" ,Inf), {Inf, NaN});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3532
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3533 ## Bug #42343-2, test padding with supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3534 %!test
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3535 %! a = textscan (",1,,4\nInf, ,NaN\n", "", "delimiter", ",", "emptyvalue", -10);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3536 %! assert (cell2mat (a), [-10, 1, -10, 4; Inf, -10, NaN, -10]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3537
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3538 ## Bug #42528
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3539 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3540 %! assert (textscan ("1i", ""){1}, 0+1i);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3541 %! assert (cell2mat (textscan ("3, 2-4i, NaN\n -i, 1, 23.4+2.2i\n 1+1 1+1j", "", "delimiter", ",")), [3+0i, 2-4i, NaN+0i; 0-i, 1+0i, 23.4+2.2i; 1 1 1+1i]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3542
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3543 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3544 %! ## TreatAsEmpty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3545 %! C = textscan ("1,2,3,NN,5,6\n", "%d%d%d%f", "delimiter", ",", "TreatAsEmpty", "NN");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3546 %! assert (C{3}(1), int32 (3));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3547 %! assert (C{4}(1), NaN);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3548
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3549 ## MultipleDelimsAsOne
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3550 %!test
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3551 %! str = "11, 12, 13,, 15\n21,, 23, 24, 25\n,, 33, 34, 35\n";
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3552 %! C = textscan (str, "%f %f %f %f", "delimiter", ",", "multipledelimsasone", 1, "endofline", "\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3553 %! assert (C{1}', [11, 21, 33]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3554 %! assert (C{2}', [12, 23, 34]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3555 %! assert (C{3}', [13, 24, 35]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3556 %! assert (C{4}', [15, 25, NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3557
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3558 ## Bug #44750
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3559 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3560 %! assert (textscan ("/home/foo/", "%s", "delimiter", "/", "MultipleDelimsAsOne", 1){1}, ...
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3561 %! {"home"; "foo"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3562
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3563 ### Allow cuddling %sliteral but warn it is ambiguous
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3564 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3565 #%! C = textscan ("abcxyz51\nxyz83\n##xyz101", "%s xyz %d");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3566 #%! assert (C{1}([1 3]), {"abc"; "##"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3567 #%! assert (isempty (C{1}{2}), true);
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3568 #%! assert (C{2}, int32 ([51; 83; 101]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3569 ### Literals are not delimiters.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3570
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3571 ## Test for false positives in check for non-supported format specifiers
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3572 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3573 %! assert (textscan ("Total: 32.5 % (of cm values)", "Total: %f %% (of cm values)"){1}, 32.5, 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3574
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3575 ## Test various forms of string format specifiers (bug #45712)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3576 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3577 %! str = "14 :1 z:2 z:3 z:5 z:11";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3578 %! C = textscan (str, "%f %s %*s %3s %*3s %f", "delimiter", ":");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3579 %! assert (C, {14, {"1 z"}, {"3 z"}, 11});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3580
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3581 %% Bit width, fixed width conv. specifiers
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3582 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3583 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3584 %! str2 = [str2 str2 str2 str2 str2 str2 str2 str2];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3585 %! str2 = [str2 "123456789.01234 1234567890.1234 12345.678901234 12345.678901234"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3586 %! pttrn = "%3u8%*s %5u16%*s %10u32%*s %15u64 %3d8%*s %5d16%*s %10d32%*s %15d64 %9f32%*s %14f64%*s %10.2f32%*s %12.2f64%*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3587 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3588 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3589 %! assert (C{2}, uint16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3590 %! assert (C{3}, uint32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3591 %! assert (C{4}, uint64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3592 %! assert (C{5}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3593 %! assert (C{6}, int16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3594 %! assert (C{7}, int32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3595 %! assert (C{8}, int64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3596 %! assert (C{9}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3597 %! assert (C{10}, double (1234567890.123), 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3598 %! assert (C{11}, single (12345.68), 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3599 %! assert (C{12}, double (12345.68), 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3600
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3601 %% Bit width, fixed width conv. specifiers -- check the right amount is left
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3602 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3603 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3604 %! str2 = [str2 str2 "123456789.01234"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3605 %! pttrn = "%3u8 %5u16 %10u32 %3d8 %5d16 %10d32 %9f32 %9f";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3606 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3607 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3608 %! assert (C{2}, uint16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3609 %! assert (C{3}, uint32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3610 %! assert (C{4}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3611 %! assert (C{5}, int16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3612 %! assert (C{6}, int32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3613 %! assert (C{7}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3614 %! assert (C{8}, double (0.01234), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3615
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3616 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3617 %! C = textscan ("123.123", "%2f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3618 %! assert (C{1}, 12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3619 %! assert (C{2}, 3.1, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3620 %! assert (C{3}, 23);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3621
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3622 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3623 %! C = textscan ("123.123", "%3f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3624 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3625 %! assert (C{2}, 0.12, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3626 %! assert (C{3}, 3);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3627
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3628 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3629 %! C = textscan ("123.123", "%4f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3630 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3631 %! assert (C{2}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3632
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3633 %% field width interrupts exponent. (Matlab incorrectly gives [12, 2e12])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3634 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3635 %! assert (textscan ("12e12", "%4f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3636 %! assert (textscan ("12e+12", "%5f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3637 %! assert (textscan ("125e-12","%6f"), {[12.5; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3638
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3639 %% %[] tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3640 %% Plain [..] and *[..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3641 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3642 %! ar = "abcdefguvwxAny\nacegxyzTrailing\nJunk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3643 %! C = textscan (ar, "%[abcdefg] %*[uvwxyz] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3644 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3645 %! assert (C{2}, {"Any"; "Trailing"; "Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3646
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3647 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3648 %! assert (textscan ("A2 B2 C3", "%*[ABC]%d", 3), {int32([2; 2; 3])});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3649
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3650 %% [^..] and *[^..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3651 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3652 %! br = "abcdefguvwx1Any\nacegxyz2Trailing\n3Junk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3653 %! C = textscan (br, "%[abcdefg] %*[^0123456789] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3654 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3655 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3656
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3657 %% [..] and [^..] containing delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3658 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3659 %! cr = "ab cd efguv wx1Any\na ce gx yz2Trailing\n 3Junk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3660 %! C = textscan (cr, "%[ abcdefg] %*[^0123456789] %s", "delimiter", " \n", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3661 %! assert (C{1}, {"ab cd efg"; "a ce g"; " "});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3662 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3663
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3664 %% Bug #36464
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3665 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3666 %! assert (textscan ('1 2 3 4 5 6', "%*n%n%*[^\n]"){1}, 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3667
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3668 %% test %[]] and %[^]]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3669 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3670 %! assert (textscan ('345]', "%*[123456]%[]]"){1}{1}, "]");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3671 %! assert (textscan ('345]', "%*[^]]%s"){1}{1}, "]");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3672
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3673 %% Test that "-i" checks the next two characters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3674 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3675 %! C = textscan ("-i -in -inf -infinity", "%f %f%s %f %f %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3676 %! assert (C, {-i, -i, {"n"}, -Inf, -Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3677
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3678 %% Again for "+i", this time with custom parser
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3679 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3680 %! C = textscan ("+i +in +inf +infinity", "%f %f%s %f %f %s", "ExpChars", "eE");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3681 %! assert (C, {i, i, {"n"}, Inf, Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3682
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3683 %% Check single quoted format interprets control sequences
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3684 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3685 %! C = textscan ("1 2\t3 4", '%f %[^\t] %f %f');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3686 %! assert (C, {1, {"2"}, 3, 4});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3687
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3688 %% Check overflow and underflow of integer types
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3689 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3690 %! a = "-1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3691 %! b = "1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3692 %! fmt = "%d8 %d16 %d32 %d64 %u8 %u16 %u32 %u64 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3693 %! C = textscan ([a a a a a a a a b b b b b b b b], fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3694 %! assert (C{1}, int8 ([-128; 127]));
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3695 %! assert (C{2}, int16 ([-32768; 32767]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3696 %! assert (C{3}, int32 ([-2147483648; 2147483647]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3697 %! assert (C{4}, int64 ([-9223372036854775808; 9223372036854775807]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3698 %! assert (C{5}, uint8 ([0; 255]));
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3699 %! assert (C{6}, uint16 ([0; 65535]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3700 %! assert (C{7}, uint32 ([0; 4294967295]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3701 %! assert (C{8}, uint64 ([0; 18446744073709551615]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3702
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3703 %% Tests from Matlab (does The MathWorks have any copyright over the input?)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3704 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3705 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3706 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3707 %! fprintf (fid,"09/12/2005 Level1 12.34 45 1.23e10 inf Nan Yes 5.1+3i\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3708 %! fprintf (fid,"10/12/2005 Level2 23.54 60 9e19 -inf 0.001 No 2.2-.5i\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3709 %! fprintf (fid,"11/12/2005 Level3 34.90 12 2e5 10 100 No 3.1+.1i\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3710 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3711 %! C = textscan (fid,"%s %s %f32 %d8 %u %f %f %s %f");
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3712 %! %assert (C{1}, {"09/12/2005";"10/12/2005";"11/12/2005"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3713 %! assert (C{2}, {"Level1";"Level2";"Level3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3714 %! assert (C{3}, [single(12.34);single(23.54);single(34.90)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3715 %! assert (C{4}, [int8(45);int8(60);int8(12)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3716 %! assert (C{5}, [uint32(4294967295);uint32(4294967295);uint32(200000)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3717 %! assert (C{6}, [inf;-inf;10]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3718 %! assert (C{7}, [NaN;0.001;100], eps);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3719 %! assert (C{8}, {"Yes";"No";"No"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3720 %! assert (C{9}, [5.1+3i;2.2-0.5i;3.1+0.1i]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3721 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3722 %! C = textscan (fid,"%s Level%d %f32 %d8 %u %f %f %s %f");
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3723 %! assert (C{2}, [int32(1);int32(2);int32(3)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3724 %! assert (C{3}, [single(12.34);single(23.54);single(34.90)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3725 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3726 %! C = textscan (fid,'%s %*[^\n]');
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3727 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3728 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3729 %! assert (C, {{"09/12/2005";"10/12/2005";"11/12/2005"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3730
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3731 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3732 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3733 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3734 %! fprintf (fid,"1, 2, 3, 4, , 6\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3735 %! fprintf (fid,"7, 8, 9, , 11, 12\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3736 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3737 %! C = textscan (fid,"%f %f %f %f %u8 %f", "Delimiter",",","EmptyValue",-Inf);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3738 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3739 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3740 %! assert (C{4}, [4; -Inf]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3741 %! assert (C{5}, uint8 ([0; 11]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3742
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3743 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3744 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3745 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3746 %! fprintf (fid,"abc, 2, NA, 3, 4\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3747 %! fprintf (fid,"// Comment Here\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3748 %! fprintf (fid,"def, na, 5, 6, 7\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3749 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3750 %! C = textscan (fid,"%s %n %n %n %n","Delimiter",",","TreatAsEmpty",{"NA","na"},"CommentStyle","//");
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3751 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3752 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3753 %! assert (C{1}, {"abc";"def"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3754 %! assert (C{2}, [2; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3755 %! assert (C{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3756 %! assert (C{4}, [3; 6]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3757 %! assert (C{5}, [4; 7]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3758
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3759 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3760 %!## Test start of comment as string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3761 %! c = textscan ("1 / 2 // 3", "%n %s %u8", "CommentStyle", {"//"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3762 %! assert (c, {1, "/", 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3763 */
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3764
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3765 // These tests have end-comment sequences, so can't just be in a comment
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3766 #if 0
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3767 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3768 %!## Test unfinished comment
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3769 %! c = textscan ("1 2 /* half comment", "%n %u8", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3770 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3771
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3772 ## Test reading from a real file
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3773 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3774 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3775 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3776 %! d = rand (1, 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3777 %! fprintf (fid, " %f %f /* comment */ %f %f ", d);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3778 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3779 %! A = textscan (fid, "%f %f", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3780 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3781 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3782 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3783 %! assert (A{1}, [d(1); d(3)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3784 %! assert (A{2}, [d(2); d(4)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3785 %! assert (E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3786 #endif