annotate libinterp/corefcn/textscan.cc @ 21497:2d71bb0011a0

textscan: Use empty () rather than 'length () == 0' on strings * textscan.cc (textscan_format_list::textscan_format_list, textscan_format_list::read_first_row, textscan::scan_one, textscan::parse_options): Use empty () instead of checking length () for clarity. * textscan.h (textscan::is_delim, textscan::whitespace_delim): Likewise.
author Mike Miller <mtmiller@octave.org>
date Fri, 18 Mar 2016 23:08:18 -0700
parents 5d69326e8cc5
children 9cb37fede0b6
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"
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
33 #include "ov.h"
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
34 #include "ovl.h"
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
35 #include "textscan.h"
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
36 #include "utils.h"
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
37
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
38 // 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
39 // 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
40 //
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
41 // 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
42 // 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
43 // textscan workload.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
44 //
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
45 // 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
46 // 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
47 // 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
48 // 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
49 // 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
50
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
51 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
52 delimited_stream
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
53 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
54 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
55
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
56 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
57 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
58
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
59 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
60
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
61 ~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
62
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
63 // 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
64 // 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
65 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
66 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
67 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
68 refresh_buf ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
69 }
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 // 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
72 // 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
73 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
74
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
75 // 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
76 // 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
77 int get (void)
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
78 {
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
79 if (delimited)
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
80 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
81 else
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
82 return get_undelim ();
db67dc54fa2c textscan: Avoid buffer out-of-bounds errors in delimited_stream
Mike Miller <mtmiller@octave.org>
parents: 21491
diff changeset
83 }
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
84
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
85 // 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
86 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
87
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
88 // 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
89 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
90
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
91 // 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
92 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
93
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
94 // 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
95 // 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
96 // 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
97 // 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
98 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
99
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
100 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
101
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
102 // 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
103
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
104 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
105
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
106 // 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
107 // 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
108 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
109
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
110 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
111
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
112 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
113 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
114 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
115 }
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 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
118
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
119 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
120
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
121 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
122
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
123 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
124
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
125 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
126 = (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
127 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
128 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
129 }
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 // 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
132 // (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
133
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
134 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
135
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
136 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
137
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
138 private:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
139
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
140 // 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
141 int bufsize;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
142
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
143 // 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
144 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
145
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
146 // 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
147 char *buf;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
148
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
149 // 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
150 char *idx;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
151
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
152 // 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
153 // 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
154 char *last;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
155
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
156 // 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
157 char *eob;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
158
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
159 // 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
160 bool delimited;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
161
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
162 // 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
163 int longest;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
164
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
165 // 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
166 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
167
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
168 // 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
169 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
170
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
171 // 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
172 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
173
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
174 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
175
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
176 // No copying!
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
177
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
178 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
179
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
180 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
181 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
182
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
183 // 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
184 // 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
185 // 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
186
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
187 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
188 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
189 int longest_lookahead,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
190 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
191 : 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
192 delims (delimiters),
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
193 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
194 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
195 buf = new char[bufsize];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
196 eob = buf + bufsize;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
197 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
198 progress_marker = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
199 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
200 }
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 // 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
203 // 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
204 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
205 const delimited_stream& ds)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
206 : 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
207 delims (ds.delims),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
208 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
209 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
210 buf = new char[bufsize];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
211 eob = buf + bufsize;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
212 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
213 progress_marker = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
214 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
215 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
216
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
217 delimited_stream::~delimited_stream (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
218 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
219 // 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
220 if (! eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
221 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
222 i_stream.clear ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
223 i_stream.seekg (buf_in_file);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
224 i_stream.read (buf, idx - buf);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
225 }
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 delete [] buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
228 }
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 // 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
231 // if necessary.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
232
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
233 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
234 delimited_stream::get_undelim (void)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
235 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
236 int retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
237 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
238 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
239 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
240 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
241 }
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 if (idx < eob)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
244 retval = *idx++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
245 else
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
246 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
247 refresh_buf ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
248
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
249 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
250 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
251 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
252 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
253 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
254 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
255 retval = *idx++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
256 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
257
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
258 if (idx >= last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
259 delimited = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
260
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
261 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
262 }
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 // 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
265 // 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
266
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
267 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
268 delimited_stream::peek_undelim (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
269 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
270 int retval = get_undelim ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
271 putback ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
272
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
273 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
274 }
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 // 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
277 // 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
278 // 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
279 // processed.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
280
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
281 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
282 delimited_stream::refresh_buf (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
283 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
284 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
285 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
286
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
287 int retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
288 int old_remaining = eob - idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
289
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
290 if (old_remaining < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
291 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
292 idx = eob;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
293 old_remaining = 0;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
296 octave_quit (); // allow ctrl-C
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
297
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
298 if (old_remaining > 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
299 memmove (buf, idx, old_remaining);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
300
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
301 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
302 idx = buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
303
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
304 int gcount; // chars read
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
305 if (! i_stream.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
306 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
307 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
308 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
309 gcount = i_stream.gcount ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
310 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
311 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
312 gcount = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
313
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
314 eob = buf + old_remaining + gcount;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
315 last = eob;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
316 if (gcount == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
317 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
318 delimited = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
319
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
320 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
321 retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
322 else
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
323 // 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
324 retval = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
325 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
326 else
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
327 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
328 delimited = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
329
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
330 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
331 {
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
332 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
333 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
334 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
335
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
336 if (last - buf < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
337 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
338
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
339 retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
340 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
341
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
342 // 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
343 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
344 *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
345
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
346 return retval;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
349 // 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
350 // 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
351 // 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
352 // 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
353 // 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
354
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
355 char *
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
356 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
357 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
358 char *retval;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
359
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
360 if (eob - idx > size)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
361 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
362 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
363 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
364 if (idx > last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
365 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
366 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
367 else
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
368 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
369 // 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
370 // 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
371 // 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
372 // 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
373
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
374 if (eob - prior_tell + size < bufsize)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
375 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
376 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
377 idx = prior_tell;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
378 refresh_buf ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
379 idx += gap;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
380 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
381 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
382 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
383 refresh_buf ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
384 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
385
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
386 prior_tell = buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
387
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
388 if (eob - idx > size)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
389 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
390 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
391 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
392 if (idx > last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
393 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
394 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
395 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
396 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
397 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
398 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
399 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
400 memset (eob, 0, size + (idx - buf));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
401 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
402 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
403 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
404 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
405 retval = buffer;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
406 // FIXME -- read bufsize at a time
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
407 int i;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
408 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
409 *buffer++ = get_undelim ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
410 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
411 memset (buffer, 0, size - i);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
412 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
413 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
414 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
415
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
416 return retval;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
419 // 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
420 // must have length at least 1.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
421
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
422 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
423 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
424 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
425 int len = out.length (), used = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
426 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
427 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
428 && ch != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
429 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
430 out[used++] = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
431 if (used == len)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
432 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
433 len <<= 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
434 out.resize (len);
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
437 out.resize (used);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
438 field_done ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
439
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
440 return ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
441 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
442
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
443 // 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
444
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
445 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
446 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
447 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
448 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
449
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
450 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
451 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
452 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
453 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
454 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
455
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
456 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
457 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
458 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
459 : 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
460 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
461 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
462 { }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
463
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
464 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
465 : 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
466 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
467 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
468 { }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
469
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
470 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
471 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
472 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
473 {
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
474 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
475 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
476 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
477 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
478 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
479 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
480 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
481 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
482 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
483
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
484 return *this;
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
487 // The C-style format string.
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
488 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
489
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
490 // 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
491 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
492
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
493 // 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
494 // 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
495 int prec;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
496
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
497 // 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
498 // 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
499 int bitwidth;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
500
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
501 // 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
502 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
503
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
504 // 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
505 // -- `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
506 char type;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
507
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
508 // 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
509 bool discard;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
510
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
511 // 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
512 bool numeric;
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
515 class textscan;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
516
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
517 // 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
518
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
519 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
520 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
521 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
522 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
523
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
524 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
525
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
526 ~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
527
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
528 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
529
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
530 // 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
531 // 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
532 // 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
533 // last conversion.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
534
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
535 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
536
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
537 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
538 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
539 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
540 return current ();
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
543 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
544 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
545 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
546 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
547
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
548 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
549 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
550 curr_idx++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
551
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
552 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
553 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
554 if (cycle)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
555 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
556 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
557 return 0;
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
560 return current ();
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
563 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
564
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
565 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
566
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
567 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
568
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
569 // 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
570 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
571
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
572 // 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
573 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
574
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
575 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
576
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
577 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
578
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
579 private:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
580
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
581 // 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
582 // 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
583 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
584
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
585 // 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
586 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
587
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
588 // 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
589 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
590
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
591 // 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
592 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
593
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
594 // Temporary buffer.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
595 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
596
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
597 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
598 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
599 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
600 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
601
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
602 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
603
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
604 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
605 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
606 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
607 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
608 // No copying!
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
609
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
610 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
611
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
612 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
613 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
614
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
615
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
616 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
617 : 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
618 fmt_elts (), buf (0)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
619 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
620 size_t n = s.length ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
621
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
622 size_t i = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
623
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
624 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
625 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
626 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
627 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
628 char type = '\0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
629
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
630 bool have_more = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
631
21497
2d71bb0011a0 textscan: Use empty () rather than 'length () == 0' on strings
Mike Miller <mtmiller@octave.org>
parents: 21496
diff changeset
632 if (s.empty ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
633 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
634 buf = new std::ostringstream ("%f");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
635 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
636 type = 'f';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
637 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
638 discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
639 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
640 set_from_first = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
641 nconv = 1;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
642 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
643 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
644 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
645 set_from_first = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
646
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
647 while (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
648 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
649 have_more = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
650
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
651 if (! buf)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
652 buf = new std::ostringstream ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
653
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
654 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
655 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
656 // Process percent-escape conversion type.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
657
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
658 process_conversion (s, i, n);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
659
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
660 have_more = (buf != 0);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
661 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
662 else if (isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
663 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
664 while (++i < n && isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
665 /* skip whitespace */;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
666
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
667 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
668 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
669 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
670 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
671 type = textscan_format_elt::literal_conversion;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
672
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
673 width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
674 prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
675 bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
676 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
677
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
678 while (i < n && ! isspace (s[i])
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
679 && (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
680 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
681 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
682 i++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
683 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
684 width++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
685 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
686
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
687 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
688 discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
689
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
690 have_more = false;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
693 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
694 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
695 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
696 break;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
701 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
702 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
703
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
704 delete buf;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
707 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
708 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
709 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
710
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
711 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
712 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
713 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
714 delete elt;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
718 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
719 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
720 int bitwidth, octave_value val_type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
721 bool discard, char type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
722 const std::string& char_class)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
723 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
724 if (buf)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
725 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
726 std::string text = buf->str ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
727
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
728 if (! text.empty ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
729 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
730 textscan_format_elt *elt
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
731 = 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
732
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
733 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
734 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
735
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
736 fmt_elts.push_back (elt);
21467
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
739 delete buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
740 buf = 0;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
744 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
745 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
746 size_t n)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
747 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
748 unsigned width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
749 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
750 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
751 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
752 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
753 char type = '\0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
754
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
755 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
756
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
757 bool have_width = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
758
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
759 while (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
760 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
761 switch (s[i])
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
762 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
763 case '*':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
764 if (discard)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
765 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
766 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
767 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
768 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
769 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
770 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
771 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
772
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
773 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
774 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
775 if (have_width)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
776 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
777 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
778 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
779 char c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
780 width = width * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
781 have_width = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
782 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
783 while (i < n && isdigit (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
784 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
785 c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
786 width = width * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
787 *buf << c;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
790 if (i < n && s[i] == '.')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
791 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
792 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
793 prec = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
794 while (i < n && isdigit (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
795 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
796 c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
797 prec = prec * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
798 *buf << c;
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
802 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
803
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
804 case 'd': case 'u':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
805 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
806 bool done = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
807 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
808 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
809 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
810 if (s[i] == '8')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
811 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
812 bitwidth = 8;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
813 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
814 val_type = octave_value (int8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
815 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
816 val_type = octave_value (uint8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
817 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
818 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
819 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
820 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
821 bitwidth = 16;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
822 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
823 val_type = octave_value (int16NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
824 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
825 val_type = octave_value (uint16NDArray ());
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 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
828 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
829 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
830 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
831 done = false; // use default size below
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 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
834 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
835 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
836 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
837 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
838 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
839 val_type = octave_value (int64NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
840 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
841 val_type = octave_value (uint64NDArray ());
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 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
844 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
845 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
846 done = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
847 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
848 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
849 done = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
850
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
851 if (! done)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
852 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
853 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
854 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
855 val_type = octave_value (int32NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
856 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
857 val_type = octave_value (uint32NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
858 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
859 goto fini;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
862 case 'f':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
863 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
864 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
865 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
866 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
867 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
868 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
869 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
870 val_type = octave_value (FloatNDArray ());
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 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
873 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
874 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
875 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
876 val_type = octave_value (NDArray ());
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 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
879 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
880 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
881 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
882 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
883 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
884 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
885 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
886
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
887 case 'n':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
888 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
889 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
890 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
891 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
892
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
893 case 's': case 'q': case '[': case 'c':
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
894 if (! discard)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
895 val_type = octave_value (Cell ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
896 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
897 has_string = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
898 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
899
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
900 fini:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
901 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
902 if (! have_width)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
903 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
904 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
905 width = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
906 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
907 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
908 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
909
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
910 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
911 discard, type) == 0)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
912 return;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
913 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
914 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
915
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
916 default:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
917 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
918 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
919
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
920 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
921 break;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
924 nconv = -1;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
927 // Parse [...] and [^...]
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
928 //
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
929 // 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
930 // 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
931 // 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
932 // 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
933 // pattern.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
934 //
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
935 // 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
936 // 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
937 // The latter is efficient for [^...] patterns.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
938
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
939 static std::string
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
940 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
941 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
942 int len = pattern.length ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
943 if (len == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
944 return "";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
945
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
946 std::string retval (256, '\0');
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
947 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
948
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
949 int in = 0, out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
950 unsigned char ch, prev = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
951 bool flip = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
952
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
953 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
954 if (ch == '^')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
955 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
956 in++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
957 flip = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
958 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
959 mask[pattern[in]] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
960 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
961
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
962 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
963 bool prev_prev_was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
964 for (; in < len; in++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
965 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
966 bool was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
967 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
968 if (ch == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
969 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
970
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
971 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
972 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
973 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
974 if (start_of_range < ch
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
975 && ((isupper (ch) && isupper (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
976 || (islower (ch) && islower (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
977 || (isdigit (ch) && isdigit (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
978 || mask['-'] > 1)) // not the first '-'
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
979 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
980 was_range = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
981 out--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
982 mask['-']--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
983 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
984 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
985 if (mask[i] == '\0')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
986 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
987 mask[i] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
988 retval[out++] = i;
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
992 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
993 if (! was_range)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
994 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
995 if (mask[ch]++ == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
996 retval[out++] = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
997 else if (ch != '-')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
998 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
999 "textscan: [...] contains two '%c's", ch);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1000
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1001 if (prev == '-' && mask['-'] >= 2)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1002 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1003 "textscan: [...] contains two '-'s "
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1004 "outside range expressions");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1005 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1006 prev = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1007 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
1008 prev_was_range = was_range;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1009 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1010
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1011 if (flip) // [^...]
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1012 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1013 out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1014 for (int i = 0; i < 256; i++)
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1015 if (! mask[i])
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1016 retval[out++] = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1017 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1018
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1019 retval.resize (out);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1020
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1021 return retval;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1024 int
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1025 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
1026 size_t n, unsigned int& width,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1027 int& prec, int& bitwidth,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1028 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
1029 char& type)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1030 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1031 int retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1032
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1033 std::string char_class;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1034
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1035 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
1036 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
1037
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1038 if (type != '%')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1039 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1040 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1041 if (type == '[')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1042 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1043 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1044 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1045 beg_idx = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1046
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1047 if (s[i] == '^')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1048 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1049 type = '^';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1050 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1051
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1052 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1053 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1054 beg_idx = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1055
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1056 if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1057 *buf << s[i++];
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1060 else if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1061 *buf << s[i++];
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1064 while (i < n && s[i] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1065 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1066
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1067 if (i < n && s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1068 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1069 end_idx = i-1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1070 *buf << s[i++];
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1073 if (s[i-1] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1074 retval = nconv = -1;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1078 if (nconv >= 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1079 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1080 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
1081 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
1082 end_idx - beg_idx + 1));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1083
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1084 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
1085 char_class);
21462
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1088 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1089 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1090
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1091 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1092 textscan_format_list::printme (void) const
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1093 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1094 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
1095
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1096 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
1097 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1098 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
1099
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1100 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1101 << "width: " << elt->width << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1102 << "digits " << elt->prec << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1103 << "bitwidth: " << elt->bitwidth << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1104 << "discard: " << elt->discard << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1105 << "type: ";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1106
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1107 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
1108 std::cerr << "literal text\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1109 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
1110 std::cerr << "whitespace\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1111 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1112 std::cerr << elt->type << "\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1113
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1114 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1115 << "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
1116 << "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
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1120 // 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
1121 // 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
1122
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1123 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1124 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
1125 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1126 // 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
1127 std::string first_line (20, ' ');
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1128
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1129 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
1130
21497
2d71bb0011a0 textscan: Use empty () rather than 'length () == 0' on strings
Mike Miller <mtmiller@octave.org>
parents: 21496
diff changeset
1131 if (! first_line.empty ()
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1132 && 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
1133 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
1134
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1135 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
1136 delimited_stream ds (strstr, is);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1137
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1138 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
1139 Complex val;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1140 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1141 nconv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1142 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
1143 int retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1144
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1145 // 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
1146 while (! ds.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1147 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1148 bool already_skipped_delim = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1149 ts.skip_whitespace (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1150 ds.progress_benchmark ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1151 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
1152 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
1153 if (ds.fail ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1154 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1155 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
1156
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1157 if (ds.eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1158 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1159
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1160 // 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
1161 // 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
1162 // return with an error status.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1163 if (ts.return_on_error < 2)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1164 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1165 ts.skip_delim (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1166 if (ds.no_progress ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1167 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1168 retval = 4;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1169 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1170 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1171 already_skipped_delim = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1172 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1173 else // skip offending field
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1174 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1175 std::ios::iostate state = ds.rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1176 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
1177
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1178 std::string dummy;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1179 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
1180 ts.scan_string (ds, fe, dummy);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1181
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1182 progress = (dummy.length ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1183 ds.setstate (state);
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1186 val = ts.empty_value.scalar_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1187
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1188 if (! --max_empty)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1189 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1190 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1191
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1192 if (val.imag () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1193 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
1194 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1195 val_type = octave_value (ComplexNDArray (dv, val));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1196
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1197 output_container.push_back (val_type);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1198
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1199 if (! already_skipped_delim)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1200 ts.skip_delim (ds);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1201
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1202 if (! progress && ds.no_progress ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1203 break;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1204
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1205 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1206 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1207
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1208 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
1209
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1210 // 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
1211 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
1212 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
1213
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1214 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
1215 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1216
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1217 // 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
1218
21488
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1219 static Cell
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1220 init_inf_nan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1221 {
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1222 Cell retval (dim_vector (1, 2));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1223
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1224 retval(0) = Cell (octave_value ("inf"));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1225 retval(1) = Cell (octave_value ("nan"));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1226
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1227 return retval;
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
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1230 textscan::textscan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1231 : buf (), whitespace_table (), delim_table (), delims (),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1232 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
1233 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
1234 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
1235 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
1236 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
1237 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
1238 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
1239 numeric_delim (false), lines (0)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1240 { }
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1241
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1242 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1243 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
1244 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1245 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
1246 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
1247
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1248 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
1249 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
1250 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
1251 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1252 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
1253
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1254 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
1255 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
1256
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1257 params++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1258 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1259 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1260 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
1261 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
1262
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1263 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
1264
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1265 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
1266 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1267 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
1268 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1269 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
1270
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1271 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
1272 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
1273 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
1274
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1275 params++;
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 }
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 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
1280
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1281 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
1282
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1283 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
1284
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1285 return do_scan (isp, fmt_list, ntimes);
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1288 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1289 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
1290 octave_idx_type ntimes)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1291 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1292 octave_value retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1293
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1294 if (fmt_list.num_conversions () == -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1295 error ("textscan: invalid format specified");
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1296
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1297 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1298 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
1299
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1300 // skip the first header_lines
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1301 std::string dummy;
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1302 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
1303 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
1304
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1305 // 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
1306
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1307 // 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
1308 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
1309 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
1310
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1311 // 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
1312 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
1313 if (buffer_size)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1314 buf_size = buffer_size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1315 else if (ntimes > 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1316 {
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1317 // 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
1318 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
1319 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
1320 }
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1321 // Finally, create the stream.
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1322 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
1323
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1324 // 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
1325 // (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
1326 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
1327 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
1328 row_idx(1) = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1329
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1330 int err = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1331 octave_idx_type row = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1332
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1333 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
1334 skip_delim (is);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1335
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1336 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
1337
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1338 // 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
1339 if (fmt_list.set_from_first)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1340 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1341 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
1342 lines = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1343
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1344 done_after = fmt_list.numel () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1345 if (! err)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1346 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
1347 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1348 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1349 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
1350
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1351 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
1352
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1353 // 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
1354 // Check now which columns to merge.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1355 // 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
1356 // after reading in data.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1357 // 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
1358 // 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
1359 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
1360 int conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1361 if (collect_output)
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 int prev_type = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1364 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
1365 col != out.end (); col++)
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 if (col->type_id () == prev_type
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1368 || (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
1369 merge_with_prev [conv++] = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1370 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1371 merge_with_prev [conv++] = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1372
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1373 prev_type = col->type_id ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1374 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1375 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1376
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1377 // 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
1378 // infinite loop below.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1379 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1380 error ("textscan: No conversions specified");
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 // 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
1383 if (! err)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1384 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1385 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
1386 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1387 if (row == 0 || row >= size)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1388 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1389 size += size+1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1390 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
1391 col != out.end (); col++)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1392 *col = (*col).resize (dim_vector (size, 1), 0);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1393 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1394
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1395 row_idx(0) = row;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1396 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
1397
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1398 if (err > 0 || ! is || (lines >= ntimes && ntimes > -1))
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1399 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1400 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1401 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1402
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1403 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
1404 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
1405 done_after + 1, row + 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1406
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1407 // 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
1408 bool uneven_columns = false;
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1409 if (err & 4)
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1410 uneven_columns = true;
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1411 else if (isp.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1412 {
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1413 isp.clear ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1414 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
1415 int last_char = isp.get ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1416 isp.setstate (isp.eofbit);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1417 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
1418 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1419
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1420 // convert return value to Cell array
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1421 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
1422
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1423 // (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
1424 // 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
1425 if (err & 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1426 done_after = out.size () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1427
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1428 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
1429 dim_vector dv (valid_rows, 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1430
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1431 ra_idx(0) = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1432 int i = 0;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1433 if (! collect_output)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1434 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1435 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
1436 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
1437 col != out.end (); col++, i++)
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 // 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
1440 if (i == done_after && uneven_columns)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1441 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
1442
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1443 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1444 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
1445 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1446 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1447 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1448 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
1449 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1450 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
1451 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
1452 int prev_type = -1;
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 conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1455 retval = Cell ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1456 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
1457 col != out.end (); col++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1458 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1459 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
1460 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1461 if (prev_type != -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1462 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1463 ra_idx(1) = i++;
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
1464 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
1465 ra_idx);
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 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
1468 group_size = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1469 prev_type = col->type_id ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1470 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1471 else
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 ra_idx(1) = group_size++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1474 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
1475 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1476 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1477 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1478 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1479 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
1480 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1481
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1482 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1483 }
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1484
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1485 // 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
1486 // 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
1487
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1488 static double
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1489 pown (double x, unsigned int n)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1490 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1491 double retval = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1492
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1493 for (unsigned int d = n; d; d >>= 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1494 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1495 if (d & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1496 retval *= x;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1497 x *= x;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1498 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1499
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1500 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1501 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1502
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1503 // 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
1504 // exp_chars option of options.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1505
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1506 double
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1507 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
1508 const textscan_format_elt& fmt) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1509 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1510 int sign = 1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1511 unsigned int width_left = fmt.width;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1512 double retval = 0;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1513 bool valid = false; // syntactically correct double?
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1514
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1515 int ch = is.peek ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1516
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1517 if (ch == '+')
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 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1520 ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1521 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1522 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1523 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1524 else if (ch == '-')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1525 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1526 sign = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1527 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1528 ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1529 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1530 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1531 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1532
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1533 // Read integer part
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1534 if (ch != '.')
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 >= '0' && ch <= '9') // valid if at least one digit
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1537 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1538 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
1539 retval = retval * 10 + (ch - '0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1540 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1541 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1542
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1543 // Read fractional part, up to specified precision
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1544 if (ch == '.' && width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1545 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1546 double multiplier = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1547 int precision = fmt.prec;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1548 int i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1549
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1550 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1551 width_left--; // Consider width of '.'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1552
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1553 if (precision == -1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1554 precision = 1<<30; // FIXME Should be MAXINT
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1555
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1556 if (! valid) // if there was nothing before '.'...
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1557 is.get (); // ...ch was a "peek", not "get".
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1558
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1559 for (i = 0; i < precision; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1560 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1561 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
1562 retval += (ch - '0') * (multiplier *= 0.1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1563 else
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 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1566 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1567 }
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1570 // 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
1571 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
1572 && ch <= '9')
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1573 retval += multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1574
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1575 if (i > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1576 valid = true; // valid if at least one digit after '.'
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 // 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
1579 if (i == precision)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1580 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
1581 ; // discard
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1582
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1583 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1584 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1585
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1586 // 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
1587 bool used_exp = false;
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1588 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
1589 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1590 int ch1 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1591 if (ch1 == '-' || ch1 == '+' || (ch1 >= '0' && ch1 <= '9'))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1592 { // 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
1593 width_left--; // count "E"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1594 int exp = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1595 int exp_sign = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1596 if (ch1 == '+')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1597 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1598 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1599 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1600 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1601 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1602 else if (ch1 == '-')
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 exp_sign = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1605 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1606 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1607 width_left--;
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 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1610 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
1611 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1612 exp = exp*10 + ch - '0';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1613 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1614 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1615 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
1616 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
1617 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1618
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1619 double multiplier = pown (10, exp);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1620 if (exp_sign > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1621 retval *= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1622 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1623 retval /= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1624
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1625 used_exp = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1626 }
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 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
1629 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
1630 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1631
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1632 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1633 if (! valid && width_left >= 3)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1634 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1635 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
1636 if (i == 0)
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 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1639 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1640 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1641 else if (i == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1642 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1643 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1644 valid = 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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1648 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1649 if (! valid && width_left >= 3)
21462
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 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
1652 if (i == 0)
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 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1655 valid = true;
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 else if (i == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1658 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1659 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1660 valid = true;
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1663
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1664 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1665 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1666 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1667 is.setstate (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1668
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1669 return retval * sign;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1670 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1671
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1672 // 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
1673 // 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
1674 // 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
1675
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1676 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1677 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
1678 Complex& val) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1679 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1680 double im = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1681 double re = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1682 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
1683 bool inf = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1684
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1685 int ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1686 if (ch == '+' || ch == '-') // check for [+-][ij] with no coefficients
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 ch = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1689 int ch2 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1690 if (ch2 == 'i' || ch2 == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1691 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1692 double value = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1693 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1694 // Check not -inf
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1695 if (is.peek () == 'n')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1696 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1697 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1698 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1699
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1700 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1701 ch2 = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1702 if (ch2 == 'f')
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 inf = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1705 re = (ch == '+') ? octave_Inf : -octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1706 value = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1707 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1708 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1709 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1710 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1711 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1712 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1713 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1714
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1715 im = (ch == '+') ? value : -value;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1716 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1717 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1718 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1719 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1720
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1721 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
1722 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1723 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1724 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1725 //re = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1726 re = read_double (is, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1727
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1728 // check for "treat as empty" string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1729 if (treat_as_empty.numel ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1730 && (is.fail () || octave_is_NaN_or_NA (Complex (re))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1731 || re == octave_Inf))
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 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
1735 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1736 if (ch == treat_as_empty (i).string_value ()[0])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1737 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1738 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
1739 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1740 }
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 if (as_empty) // if first char matched...
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1743 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1744 as_empty = false; // ...look for the whole string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1745
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1746 is.clear (state); // treat_as_empty "-" causes partial read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1747 is.seekg (pos); // reset to position before failed read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1748
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1749 // treat_as_empty strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1750 // 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
1751 // that matches.
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
1752 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
1753 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
1754
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1755 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1756 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1757 // FIXME -- is.read could invalidate pos
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1758
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1759 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
1760 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1761 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
1762 if (! strncmp (s.c_str (), look, s.size ()))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1763 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1764 as_empty = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1765 // 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
1766 is.read (&look_buf[0], s.size (), pos);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1767 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1768 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1769 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1770 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1771 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1772
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1773 if (! is.eof () && ! as_empty)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1774 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1775 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
1776 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1777 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
1778 if (ch == 'i' || ch == 'j') // pure imaginary
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 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1781 im = re;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1782 re = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1783 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1784 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
1785 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1786 // 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
1787 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1788 state = is.rdstate ();
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 //im = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1791 im = read_double (is, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1792 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1793 im = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1794
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1795 if (is.peek () == 'i' || is.peek () == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1796 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1797 else
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 im = 0; // no valid imaginary part. Restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1800 is.clear (state); // eof shouldn't cause fail.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1801 is.seekg (pos);
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1804 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
1805 is.clear (state); // failed peek shouldn't cause fail
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1806 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1807 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1808 if (as_empty)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1809 val = empty_value.scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1810 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1811 val = Complex (re, im);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1812 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1813
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1814 // 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
1815
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1816 int
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1817 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
1818 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1819 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1820 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1821 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1822
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1823 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
1824 ? 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
1825 : 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
1826 != std::istream::traits_type::eof ())
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1827 && pattern.find (c1) == std::string::npos)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1828 obuf << static_cast<char> (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1829
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1830 val = obuf.str ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1831
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1832 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1833 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1834
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1835 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1836 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1837
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1838 // 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
1839 // 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
1840
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1841 std::string
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1842 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
1843 const std::string& ends) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1844 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1845 std::string retval ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1846 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1847 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1848 { // find sequence ending with an ending char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1849 std::string next;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1850 scan_caret (is, ends.c_str (), next);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1851 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
1852
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1853 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
1854 ? 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
1855
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1856 if (last != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1857 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1858 retval = retval + static_cast<char> (last);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1859 for (int i = 0; i < delimiters.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1860 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1861 std::string delim = delimiters(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1862 int start = retval.length () - delim.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1863 if (start < 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1864 start = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1865 std::string may_match = retval.substr (start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1866 if (may_match == delim)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1867 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1868 done = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1869 retval = retval.substr (0, start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1870 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1871 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1872 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1873 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1874 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1875 while (! done && is && ! is.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 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1878 }
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1881 // 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
1882 // 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
1883 // 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
1884
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1885 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1886 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
1887 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1888 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1889 if (delim_list.numel () == 0)
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 unsigned int i = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1892 unsigned int width = fmt.width;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1893
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1894 for (i = 0; i < width; i++)
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 if (i+1 > val.length ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1897 val = val + val + ' '; // grow even if empty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1898 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
1899 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
1900 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1901 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1902 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1903 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1904 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1905 val[i] = ch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1906 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1907 val = val.substr (0, i); // trim pre-allocation
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1908 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1909 else // Cell array of multi-character delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1910 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1911 std::string ends ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1912 for (int i = 0; i < delim_list.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1913 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1914 std::string tmp = delim_list(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1915 ends += tmp.substr (tmp.length () - 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1916 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1917 val = textscan::read_until (is, delim_list, ends);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1918 }
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1921 // 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
1922
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1923 int
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1924 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
1925 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1926 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1927 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1928 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1929
21494
8e9833b8791d textscan: Prefer using std::string to char *
Mike Miller <mtmiller@octave.org>
parents: 21493
diff changeset
1930 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
1931 obuf << static_cast<char> (c1);
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 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
1934 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1935 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1936 return c1;
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1939 // 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
1940 // 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
1941 // 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
1942
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1943 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1944 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
1945 std::string& val)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1946 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1947 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1948
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1949 if (is.peek () != '\"')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1950 scan_string (is, fmt, val);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1951 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1952 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1953 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1954 scan_caret (is, "\"", val); // read everything until "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1955 is.get (); // swallow "
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 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
1958 { // and keep looking for single "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1959 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1960 std::string val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1961 scan_caret (is, "\"", val1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1962 val = val + "\"" + val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1963 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1964 }
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1967
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1968 // 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
1969 // including any whitespace or delimiters.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1970
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1971 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1972 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
1973 std::string& val) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1974 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1975 val.resize (fmt.width);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1976
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1977 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
1978 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1979 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
1980 if (ch != std::istream::traits_type::eof ())
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1981 val[i] = ch;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1982 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1983 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1984 val.resize (i);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1985 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1986 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1987 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1988 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1989
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1990
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1991 // 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
1992
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1993 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1994 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
1995 octave_value& ov, Array<octave_idx_type> row)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1996 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1997 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1998
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1999 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2000
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2001 octave_value val;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2002 if (fmt.numeric)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2003 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2004 if (fmt.type == 'f' || fmt.type == 'n')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2005 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2006 Complex v;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2007 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2008 scan_complex (is, fmt, v);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2009
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2010 if (! fmt.discard && ! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2011 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2012 if (fmt.bitwidth == 64)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2013 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2014 if (ov.is_real_type () && v.imag () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2015 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
2016 else
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 if (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2019 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2020 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2021 ov.internal_rep ()->fast_elem_insert (row(0), v);
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2024 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2025 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2026 if (ov.is_real_type () && v.imag () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2027 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2028 float (v.real ()));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2029 else
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 (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2032 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2033 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2034 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2035 FloatComplex (v));
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2038 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2039 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2040 else
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 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
2043 // 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
2044 // Some loss of precision for d64 and u64.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2045 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2046 v = read_double (is, fmt);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2047 if (! fmt.discard && ! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2048 switch (fmt.bitwidth)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2049 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2050 case 64:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2051 switch (fmt.type)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2052 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2053 case 'd':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2054 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2055 octave_int64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2056 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
2057 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2058 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2059
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2060 case 'u':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2061 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2062 octave_uint64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2063 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
2064 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2065 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2066 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2067 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2068
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2069 case 32:
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_int32 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);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2076 }
21469
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':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2080 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2081 octave_uint32 vv = v;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2082 ov.internal_rep ()->fast_elem_insert (row(0), vv);
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 16:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2089 if (fmt.type == 'd')
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 octave_int16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2092 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
2093 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2094 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2095 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2096 octave_uint16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2097 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
2098 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2099 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2100
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2101 case 8:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2102 if (fmt.type == 'd')
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2103 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2104 octave_int8 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2105 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
2106 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2107 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2108 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2109 octave_uint8 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2110 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
2111 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2112 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2113 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2114 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2115
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2116 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2117 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2118 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2119 ov = do_cat_op (ov, empty_value, row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2120
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2121 // 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
2122 if (return_on_error == 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2123 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2124 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2125 is.clear (); // clear to allow read pointer to advance
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2126
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2127 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2128 scan_string (is, fmt, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2129
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2130 is.setstate (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2131 }
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2134 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2135 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2136 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2137 std::string vv (" "); // initial buffer. Grows as needed
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2138 switch (fmt.type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2139 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2140 case 's':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2141 scan_string (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2142 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2143
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2144 case 'q':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2145 scan_qstring (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2146 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2147
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2148 case 'c':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2149 scan_cstring (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2150 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2151
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2152 case '[':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2153 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
2154 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2155
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2156 case '^':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2157 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
2158 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2159 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2160
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2161 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2162 ov.internal_rep ()->fast_elem_insert (row (0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2163 Cell (octave_value (vv)));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2164
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2165 // 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
2166 if (! vv.empty ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2167 is.clear (is.rdstate () & ~std::ios_base::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2168 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2169
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2170 is.field_done ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2171 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2172
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2173 // 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
2174 // values in row ROW of retval.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2175
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2176 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2177 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
2178 textscan_format_list& fmt_list,
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
2179 std::list<octave_value>& retval,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2180 Array<octave_idx_type> row, int& done_after)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2181 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2182 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
2183 std::list<octave_value>::iterator out = retval.begin ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2184 bool no_conversions = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2185 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2186 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
2187
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2188 octave_quit ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2189
21486
4581d0d52948 avoid signed/unsigned comparison warnings introduced in previous change
John W. Eaton <jwe@octave.org>
parents: 21485
diff changeset
2190 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
2191 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2192 bool this_conversion_failed = false;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2193
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2194 // Clear fail of previous numeric conversions.
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2195 is.clear ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2196
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2197 switch (elem->type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2198 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2199 case 'C':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2200 case 'D':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2201 std::cerr << "textscan: Conversion %" << elem->type
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2202 << " not yet implemented\n";
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2203 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2204
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2205 case 'u':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2206 case 'd':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2207 case 'f':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2208 case 'n':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2209 case 's':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2210 case '[':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2211 case '^':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2212 case 'q':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2213 case 'c':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2214 scan_one (is, *elem, *out, row);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2215 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2216
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2217 case textscan_format_elt::literal_conversion :
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2218 match_literal (is, *elem);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2219 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2220
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2221 default:
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2222 error ("Unknown format element '%c'", elem->type);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2223 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2224
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2225 if (! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2226 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2227 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2228 no_conversions = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2229 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2230 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2231 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2232 if (return_on_error < 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2233 this_conversion_failed = true;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2234
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2235 is.clear (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2236 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2237
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2238 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2239 out++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2240
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2241 elem = fmt_list.next ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2242 char *pos = is.tellg ();
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2243
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2244 // FIXME -- these conversions "ignore delimiters". Should they include
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2245 // 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
2246 if (elem->type != textscan_format_elt::literal_conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2247 // && elem->type != '[' && elem->type != '^' && elem->type != 'c'
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 skip_delim (is);
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2250
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2251 if (this_conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2252 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2253 if (is.tellg () == pos && ! conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2254 { // done_after = first failure
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2255 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
2256 conversion_failed = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2257 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2258 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2259 this_conversion_failed = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2260 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2261
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2262 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2263 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2264 if (! done)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2265 done_after = i+1;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2266
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2267 // 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
2268 done = true;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2269 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2270 }
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2271
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2272 if (done)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2273 is.setstate (std::ios::eofbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2274
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2275 // 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
2276 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2277 return (2 + no_conversions);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2278
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2279 if (conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2280 return (4 + no_conversions);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2281
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2282 return 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2283 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2284
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2285 void
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2286 textscan::parse_options (const octave_value_list& args,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2287 textscan_format_list& fmt_list)
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 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
2290 int n = last;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2291
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2292 if (n & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2293 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
2294
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2295 delim_len = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2296 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
2297 for (int i = 0; i < last; i += 2)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2298 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2299 if (! args(i).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2300 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
2301 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
2302
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2303 std::string param = args(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2304 std::transform (param.begin (), param.end (),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2305 param.begin (), ::tolower);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2306 if (param == "delimiter")
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 bool invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2309 if (args(i+1).is_string ())
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 invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2312 have_delims = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2313 delims = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2314 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2315 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2316 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2317 invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2318 delim_list = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2319 delim_table = " "; // non-empty, to flag non-default delim
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2320
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2321 // 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
2322 for (int j = 0; j < delim_list.numel (); j++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2323 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2324 if (! delim_list(j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2325 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2326 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2327 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2328 octave_idx_type len = delim_list(j).string_value ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2329 .length ();
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
2330 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
2331 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2332 }
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 if (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2335 error ("textscan: Delimiters must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2336 "cell array of strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2337 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2338 else if (param == "commentstyle")
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 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2341 { // check here for names like "C++", "C", "shell", ...?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2342 comment_style = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2343 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2344 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2345 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2346 comment_style = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2347 int len = comment_style.numel ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2348 if ((len >= 1 && ! comment_style (0).is_string ())
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2349 || (len >= 2 && ! comment_style (1).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2350 || (len >= 3))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2351 error ("textscan: CommentStyle must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2352 "cell array of one or two strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2353 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2354 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2355 error ("textscan: CommentStyle must be either a string"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2356 " 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
2357 args(i+1).class_name ().c_str ());
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 // 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
2360 // and which character do we look for?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2361 if (comment_style.numel () >= 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 comment_len = comment_style (0).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2364 comment_char = comment_style (0).string_value ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2365 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2366 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2367 else if (param == "treatasempty")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2368 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2369 bool invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2370 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2371 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2372 treat_as_empty = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2373 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
2374 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2375 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2376 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2377 treat_as_empty = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2378 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
2379 if (! treat_as_empty (j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2380 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2381 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2382 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2383 int k = treat_as_empty (j).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2384 if (k > treat_as_empty_len)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2385 treat_as_empty_len = k;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2386 }
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 if (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2389 error ("textscan: TreatAsEmpty must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2390 "cell array of one or two strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2391
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2392 // 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
2393 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2394 else if (param == "collectoutput")
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 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2397 collect_output = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2398 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2399 error ("textscan: CollectOutput must be logical or numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2400 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2401 else if (param == "emptyvalue")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2402 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2403 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2404 empty_value = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2405 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2406 error ("textscan: EmptyValue must be numeric, not <%s>",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2407 args(i+1).class_name ().c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2408 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2409 else if (param == "headerlines")
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 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2412 header_lines = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2413 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2414 error ("textscan: HeaderLines must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2415 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2416 else if (param == "bufsize")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2417 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2418 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2419 buffer_size = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2420 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2421 error ("textscan: BufSize must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2422 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2423 else if (param == "multipledelimsasone")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2424 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2425 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2426 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2427 if (args(i+1).bool_value ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2428 multiple_delims_as_one = true;
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 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2431 error ("textscan: MultipleDimsAsOne must be logical or numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2432 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2433 else if (param == "returnonerror")
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 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2436 return_on_error = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2437 else if (args(i+1).is_string ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2438 && args(i+1).string_value () == "continue")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2439 return_on_error = 2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2440 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2441 error ("textscan: ReturnOnError must be logical or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2442 "numeric, or 'continue'");
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 else if (param == "whitespace")
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).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2447 whitespace = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2448 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2449 error ("textscan: Whitespace must be a character string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2450 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2451 else if (param == "expchars")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2452 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2453 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2454 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2455 exp_chars = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2456 default_exp = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2457 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2458 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2459 error ("textscan: ExpChars must be a character string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2460 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2461 else if (param == "endofline")
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 bool valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2464 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2465 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2466 std::string s = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2467 if (args(i+1).is_sq_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2468 s = do_string_escapes (s);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2469 int l = s.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2470 if (l == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2471 eol1 = eol2 = -2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2472 else if (l == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2473 eol1 = eol2 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2474 else if (l == 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2475 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2476 eol1 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2477 eol2 = s.c_str ()[1];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2478 if (eol1 != '\r' || eol2 != '\n') // Why limit it?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2479 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2480 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2481 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2482 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2483 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2484 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2485 valid = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2486 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2487 error ("textscan: EndOfLine must be at most one character "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2488 "or '\\r\\n'");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2489 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2490 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2491 error ("textscan: Unrecognised option '%s'", param.c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2492 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2493
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2494 whitespace_table = std::string (256, '\0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2495 for (unsigned int i = 0; i < whitespace.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2496 whitespace_table[whitespace[i]] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2497
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2498 // For Matlab compatibility, add 0x20 to whitespace, unless
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2499 // whitespace is explicitly ignored.
21497
2d71bb0011a0 textscan: Use empty () rather than 'length () == 0' on strings
Mike Miller <mtmiller@octave.org>
parents: 21496
diff changeset
2500 if (! (whitespace.empty () && fmt_list.has_string))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2501 whitespace_table[' '] = '1';
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 // Create look-up table of delimiters, based on 'delimiter'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2504 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
2505 if (eol1 >= 0 && eol1 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2506 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
2507 if (eol2 >= 0 && eol2 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2508 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
2509 if (! have_delims)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2510 for (unsigned int i = 0; i < 256; i++)
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 if (isspace (i))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2513 delim_table[i] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2514 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2515 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2516 for (unsigned int i = 0; i < delims.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2517 delim_table[delims[i]] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2518 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2519
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2520 // 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
2521 // 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
2522
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2523 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2524 textscan::skip_whitespace (delimited_stream& is, bool EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2525 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2526 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2527 bool found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2528
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2529 do
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 found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2532 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
2533 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
2534 && ( ( (c1 == eol1 || c1 == eol2) && ++lines && ! EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2535 || isspace (c1)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2536 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2537 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2538 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2539 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2540 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2541
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2542 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
2543 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2544 // 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
2545 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2546 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2547
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2548 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
2549 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
2550 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
2551 .c_str (), look, comment_len-1))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2552 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2553 found_comment = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2554
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2555 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2556 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
2557 {
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2558 std::string eol (3, '\0');
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2559 eol[0] = eol1;
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2560 eol[1] = eol2;
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2561
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2562 scan_caret (is, eol, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2563 c1 = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2564 if (c1 == eol1 && eol1 != eol2 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2565 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2566 lines++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2567 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2568 else // matching pair
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2569 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2570 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
2571 // 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
2572 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
2573 std::string may_match ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2574 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2575 { // find sequence ending with last char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2576 scan_caret (is, last, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2577 is.get_undelim (); // (read last itself)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2578
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2579 may_match = may_match + dummy + last;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2580 int start = may_match.length () - end_c.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2581 if (start < 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2582 start = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2583 may_match = may_match.substr (start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2584 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2585 while (may_match != end_c && is && ! is.eof ());
21462
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2588 else // wasn't really a comment; restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2589 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2590 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2591 is.seekg (pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2592 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2593 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2594 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2595 while (found_comment);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2596
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2597 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2598 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2599 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2600 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2601
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2602 // 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
2603 // 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
2604 // 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
2605
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2606 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2607 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
2608 bool case_sensitive) const
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 // target strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2611 // 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
2612 // that matches.
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 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2615
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2616 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
2617 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
2618
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2619 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2620 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2621 // FIXME pos may be corrupted by is.read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2622
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2623 int i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2624 int (*compare)(const char *, const char *, size_t);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2625 compare = case_sensitive ? strncmp : strncasecmp;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2626
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2627 for (i = 0; i < targets.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2628 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2629 std::string s = targets (i).string_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2630 if (! (*compare) (s.c_str (), look, s.size ()))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2631 {
21496
5d69326e8cc5 textscan: Prefer std::string local variables to char arrays
Mike Miller <mtmiller@octave.org>
parents: 21494
diff changeset
2632 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
2633 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2634 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2635 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2636
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2637 if (i == targets.numel ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2638 i = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2639
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2640 return i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2641 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2642
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2643 // Skip delimiters -- multiple if MultipleDelimsAsOne specified.
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2644 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2645 textscan::skip_delim (delimited_stream& is)
21462
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 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
2648 if (delim_list.numel () == 0) // single character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2649 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2650 if (is_delim (c1) || c1 == eol1 || c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2651 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2652 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2653 if (c1 == eol1 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2654 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
2655
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2656 if (multiple_delims_as_one)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2657 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2658 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2659 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2660 // 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
2661 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
2662 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2663 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2664 || isspace (c1) || is_delim (c1)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2665 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2666 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2667 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2668 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2669 }
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2670 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2671 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2672 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2673 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2674 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2675 else // multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2676 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2677 int first_match;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2678
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2679 if (c1 == eol1 || c1 == eol2
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2680 || (-1 != (first_match = lookahead (is, delim_list, delim_len))))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2681 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2682 if (c1 == eol1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2683 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2684 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2685 if (is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2686 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2687 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2688 else if (c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2689 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2690 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2691 }
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 if (multiple_delims_as_one)
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 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2696 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2697 // 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
2698 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
2699 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2700 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2701 || -1 != lookahead (is, delim_list, delim_len)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2702 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2703 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2704 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2705 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2706 }
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 }
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2711 return c1;
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2714 // 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
2715 // 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
2716 // false (and set failbit).
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2717
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2718 bool
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2719 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
2720 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2721 // "false" -> treat EOL as normal space
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2722 // 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
2723 skip_whitespace (is, false);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2724
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2725 for (unsigned int i = 0; i < fmt.width; i++)
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 int ch = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2728 if (ch != fmt.text[i])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2729 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2730 if (ch != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2731 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2732 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2733 return false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2734 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2735 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2736 return true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2737 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2738
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2739 DEFUN (textscan, args, ,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2740 "-*- texinfo -*-\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2741 @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
2742 @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
2743 @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
2744 @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
2745 @deftypefnx {} {@var{C} =} textscan (@var{str}, @dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2746 @deftypefnx {} {[@var{C}, @var{position}] =} textscan (@dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2747 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
2748 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2749 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
2750 parsed according to @var{format}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2751 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
2752 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
2753 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
2754 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2755 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
2756 (such as whitespace) and literals.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2757 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
2758 by the options.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2759 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
2760 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
2761 but is otherwise ignored.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2762 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2763 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
2764 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
2765 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2766 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
2767 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
2768 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
2769 and so forth.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2770 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
2771 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
2772 (see below).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2773 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2774 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
2775 parsed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2776 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
2777 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
2778 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
2779 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
2780 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2781 The following specifiers are valid:\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 @table @code\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2784 @item %f\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2785 @itemx %f64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2786 @itemx %n\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2787 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
2788 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2789 @item %f32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2790 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
2791 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2792 @item %d\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2793 @itemx %d8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2794 @itemx %d16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2795 @itemx %d32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2796 @itemx %d64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2797 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
2798 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
2799 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2800 @item %u\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2801 @itemx %u8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2802 @itemx %u16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2803 @itemx %u32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2804 @itemx %u64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2805 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
2806 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
2807 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2808 @item %s\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2809 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
2810 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
2811 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2812 @item %q\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2813 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
2814 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
2815 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
2816 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
2817 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
2818 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
2819 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
2820 'He said \"Hello\"'.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2821 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2822 @item %c\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2823 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
2824 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
2825 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2826 @item %[...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2827 @itemx %[^...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2828 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
2829 characters between the brackets.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2830 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
2831 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
2832 (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
2833 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
2834 alphanumeric characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2835 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
2836 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
2837 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
2838 NOT listed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2839 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2840 @item %N...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2841 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
2842 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
2843 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
2844 a single character.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2845 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
2846 number of characters read;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2847 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
2848 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
2849 components individually.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2850 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
2851 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
2852 subsequent digits are skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2853 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
2854 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2855 @item %*...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2856 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
2857 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2858 @item literals\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2859 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
2860 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
2861 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
2862 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
2863 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2864 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2865 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
2866 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
2867 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2868 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
2869 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
2870 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
2871 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
2872 line of the input.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2873 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
2874 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2875 For example, the string\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2876 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2877 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2878 @group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2879 @var{str} = \"\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2880 Bunny Bugs 5.5\\n\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2881 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
2882 Penguin Tux 6\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2883 @end group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2884 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2885 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2886 @noindent\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2887 can be read using\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2888 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2889 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2890 @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
2891 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2892 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2893 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
2894 number of items read:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2895 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2896 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2897 @item -1\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2898 (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
2899 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2900 @item N\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2901 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
2902 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
2903 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
2904 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
2905 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
2906 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
2907 @end table\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 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
2910 The following properties are recognized:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2911 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2912 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2913 @item @qcode{\"BufSize\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2914 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
2915 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
2916 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
2917 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
2918 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2919 @item @qcode{\"CollectOutput\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2920 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
2921 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
2922 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
2923 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2924 @item @qcode{\"CommentStyle\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2925 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
2926 @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
2927 (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
2928 (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
2929 second strings.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2930 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
2931 delimiters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2932 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2933 @item @qcode{\"Delimiter\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2934 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
2935 split @var{str} into words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2936 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
2937 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
2938 (default value = any whitespace.)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2939 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2940 @item @qcode{\"EmptyValue\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2941 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
2942 The default is NaN@.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2943 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
2944 then default is zero.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2945 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2946 @item @qcode{\"EndOfLine\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2947 @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
2948 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
2949 @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
2950 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
2951 @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
2952 @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
2953 newline.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2954 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
2955 used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2956 @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
2957 @c to whitespace.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2958 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2959 @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
2960 @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
2961 @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
2962 @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
2963 @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
2964 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2965 @item @qcode{\"HeaderLines\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2966 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
2967 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
2968 lines of any type.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2969 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2970 @item @qcode{\"MultipleDelimsAsOne\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2971 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
2972 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
2973 as a single delimiter.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2974 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
2975 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
2976 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
2977 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
2978 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
2979 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2980 @item @qcode{\"TreatAsEmpty\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2981 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
2982 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
2983 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2984 @item @qcode{\"ReturnOnError\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2985 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
2986 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
2987 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
2988 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
2989 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
2990 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2991 @item @qcode{\"Whitespace\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2992 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
2993 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
2994 @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
2995 @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
2996 @qcode{\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2997 @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
2998 (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
2999 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
3000 always part of whitespace.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3001 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3002 @end table\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 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
3005 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
3006 textscan's behavior depends on\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3007 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
3008 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
3009 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3010 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3011 @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
3012 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
3013 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
3014 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3015 @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
3016 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
3017 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3018 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3019 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3020 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
3021 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
3022 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3023 @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
3024 @end deftypefn")
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3025 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3026 octave_value_list retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3027
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3028 if (args.length () < 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3029 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
3030
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3031 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
3032
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3033 textscan tscanner;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3034
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3035 if (args(0).is_string ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3036 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3037 std::istringstream is (args(0).string_value ());
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3038
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3039 retval(0) = tscanner.scan (is, tmp_args);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3040
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3041 std::ios::iostate state = is.rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3042 is.clear ();
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3043 retval(1) = octave_value (static_cast<long> (is.tellg ()));
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3044 is.setstate (state);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3045 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3046 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3047 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3048 octave_stream os = octave_stream_list::lookup (args(0), "textscan");
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3049 std::istream *isp = os.input_stream ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3050 if (! isp)
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3051 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
3052
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3053 retval(0) = tscanner.scan (*isp, tmp_args);
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3054
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3055 // FIXME -- warn if stream is not opened in binary mode?
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3056 std::ios::iostate state = os.input_stream ()->rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3057 os.input_stream ()->clear ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3058 retval(1) = os.tell ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3059 os.input_stream ()->setstate (state);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3060 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3061
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3062 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3063 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3064
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3065 /*
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3066 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3067 %! 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
3068 %! fmtstr = "%f %d %f %s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3069 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3070 %! assert (isequal (c{1}, [1;5]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3071 %! assert (length (c{1}), 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3072 %! assert (iscellstr (c{4}));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3073 %! assert (isequal (c{3}, [3; -Inf]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3074
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3075 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3076 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3077 %! b = [b; 8*b/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3078 %! 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
3079 %! fmt = "%f miles/hr = %f kilometers/hr";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3080 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3081 %! assert (c{1}, b(1,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3082 %! assert (c{2}, b(2,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3083
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3084 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3085 %! 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
3086 %! a = textscan (str, "%d %n %f %s %n", "delimiter", ",",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3087 %! "treatAsEmpty", {"NA", "na", "-"},"commentStyle", "//");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3088 %! assert (a{1}, int32 ([13; 36]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3089 %! assert (a{2}, [NaN; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3090 %! assert (a{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3091 %! assert (a{4}, {"str1"; "str3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3092 %! assert (a{5}, [-25; 6]);
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 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3095 %! str = "Km:10 = hhhBjjj miles16hour\r\n";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3096 %! str = [str "Km:15 = hhhJjjj miles241hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3097 %! str = [str "Km:2 = hhhRjjj miles3hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3098 %! str = [str "Km:25 = hhhZ\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3099 %! fmt = "Km:%d = hhh%1sjjj miles%dhour";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3100 %! a = textscan (str, fmt, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3101 %! assert (a{1}', int32 ([10 15 2 25]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3102 %! assert (a{2}', {'B' 'J' 'R' 'Z'});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3103 %! assert (a{3}', int32 ([16 241 3 0]));
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 with default endofline parameter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3106 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3107 %! c = textscan ("L1\nL2", "%s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3108 %! assert (c{:}, {"L1"; "L2"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3109
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3110 ## 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
3111 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3112 %! c = textscan ("L1\nL2", "%s", "endofline", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3113 %! assert (int8 ([c{:}{:}]), int8 ([ 76, 49, 10, 76, 50 ]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3114
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3115 ### 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
3116 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3117 #%! ## 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
3118 #%! str = "Text1Text2Text\nTextText4Text\nText57Text";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3119 #%! c = textscan (str, "Text%*dText%dText");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3120 #%! assert (c{1}, int32 ([2; 4; 0]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3121
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3122 ## CollectOutput test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3123 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3124 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3125 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3126 %! 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
3127 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3128 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3129 %! assert (size (c{3}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3130 %! assert (size (c{2}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3131
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3132 ## CollectOutput test with uneven column length files
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3133 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3134 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3135 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3136 %! 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
3137 %! str = [str "110 miles/hr"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3138 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3139 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3140 %! assert (size (c{1}), [11, 1]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3141 %! assert (size (c{3}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3142 %! assert (size (c{2}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3143 %! assert (c{3}(end), NaN);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3144 %! assert (c{2}{11, 1}, "/hr");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3145 %! assert (isempty (c{2}{11, 2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3146
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3147 ## Double quoted string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3148 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3149 %! str = 'First "the second called ""the middle""" third';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3150 %! fmt = "%q";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3151 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3152 %! 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
3153
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3154 ## Arbitrary character
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3155 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3156 %! 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
3157 %! assert (c{1}, {"a"; "ond"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3158 %! assert (c{2}, {"f"; "t"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3159 %! assert (c{3}, {"irst, \n sec"; "hird"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3160
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3161 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3162 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3163 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3164 %! c = textscan (str, "%4d %4d", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3165 %! assert (c, {[12 34; 1234 5678; 9 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3166
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3167 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3168 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3169 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3170 %! c = textscan (str, "%4f %f", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3171 %! assert (c, {[12 34; 1234 56789; 7 NaN]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3172
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3173 ## Ignore trailing delimiter, but use leading one
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3174 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3175 %! 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
3176 %! c = textscan (str, "%10.2f %f", "delimiter", ",", "collectOutput", 1,
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3177 %! "expChars", "e|");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3178 %! 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
3179
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3180 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3181 %! ## Multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3182 %! str = "99end2 space88gap 4564";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3183 %! 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
3184 %! assert (c{1}, int32 ([99; 88]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3185 %! assert (c{2}, {"2 "; "4564"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3186
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3187 ### Delimiters as part of literals, and following literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3188 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3189 #%! str = "12 R&D & 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3190 #%! 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
3191 #%! assert (c, {[12 -99; 7 -99]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3192 #
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3193 ### Delimiters as part of literals, and before literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3194 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3195 #%! str = "12 & R&D 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3196 #%! 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
3197 #%! assert (c, {[12 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3198
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3199 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3200 %! ## 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
3201 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3202 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3203 %! fprintf (fid, "1\n2\n3\n4\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3204 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3205 %! c = textscan (fid, "%f %f", 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3206 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3207 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3208 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3209 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3210 %! assert (!E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3211
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3212 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3213 %! ## 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
3214 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3215 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3216 %! 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
3217 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3218 %! c = textscan (fid, "%f %f", 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3219 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3220 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3221 %! assert (c, {[1;3], [2;4]})
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 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3224 %! ## Check number of lines read, with multiple delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3225 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3226 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3227 %! 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
3228 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3229 %! c = textscan (fid, "%f %f", 4, "delimiter", "-", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3230 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3231 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3232 %! assert (c, {[1;3], [2;4]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3233
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3234 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3235 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3236 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3237 %! fid = fopen (f, "w+");
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3238 %! 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
3239 %! fprintf (fid, str);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3240 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3241 %! c = textscan (fid, "%f %f %f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3242 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3243 %! d = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3244 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3245 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3246 %! unlink (f);
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3247 %! u = textscan (str, "%f %f %f");
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3248 %! v = textscan (str, "%f %f %f", "ReturnOnError", 1);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3249 %! assert (c, {[1;4], [2;NaN], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3250 %! 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
3251 %! 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
3252 %! assert (v, {[1;4], [2], [3]})
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3253
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3254 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3255 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3256 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3257 %! fid = fopen (f, "w+");
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3258 %! 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
3259 %! fprintf (fid, str);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3260 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3261 %! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3262 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3263 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3264 %! unlink (f);
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3265 %! 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
3266 %! 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
3267 %! assert (u, {[1;4], 2, 3})
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3268
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3269 %!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
3270
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3271 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3272 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3273 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3274 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3275 %! fprintf (fid, "1 s 3\n4 5 6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3276 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3277 %! c = textscan (fid, "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3278 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3279 %! d = textscan (fid, "", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3280 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3281 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3282 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3283 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3284 %! assert (d, {1})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3285
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3286 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3287 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3288 %! 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
3289 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3290
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3291 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3292 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3293 %! 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
3294 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3295
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3296 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3297 %! ## Check ReturnOnError in first column
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3298 %! 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
3299 %! assert (c, {1, 2, 3})
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 input validation
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3302 %!error textscan ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3303 %!error textscan (single (40))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3304 %!error textscan ({40})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3305 %!error <must be a string> textscan ("Hello World", 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3306 #%!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
3307 %!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
3308 %!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
3309 %!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
3310
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3311 ## Test incomplete first data line
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3312 %! R = textscan (['Empty1' char(10)], 'Empty%d %f');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3313 %! assert (R{1}, int32 (1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3314 %! assert (isempty (R{2}), true);
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 ## bug #37023
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3317 %!test
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3318 %! 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
3319 %! assert (data{1}, [1; 2], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3320 %! assert (data{2}, [1; 3], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3321
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3322 ## Whitespace test (bug #37333) using delimiter ";"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3323 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3324 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3325 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3326 %! tc{1, end+1} = "C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3327 %! tc{1, end+1} = " C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3328 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3329 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3330 %! c = textscan (string, "%s", "delimiter", ";");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3331 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3332 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3333 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3334 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3335 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3336 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3337 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3338
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3339 ## Whitespace test (bug #37333), adding multipleDelimsAsOne true arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3340 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3341 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3342 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3343 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3344 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3345 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3346 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3347 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3348 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3349 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3350 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3351 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3352 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3353 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3354 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3355
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3356 ## Whitespace test (bug #37333), adding multipleDelimsAsOne false arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3357 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3358 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3359 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3360 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3361 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3362 %! tc{1, end+1} = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3363 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3364 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3365 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 0);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3366 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3367 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3368 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3369 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3370 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3371 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3372 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3373
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3374 ## Whitespace test (bug #37333) whitespace "" arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3375 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3376 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3377 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3378 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3379 %! tc{1, end+1} = "C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3380 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3381 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3382 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3383 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3384 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3385 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3386 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3387 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3388 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3389
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3390 ## Whitespace test (bug #37333), whitespace " " arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3391 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3392 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3393 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3394 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3395 %! tc{1, end+1} = "C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3396 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3397 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3398 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3399 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3400 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3401 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3402 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3403 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3404 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3405 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3406
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3407 ## 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
3408 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3409 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3410 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3411 %! 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
3412 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3413 %! A = textscan (fid, "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3414 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3415 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3416 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3417 %! assert (A{1}, [1 ; 5], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3418 %! assert (A{2}, [2 ; 6], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3419 %! assert (A{3}, [3 ; 7], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3420 %! assert (A{4}, [4 ; 8], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3421 %! assert (E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3422
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3423 ## 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
3424 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3425 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3426 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3427 %! fprintf (fid, " ,2,,4\n5,6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3428 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3429 %! A = textscan (fid, "", "delimiter", ",", "EmptyValue", 999, "CollectOutput" , 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3430 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3431 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3432 %! 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
3433
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3434 ## Error message tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3435
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3436 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3437 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3438 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3439 %! msg1 = "textscan: 1 parameters given, but only 0 values";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3440 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3441 %! A = textscan (fid, "", "headerlines");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3442 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3443 %! assert (!feof (fid));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3444 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3445 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3446 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3447
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3448 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3449 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3450 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3451 %! msg1 = "textscan: HeaderLines must be numeric";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3452 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3453 %! A = textscan (fid, "", "headerlines", "hh");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3454 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3455 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3456 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3457 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3458
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3459 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3460 %! ## Skip headerlines
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3461 %! 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
3462 %! assert (A, {[1 2; 3 4]});
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 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3465 %! ## Skip headerlines with non-default EOL
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3466 %! 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
3467 %! assert (A, {[3 4]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3468
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3469 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3470 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3471 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3472 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3473 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3474 %! 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
3475 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3476 %! A = textscan (fid, "%f", "EndOfLine", "\n\r");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3477 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3478 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3479 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3480 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3481
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3482 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3483 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3484 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3485 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3486 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3487 %! 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
3488 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3489 %! A = textscan (fid, "%f", "EndOfLine", 33);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3490 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3491 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3492 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3493 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3494
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3495 ## Bug #41824
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3496 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3497 %! assert (textscan ("123", "", "whitespace", " "){:}, 123);
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 ## Bug #42343-1, just test supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3500 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3501 %! assert (textscan (",NaN", "", "delimiter", "," ,"emptyValue" ,Inf), {Inf, NaN});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3502
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3503 ## Bug #42343-2, test padding with supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3504 %!test
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3505 %! 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
3506 %! 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
3507
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3508 ## Bug #42528
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3509 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3510 %! assert (textscan ("1i", ""){1}, 0+1i);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3511 %! 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
3512
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3513 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3514 %! ## TreatAsEmpty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3515 %! 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
3516 %! assert (C{3}(1), int32 (3));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3517 %! assert (C{4}(1), NaN);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3518
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3519 ## MultipleDelimsAsOne
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3520 %!test
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3521 %! 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
3522 %! 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
3523 %! assert (C{1}', [11, 21, 33]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3524 %! assert (C{2}', [12, 23, 34]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3525 %! assert (C{3}', [13, 24, 35]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3526 %! assert (C{4}', [15, 25, NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3527
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3528 ## Bug #44750
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3529 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3530 %! assert (textscan ("/home/foo/", "%s", "delimiter", "/", "MultipleDelimsAsOne", 1){1}, ...
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3531 %! {"home"; "foo"});
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 ### Allow cuddling %sliteral but warn it is ambiguous
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3534 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3535 #%! C = textscan ("abcxyz51\nxyz83\n##xyz101", "%s xyz %d");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3536 #%! assert (C{1}([1 3]), {"abc"; "##"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3537 #%! assert (isempty (C{1}{2}), true);
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3538 #%! assert (C{2}, int32 ([51; 83; 101]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3539 ### Literals are not delimiters.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3540
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3541 ## 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
3542 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3543 %! 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
3544
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3545 ## Test various forms of string format specifiers (bug #45712)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3546 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3547 %! 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
3548 %! 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
3549 %! assert (C, {14, {"1 z"}, {"3 z"}, 11});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3550
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3551 %% Bit width, fixed width conv. specifiers
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3552 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3553 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3554 %! str2 = [str2 str2 str2 str2 str2 str2 str2 str2];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3555 %! 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
3556 %! 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
3557 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3558 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3559 %! assert (C{2}, uint16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3560 %! assert (C{3}, uint32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3561 %! assert (C{4}, uint64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3562 %! assert (C{5}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3563 %! assert (C{6}, int16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3564 %! assert (C{7}, int32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3565 %! assert (C{8}, int64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3566 %! assert (C{9}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3567 %! assert (C{10}, double (1234567890.123), 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3568 %! assert (C{11}, single (12345.68), 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3569 %! assert (C{12}, double (12345.68), 1e-11);
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 %% 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
3572 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3573 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3574 %! str2 = [str2 str2 "123456789.01234"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3575 %! pttrn = "%3u8 %5u16 %10u32 %3d8 %5d16 %10d32 %9f32 %9f";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3576 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3577 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3578 %! assert (C{2}, uint16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3579 %! assert (C{3}, uint32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3580 %! assert (C{4}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3581 %! assert (C{5}, int16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3582 %! assert (C{6}, int32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3583 %! assert (C{7}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3584 %! assert (C{8}, double (0.01234), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3585
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3586 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3587 %! C = textscan ("123.123", "%2f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3588 %! assert (C{1}, 12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3589 %! assert (C{2}, 3.1, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3590 %! assert (C{3}, 23);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3591
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3592 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3593 %! C = textscan ("123.123", "%3f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3594 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3595 %! assert (C{2}, 0.12, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3596 %! assert (C{3}, 3);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3597
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3598 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3599 %! C = textscan ("123.123", "%4f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3600 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3601 %! assert (C{2}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3602
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3603 %% field width interrupts exponent. (Matlab incorrectly gives [12, 2e12])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3604 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3605 %! assert (textscan ("12e12", "%4f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3606 %! assert (textscan ("12e+12", "%5f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3607 %! assert (textscan ("125e-12","%6f"), {[12.5; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3608
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3609 %% %[] tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3610 %% Plain [..] and *[..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3611 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3612 %! ar = "abcdefguvwxAny\nacegxyzTrailing\nJunk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3613 %! C = textscan (ar, "%[abcdefg] %*[uvwxyz] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3614 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3615 %! assert (C{2}, {"Any"; "Trailing"; "Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3616
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3617 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3618 %! 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
3619
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3620 %% [^..] and *[^..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3621 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3622 %! br = "abcdefguvwx1Any\nacegxyz2Trailing\n3Junk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3623 %! C = textscan (br, "%[abcdefg] %*[^0123456789] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3624 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3625 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3626
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3627 %% [..] and [^..] containing delimiters
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 %! 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
3630 %! C = textscan (cr, "%[ abcdefg] %*[^0123456789] %s", "delimiter", " \n", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3631 %! assert (C{1}, {"ab cd efg"; "a ce g"; " "});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3632 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3633
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3634 %% Bug #36464
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3635 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3636 %! 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
3637
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3638 %% test %[]] and %[^]]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3639 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3640 %! assert (textscan ('345]', "%*[123456]%[]]"){1}{1}, "]");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3641 %! assert (textscan ('345]', "%*[^]]%s"){1}{1}, "]");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3642
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3643 %% Test that "-i" checks the next two characters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3644 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3645 %! 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
3646 %! assert (C, {-i, -i, {"n"}, -Inf, -Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3647
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3648 %% Again for "+i", this time with custom parser
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3649 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3650 %! 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
3651 %! assert (C, {i, i, {"n"}, Inf, Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3652
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3653 %% Check single quoted format interprets control sequences
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3654 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3655 %! 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
3656 %! assert (C, {1, {"2"}, 3, 4});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3657
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3658 %% Check overflow and underflow of integer types
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3659 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3660 %! a = "-1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3661 %! b = "1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3662 %! fmt = "%d8 %d16 %d32 %d64 %u8 %u16 %u32 %u64 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3663 %! 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
3664 %! assert (C{1}, int8 ([-128; 127]));
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3665 %! assert (C{2}, int16 ([-32768; 32767]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3666 %! assert (C{3}, int32 ([-2147483648; 2147483647]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3667 %! assert (C{4}, int64 ([-9223372036854775808; 9223372036854775807]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3668 %! assert (C{5}, uint8 ([0; 255]));
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3669 %! assert (C{6}, uint16 ([0; 65535]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3670 %! assert (C{7}, uint32 ([0; 4294967295]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3671 %! assert (C{8}, uint64 ([0; 18446744073709551615]));
21462
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 %% 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
3674 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3675 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3676 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3677 %! 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
3678 %! 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
3679 %! 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
3680 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3681 %! 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
3682 %! %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
3683 %! assert (C{2}, {"Level1";"Level2";"Level3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3684 %! 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
3685 %! assert (C{4}, [int8(45);int8(60);int8(12)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3686 %! assert (C{5}, [uint32(4294967295);uint32(4294967295);uint32(200000)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3687 %! assert (C{6}, [inf;-inf;10]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3688 %! assert (C{7}, [NaN;0.001;100], eps);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3689 %! assert (C{8}, {"Yes";"No";"No"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3690 %! 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
3691 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3692 %! 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
3693 %! assert (C{2}, [int32(1);int32(2);int32(3)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3694 %! 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
3695 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3696 %! C = textscan (fid,'%s %*[^\n]');
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3697 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3698 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3699 %! 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
3700
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3701 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3702 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3703 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3704 %! fprintf (fid,"1, 2, 3, 4, , 6\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3705 %! fprintf (fid,"7, 8, 9, , 11, 12\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3706 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3707 %! 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
3708 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3709 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3710 %! assert (C{4}, [4; -Inf]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3711 %! assert (C{5}, uint8 ([0; 11]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3712
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3713 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3714 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3715 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3716 %! fprintf (fid,"abc, 2, NA, 3, 4\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3717 %! fprintf (fid,"// Comment Here\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3718 %! fprintf (fid,"def, na, 5, 6, 7\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3719 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3720 %! 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
3721 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3722 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3723 %! assert (C{1}, {"abc";"def"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3724 %! assert (C{2}, [2; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3725 %! assert (C{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3726 %! assert (C{4}, [3; 6]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3727 %! assert (C{5}, [4; 7]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3728
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3729 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3730 %!## Test start of comment as string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3731 %! c = textscan ("1 / 2 // 3", "%n %s %u8", "CommentStyle", {"//"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3732 %! assert (c, {1, "/", 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3733 */
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3734
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3735 // 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
3736 #if 0
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3737 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3738 %!## Test unfinished comment
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3739 %! c = textscan ("1 2 /* half comment", "%n %u8", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3740 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3741
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3742 ## Test reading from a real file
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 %! d = rand (1, 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3747 %! fprintf (fid, " %f %f /* comment */ %f %f ", d);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3748 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3749 %! A = textscan (fid, "%f %f", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3750 %! E = feof (fid);
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 (A{1}, [d(1); d(3)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3754 %! assert (A{2}, [d(2); d(4)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3755 %! assert (E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3756 #endif