annotate libinterp/corefcn/textscan.cc @ 21493:a41e48ef2b99

textscan: Don't pad columns with ReturnOnError = 1 * textscan.cc (textscan::do_scan): Do not check for EOL at EOF if error occurs. Add BIST tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Fri, 18 Mar 2016 19:58:37 -0700
parents db67dc54fa2c
children 8e9833b8791d
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 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
332 if (strchr (delims.c_str (), *last))
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
456 textscan_format_elt (const char *txt = 0, int w = 0, int p = -1,
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 ())
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
459 : text (strsave (txt)), width (w), prec (p), bitwidth (bw),
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)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
465 : text (strsave (e.text)), width (e.width), 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
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 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
474 text = strsave (e.text);
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 ~textscan_format_elt (void) { delete [] text; }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
488
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
489 // The C-style format string.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
490 const char *text;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
491
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
492 // The maximum field width.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
493 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
494
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
495 // 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
496 // 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
497 int prec;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
498
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
499 // 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
500 // 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
501 int bitwidth;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
502
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
503 // 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
504 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
505
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
506 // 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
507 // -- `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
508 char type;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
509
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
510 // TRUE if 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
511 bool discard;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
512
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
513 // 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
514 bool numeric;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
515 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
516
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
517 class textscan;
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 // 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
520
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
521 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
522 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
523 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
524 public:
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 (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
527
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
528 ~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
529
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
530 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
531
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
532 // 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
533 // 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
534 // 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
535 // last conversion.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
536
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
537 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
538
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
539 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
540 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
541 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
542 return current ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
543 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
544
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
545 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
546 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
547 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
548 }
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 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
551 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
552 curr_idx++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
553
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
554 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
555 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
556 if (cycle)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
557 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
558 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
559 return 0;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
560 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
561
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
562 return current ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
563 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
564
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
565 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
566
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
567 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
568
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
569 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
570
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
571 // 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
572 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
573
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
574 // 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
575 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
576
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
577 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
578
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
579 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
580
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
581 private:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
582
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
583 // 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
584 // 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
585 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
586
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
587 // 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
588 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
589
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
590 // List 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
591 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
592
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
593 // 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
594 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
595
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
596 // Temporary buffer.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
597 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
598
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
599 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
600 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
601 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
602 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
603
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
604 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
605
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
606 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
607 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
608 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
609 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
610 // No copying!
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 (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 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
615 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
616
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
617
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
618 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
619 : 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
620 fmt_elts (), buf (0)
21467
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 n = s.length ();
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 size_t i = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
625
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
626 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
627 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
628 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
629 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
630 char type = '\0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
631
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
632 bool have_more = true;
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 if (s.length () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
635 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
636 buf = new std::ostringstream ("%f");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
637 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
638 type = 'f';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
639 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
640 discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
641 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
642 set_from_first = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
643 nconv = 1;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
644 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
645 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
646 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
647 set_from_first = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
648
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
649 while (i < n)
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 have_more = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
652
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
653 if (! buf)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
654 buf = new std::ostringstream ();
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 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
657 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
658 // Process percent-escape conversion type.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
659
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
660 process_conversion (s, i, n);
21467
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 have_more = (buf != 0);
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 else if (isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
665 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
666 while (++i < n && isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
667 /* skip whitespace */;
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 have_more = false;
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 else
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 type = textscan_format_elt::literal_conversion;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
674
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
675 width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
676 prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
677 bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
678 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
679
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
680 while (i < n && ! isspace (s[i])
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
681 && (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
682 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
683 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
684 i++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
685 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
686 width++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
687 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
688
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
689 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
690 discard, type);
21467
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 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
693 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
694
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
695 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
696 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
697 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
698 break;
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
702
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
703 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
704 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
705
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
706 delete buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
707 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
708
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
709 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
710 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
711 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
712
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
713 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
714 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
715 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
716 delete elt;
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
719
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
720 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
721 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
722 int bitwidth, octave_value val_type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
723 bool discard, char type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
724 const std::string& char_class)
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 if (buf)
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 std::string text = buf->str ();
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 if (! text.empty ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
731 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
732 textscan_format_elt *elt
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
733 = new textscan_format_elt (text.c_str (), width, prec, bitwidth,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
734 discard, type, char_class);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
735
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
736 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
737 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
738
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
739 fmt_elts.push_back (elt);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
740 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
741
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
742 delete buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
743 buf = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
744 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
745 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
746
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
747 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
748 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
749 size_t n)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
750 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
751 unsigned width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
752 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
753 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
754 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
755 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
756 char type = '\0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
757
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
758 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
759
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
760 bool have_width = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
761
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
762 while (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
763 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
764 switch (s[i])
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
765 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
766 case '*':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
767 if (discard)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
768 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
769 else
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 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
772 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
773 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
774 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
775
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
776 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
777 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
778 if (have_width)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
779 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
780 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
781 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
782 char c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
783 width = width * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
784 have_width = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
785 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
786 while (i < n && isdigit (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
787 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
788 c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
789 width = width * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
790 *buf << c;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
793 if (i < n && s[i] == '.')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
794 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
795 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
796 prec = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
797 while (i < n && isdigit (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
798 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
799 c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
800 prec = prec * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
801 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
802 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
803 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
804 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
805 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
806
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
807 case 'd': case 'u':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
808 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
809 bool done = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
810 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
811 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
812 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
813 if (s[i] == '8')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
814 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
815 bitwidth = 8;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
816 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
817 val_type = octave_value (int8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
818 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
819 val_type = octave_value (uint8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
820 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
821 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
822 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
823 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
824 bitwidth = 16;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
825 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
826 val_type = octave_value (int16NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
827 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
828 val_type = octave_value (uint16NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
829 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
830 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
831 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
832 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
833 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
834 done = false; // use default size below
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
835 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
836 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
837 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
838 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
839 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
840 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
841 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
842 val_type = octave_value (int64NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
843 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
844 val_type = octave_value (uint64NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
845 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
846 *buf << s[i++];
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
851 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
852 done = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
853
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
854 if (! done)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
855 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
856 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
857 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
858 val_type = octave_value (int32NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
859 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
860 val_type = octave_value (uint32NDArray ());
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 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
863 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
864
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
865 case 'f':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
866 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
867 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
868 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
869 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
870 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
871 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
872 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
873 val_type = octave_value (FloatNDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
874 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
875 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
876 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
877 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
878 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
879 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
880 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
881 *buf << s[i++];
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
886 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
887 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
888 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
889
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
890 case 'n':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
891 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
892 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
893 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
894 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
895
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
896 case 's': case 'q': case '[': case 'c':
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
897 if (! discard)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
898 val_type = octave_value (Cell ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
899 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
900 has_string = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
901 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
902
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
903 fini:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
904 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
905 if (! have_width)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
906 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
907 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
908 width = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
909 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
910 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
911 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
912
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
913 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
914 discard, type) == 0)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
915 return;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
916 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
917 break;
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 default:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
920 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
921 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
922
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
923 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
924 break;
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 nconv = -1;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
930 // Parse [...] and [^...]
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
931 //
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
932 // 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
933 // 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
934 // 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
935 // 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
936 // pattern.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
937 //
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
938 // 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
939 // 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
940 // The latter is efficient for [^...] patterns.
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 static std::string
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
943 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
944 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
945 int len = pattern.length ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
946 if (len == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
947 return "";
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 std::string retval (256, '\0');
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
950 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
951
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
952 int in = 0, out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
953 unsigned char ch, prev = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
954 bool flip = false;
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 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
957 if (ch == '^')
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 in++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
960 flip = true;
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 mask[pattern[in]] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
963 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
964
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
965 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
966 bool prev_prev_was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
967 for (; in < len; in++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
968 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
969 bool was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
970 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
971 if (ch == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
972 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
973
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
974 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
975 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
976 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
977 if (start_of_range < ch
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
978 && ((isupper (ch) && isupper (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
979 || (islower (ch) && islower (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
980 || (isdigit (ch) && isdigit (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
981 || mask['-'] > 1)) // not the first '-'
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
982 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
983 was_range = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
984 out--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
985 mask['-']--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
986 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
987 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
988 if (mask[i] == '\0')
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 mask[i] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
991 retval[out++] = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
992 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
993 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
994 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
995 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
996 if (! was_range)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
997 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
998 if (mask[ch]++ == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
999 retval[out++] = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1000 else if (ch != '-')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1001 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1002 "textscan: [...] contains two '%c's", ch);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1003
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1004 if (prev == '-' && mask['-'] >= 2)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1005 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1006 "textscan: [...] contains two '-'s "
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1007 "outside range expressions");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1008 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1009 prev = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1010 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
1011 prev_was_range = was_range;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1012 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1013
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1014 if (flip) // [^...]
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1015 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1016 out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1017 for (int i = 0; i < 256; i++)
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1018 if (! mask[i])
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1019 retval[out++] = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1020 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1021
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1022 retval.resize (out);
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 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1025 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1026
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1027 int
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1028 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
1029 size_t n, unsigned int& width,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1030 int& prec, int& bitwidth,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1031 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
1032 char& type)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1033 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1034 int retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1035
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1036 std::string char_class;
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 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
1039 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
1040
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 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1044 if (type == '[')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1045 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1046 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1047 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1048 beg_idx = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1049
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1050 if (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 type = '^';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1053 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1054
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1055 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1056 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1057 beg_idx = 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 if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1060 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1061 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1062 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1063 else if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1064 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1065 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1066
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1067 while (i < n && s[i] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1068 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1069
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1070 if (i < n && 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 end_idx = i-1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1073 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1074 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1075
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1076 if (s[i-1] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1077 retval = nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1078 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1079 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1080
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1081 if (nconv >= 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1082 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1083 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
1084 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
1085 end_idx - beg_idx + 1));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1086
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1087 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
1088 char_class);
21462
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1091 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1092 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1093
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1094 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1095 textscan_format_list::printme (void) const
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1096 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1097 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
1098
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1099 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
1100 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1101 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
1102
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1103 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1104 << "width: " << elt->width << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1105 << "digits " << elt->prec << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1106 << "bitwidth: " << elt->bitwidth << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1107 << "discard: " << elt->discard << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1108 << "type: ";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1109
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1110 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
1111 std::cerr << "literal text\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1112 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
1113 std::cerr << "whitespace\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1114 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1115 std::cerr << elt->type << "\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1116
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1117 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1118 << "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
1119 << "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
1120 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1121 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1122
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1123 // 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
1124 // 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
1125
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1126 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1127 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
1128 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1129 // 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
1130 std::string first_line (20, ' ');
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1131
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1132 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
1133
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1134 if (first_line.length () > 0
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1135 && 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
1136 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
1137
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1138 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
1139 delimited_stream ds (strstr, is);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1140
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1141 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
1142 Complex val;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1143 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1144 nconv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1145 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
1146 int retval = 0;
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 // 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
1149 while (! ds.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1150 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1151 bool already_skipped_delim = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1152 ts.skip_whitespace (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1153 ds.progress_benchmark ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1154 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
1155 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
1156 if (ds.fail ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1157 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1158 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
1159
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1160 if (ds.eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1161 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1162
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1163 // 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
1164 // 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
1165 // return with an error status.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1166 if (ts.return_on_error < 2)
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 ts.skip_delim (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1169 if (ds.no_progress ())
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 retval = 4;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1172 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1173 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1174 already_skipped_delim = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1175 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1176 else // skip offending field
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::ios::iostate state = ds.rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1179 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
1180
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1181 std::string dummy;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1182 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
1183 ts.scan_string (ds, fe, dummy);
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 progress = (dummy.length ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1186 ds.setstate (state);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1187 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1188
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1189 val = ts.empty_value.scalar_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1190
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1191 if (! --max_empty)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1192 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1193 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1194
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1195 if (val.imag () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1196 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
1197 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1198 val_type = octave_value (ComplexNDArray (dv, val));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1199
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1200 output_container.push_back (val_type);
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 (! already_skipped_delim)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1203 ts.skip_delim (ds);
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 if (! progress && ds.no_progress ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1206 break;
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 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1209 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1210
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1211 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
1212
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1213 // 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
1214 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
1215 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
1216
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1217 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
1218 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1219
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1220 // 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
1221
21488
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1222 static Cell
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1223 init_inf_nan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1224 {
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1225 Cell retval (dim_vector (1, 2));
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 retval(0) = Cell (octave_value ("inf"));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1228 retval(1) = Cell (octave_value ("nan"));
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 return retval;
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1231 }
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1232
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1233 textscan::textscan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1234 : buf (), whitespace_table (), delim_table (), delims (),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1235 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
1236 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
1237 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
1238 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
1239 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
1240 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
1241 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
1242 numeric_delim (false), lines (0)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1243 { }
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1244
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1245 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1246 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
1247 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1248 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
1249 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
1250
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1251 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
1252 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
1253 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
1254 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1255 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
1256
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1257 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
1258 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
1259
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1260 params++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1261 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1262 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1263 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
1264 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
1265
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1266 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
1267
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1268 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
1269 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1270 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
1271 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1272 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
1273
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1274 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
1275 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
1276 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
1277
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1278 params++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1279 }
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1282 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
1283
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1284 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
1285
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1286 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
1287
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1288 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
1289 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1290
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1291 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1292 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
1293 octave_idx_type ntimes)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1294 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1295 octave_value retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1296
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1297 if (fmt_list.num_conversions () == -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1298 error ("textscan: invalid format specified");
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1299
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1300 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1301 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
1302
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1303 // skip the first header_lines
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1304 std::string dummy;
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1305 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
1306 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
1307
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1308 // 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
1309
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1310 // 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
1311 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
1312 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
1313
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1314 // 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
1315 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
1316 if (buffer_size)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1317 buf_size = buffer_size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1318 else if (ntimes > 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1319 {
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1320 // 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
1321 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
1322 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
1323 }
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1324 // Finally, create the stream.
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1325 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
1326
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1327 // 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
1328 // (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
1329 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
1330 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
1331 row_idx(1) = 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 int err = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1334 octave_idx_type row = 0;
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 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
1337 skip_delim (is);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1338
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1339 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
1340
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1341 // 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
1342 if (fmt_list.set_from_first)
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 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
1345 lines = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1346
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1347 done_after = fmt_list.numel () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1348 if (! err)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1349 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
1350 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1351 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1352 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
1353
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1354 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
1355
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1356 // 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
1357 // Check now which columns to merge.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1358 // 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
1359 // after reading in data.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1360 // 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
1361 // 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
1362 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
1363 int conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1364 if (collect_output)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1365 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1366 int prev_type = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1367 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
1368 col != out.end (); col++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1369 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1370 if (col->type_id () == prev_type
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1371 || (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
1372 merge_with_prev [conv++] = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1373 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1374 merge_with_prev [conv++] = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1375
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1376 prev_type = col->type_id ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1377 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1378 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1379
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1380 // 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
1381 // infinite loop below.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1382 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1383 error ("textscan: No conversions specified");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1384
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1385 // 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
1386 if (! err)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1387 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1388 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
1389 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1390 if (row == 0 || row >= size)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1391 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1392 size += size+1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1393 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
1394 col != out.end (); col++)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1395 *col = (*col).resize (dim_vector (size, 1), 0);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1396 }
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 row_idx(0) = row;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1399 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
1400
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1401 if (err > 0 || ! is || (lines >= ntimes && ntimes > -1))
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1402 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1403 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1404 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1405
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1406 if ((err & 4) && ! return_on_error)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1407 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
1408 done_after + 1, row + 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1409
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1410 // 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
1411 bool uneven_columns = false;
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1412 if (err & 4)
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1413 uneven_columns = true;
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
1414 else if (isp.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1415 {
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1416 isp.clear ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1417 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
1418 int last_char = isp.get ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1419 isp.setstate (isp.eofbit);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1420 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
1421 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1422
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1423 // convert return value to Cell array
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1424 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
1425
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1426 // (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
1427 // 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
1428 if (err & 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1429 done_after = out.size () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1430
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1431 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
1432 dim_vector dv (valid_rows, 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1433
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1434 ra_idx(0) = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1435 int i = 0;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1436 if (! collect_output)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1437 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1438 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
1439 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
1440 col != out.end (); col++, i++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1441 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1442 // 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
1443 if (i == done_after && uneven_columns)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1444 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
1445
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1446 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1447 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
1448 ra_idx);
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1451 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
1452 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1453 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
1454 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
1455 int prev_type = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1456
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1457 conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1458 retval = Cell ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1459 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
1460 col != out.end (); col++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1461 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1462 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
1463 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1464 if (prev_type != -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1465 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1466 ra_idx(1) = i++;
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
1467 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
1468 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1469 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1470 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
1471 group_size = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1472 prev_type = col->type_id ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1473 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1474 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1475 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1476 ra_idx(1) = group_size++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1477 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
1478 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1479 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1480 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1481 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1482 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
1483 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1484
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1485 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1486 }
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1487
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1488 // 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
1489 // 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
1490
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1491 static double
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1492 pown (double x, unsigned int n)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1493 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1494 double retval = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1495
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1496 for (unsigned int d = n; d; d >>= 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1497 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1498 if (d & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1499 retval *= x;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1500 x *= x;
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1503 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1504 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1505
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1506 // 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
1507 // exp_chars option of options.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1508
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1509 double
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1510 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
1511 const textscan_format_elt& fmt) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1512 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1513 int sign = 1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1514 unsigned int width_left = fmt.width;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1515 double retval = 0;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1516 bool valid = false; // syntactically correct double?
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1517
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1518 int ch = is.peek ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1519
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1520 if (ch == '+')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1521 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1522 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1523 ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1524 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1525 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1526 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1527 else if (ch == '-')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1528 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1529 sign = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1530 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1531 ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1532 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1533 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1534 }
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 // Read integer part
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1537 if (ch != '.')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1538 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1539 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
1540 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1541 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
1542 retval = retval * 10 + (ch - '0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1543 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1544 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1545
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1546 // Read fractional part, up to specified precision
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1547 if (ch == '.' && width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1548 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1549 double multiplier = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1550 int precision = fmt.prec;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1551 int i;
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 (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1554 width_left--; // Consider width of '.'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1555
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1556 if (precision == -1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1557 precision = 1<<30; // FIXME Should be MAXINT
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1558
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1559 if (! valid) // if there was nothing before '.'...
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1560 is.get (); // ...ch was a "peek", not "get".
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1561
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1562 for (i = 0; i < precision; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1563 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1564 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
1565 retval += (ch - '0') * (multiplier *= 0.1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1566 else
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 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1569 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1570 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1571 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1572
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1573 // 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
1574 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
1575 && ch <= '9')
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1576 retval += multiplier;
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 if (i > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1579 valid = true; // valid if at least one digit after '.'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1580
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1581 // 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
1582 if (i == precision)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1583 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
1584 ; // discard
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1585
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1586 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1587 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1588
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1589 // 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
1590 const char *ec = exp_chars.c_str ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1591 bool used_exp = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1592 if (valid && width_left > 1 && strchr (ec, ch))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1593 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1594 int ch1 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1595 if (ch1 == '-' || ch1 == '+' || (ch1 >= '0' && ch1 <= '9'))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1596 { // 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
1597 width_left--; // count "E"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1598 int exp = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1599 int exp_sign = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1600 if (ch1 == '+')
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 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1603 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1604 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1605 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1606 else if (ch1 == '-')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1607 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1608 exp_sign = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1609 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1610 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1611 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1612 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1613 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1614 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
1615 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1616 exp = exp*10 + ch - '0';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1617 valid = true;
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 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
1620 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
1621 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1622
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1623 double multiplier = pown (10, exp);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1624 if (exp_sign > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1625 retval *= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1626 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1627 retval /= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1628
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1629 used_exp = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1630 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1631 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1632 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
1633 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
1634 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1635
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1636 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1637 if (! valid && width_left >= 3)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1638 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1639 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
1640 if (i == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1641 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1642 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1643 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1644 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1645 else if (i == 1)
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 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1648 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1649 }
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1652 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1653 if (! valid && width_left >= 3)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1654 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1655 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
1656 if (i == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1657 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1658 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1659 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1660 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1661 else if (i == 1)
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 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1664 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1665 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1666 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1667
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1668 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1669 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1670 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1671 is.setstate (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1672
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1673 return retval * sign;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1674 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1675
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1676 // 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
1677 // 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
1678 // 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
1679
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1680 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1681 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
1682 Complex& val) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1683 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1684 double im = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1685 double re = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1686 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
1687 bool inf = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1688
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1689 int ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1690 if (ch == '+' || ch == '-') // check for [+-][ij] with no coefficients
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 ch = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1693 int ch2 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1694 if (ch2 == 'i' || ch2 == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1695 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1696 double value = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1697 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1698 // Check not -inf
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1699 if (is.peek () == 'n')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1700 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1701 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1702 std::ios::iostate state = is.rdstate ();
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 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1705 ch2 = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1706 if (ch2 == 'f')
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 inf = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1709 re = (ch == '+') ? octave_Inf : -octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1710 value = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1711 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1712 else
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 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1715 is.seekg (pos); // reset to position before look-ahead
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1718
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1719 im = (ch == '+') ? value : -value;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1720 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1721 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1722 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1723 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1724
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1725 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
1726 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1727 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1728 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1729 //re = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1730 re = read_double (is, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1731
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1732 // check for "treat as empty" string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1733 if (treat_as_empty.numel ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1734 && (is.fail () || octave_is_NaN_or_NA (Complex (re))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1735 || re == octave_Inf))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1736 {
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 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
1739 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1740 if (ch == treat_as_empty (i).string_value ()[0])
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 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
1743 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1744 }
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 if (as_empty) // if first char matched...
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1747 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1748 as_empty = false; // ...look for the whole string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1749
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1750 is.clear (state); // treat_as_empty "-" causes partial read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1751 is.seekg (pos); // reset to position before failed read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1752
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1753 // treat_as_empty strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1754 // 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
1755 // that matches.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1756 char *look, look_buf [treat_as_empty_len + 1];
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1757 // prefill, in case EOF means part-filled.
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1758 memset (look_buf, '\0', treat_as_empty_len);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1759 look = is.read (look_buf, treat_as_empty_len, pos);
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 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1762 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1763 // FIXME -- is.read could invalidate pos
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1764
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1765 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
1766 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1767 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
1768 if (! strncmp (s.c_str (), look, s.size ()))
21462
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 as_empty = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1771 // read just the right amount
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1772 is.read (look_buf, s.size (), pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1773 break;
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1776 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1777 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1778
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1779 if (! is.eof () && ! as_empty)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1780 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1781 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
1782 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1783 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
1784 if (ch == 'i' || ch == 'j') // pure imaginary
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 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1787 im = re;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1788 re = 0;
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 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
1791 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1792 // 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
1793 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1794 state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1795
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1796 //im = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1797 im = read_double (is, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1798 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1799 im = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1800
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1801 if (is.peek () == 'i' || is.peek () == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1802 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1803 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1804 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1805 im = 0; // no valid imaginary part. Restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1806 is.clear (state); // eof shouldn't cause fail.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1807 is.seekg (pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1808 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1809 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1810 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
1811 is.clear (state); // failed peek shouldn't cause fail
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1814 if (as_empty)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1815 val = empty_value.scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1816 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1817 val = Complex (re, im);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1818 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1819
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1820 // 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
1821
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1822 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1823 textscan::scan_caret (delimited_stream& is, const char *pattern,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1824 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1825 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1826 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1827 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1828
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1829 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
1830 ? 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
1831 : 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
1832 != std::istream::traits_type::eof ())
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1833 && ! strchr (pattern, c1))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1834 obuf << static_cast<char> (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1835
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1836 val = obuf.str ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1837
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1838 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1839 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1840
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1841 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1842 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1843
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1844 // 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
1845 // 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
1846
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1847 std::string
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1848 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
1849 const std::string& ends) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1850 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1851 std::string retval ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1852 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1853 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1854 { // find sequence ending with an ending char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1855 std::string next;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1856 scan_caret (is, ends.c_str (), next);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1857 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
1858
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1859 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
1860 ? 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
1861
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1862 if (last != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1863 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1864 retval = retval + static_cast<char> (last);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1865 for (int i = 0; i < delimiters.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1866 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1867 std::string delim = delimiters(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1868 int start = retval.length () - delim.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1869 if (start < 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1870 start = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1871 std::string may_match = retval.substr (start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1872 if (may_match == delim)
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 done = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1875 retval = retval.substr (0, start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1876 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1877 }
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 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1881 while (! done && is && ! is.eof ());
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1882
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1883 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1884 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1885
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1886
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1887 // 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
1888 // 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
1889 // 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
1890
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1891 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1892 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
1893 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1894 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1895 if (delim_list.numel () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1896 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1897 unsigned int i = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1898 unsigned int width = fmt.width;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1899
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1900 for (i = 0; i < width; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1901 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1902 if (i+1 > val.length ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1903 val = val + val + ' '; // grow even if empty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1904 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
1905 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
1906 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1907 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1908 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1909 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1910 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1911 val[i] = ch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1912 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1913 val = val.substr (0, i); // trim pre-allocation
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1914 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1915 else // Cell array of multi-character delimiters
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 std::string ends ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1918 for (int i = 0; i < delim_list.numel (); i++)
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 std::string tmp = delim_list(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1921 ends += tmp.substr (tmp.length () - 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1922 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1923 val = textscan::read_until (is, delim_list, ends);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1924 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1925 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1926
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1927 // 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
1928
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1929 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1930 textscan::scan_bracket (delimited_stream& is, const char *pattern,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1931 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1932 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1933 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1934 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1935
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1936 while (is && strchr (pattern, (c1 = is.get_undelim ())))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1937 obuf << static_cast<char> (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1938
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1939 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
1940 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1941 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1942 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1943 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1944
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1945 // 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
1946 // 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
1947 // 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
1948
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1949 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1950 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
1951 std::string& val)
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 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1954
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1955 if (is.peek () != '\"')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1956 scan_string (is, fmt, val);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1957 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1958 {
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 scan_caret (is, "\"", val); // read everything until "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1961 is.get (); // swallow "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1962
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1963 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
1964 { // and keep looking for single "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1965 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1966 std::string val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1967 scan_caret (is, "\"", val1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1968 val = val + "\"" + val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1969 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1970 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1971 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1972 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1973
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1974 // 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
1975 // including any whitespace or delimiters.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1976
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1977 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1978 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
1979 std::string& val) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1980 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1981 val.resize (fmt.width);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1982
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1983 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
1984 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1985 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
1986 if (ch != std::istream::traits_type::eof ())
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1987 val[i] = ch;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1988 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1989 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1990 val.resize (i);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1991 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1992 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1993 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1994 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1995
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1996
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1997 // 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
1998
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1999 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2000 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
2001 octave_value& ov, Array<octave_idx_type> row)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2002 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2003 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2004
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2005 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2006
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2007 octave_value val;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2008 if (fmt.numeric)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2009 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2010 if (fmt.type == 'f' || fmt.type == 'n')
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 Complex v;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2013 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2014 scan_complex (is, fmt, v);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2015
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2016 if (! fmt.discard && ! is.fail ())
21462
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 (fmt.bitwidth == 64)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2019 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2020 if (ov.is_real_type () && v.imag () == 0)
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.real ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2022 else
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 if (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2025 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2026 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2027 ov.internal_rep ()->fast_elem_insert (row(0), v);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2028 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2029 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2030 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2031 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2032 if (ov.is_real_type () && v.imag () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2033 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2034 float (v.real ()));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2035 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2036 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2037 if (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2038 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2039 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2040 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2041 FloatComplex (v));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2042 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2043 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2044 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2045 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2046 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2047 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2048 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
2049 // 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
2050 // Some loss of precision for d64 and u64.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2051 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2052 v = read_double (is, fmt);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2053 if (! fmt.discard && ! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2054 switch (fmt.bitwidth)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2055 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2056 case 64:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2057 switch (fmt.type)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2058 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2059 case 'd':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2060 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2061 octave_int64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2062 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
2063 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2064 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2065
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2066 case 'u':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2067 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2068 octave_uint64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2069 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
2070 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2071 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2072 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2073 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2074
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2075 case 32:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2076 switch (fmt.type)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2077 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2078 case 'd':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2079 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2080 octave_int32 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2081 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
2082 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2083 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2084
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2085 case 'u':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2086 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2087 octave_uint32 vv = v;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2088 ov.internal_rep ()->fast_elem_insert (row(0), vv);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2089 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2090 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2091 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2092 break;
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 case 16:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2095 if (fmt.type == 'd')
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2096 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2097 octave_int16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2098 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
2099 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2100 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2101 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2102 octave_uint16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2103 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
2104 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2105 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2106
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2107 case 8:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2108 if (fmt.type == 'd')
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2109 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2110 octave_int8 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2111 ov.internal_rep ()->fast_elem_insert (row(0), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2112 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2113 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2114 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2115 octave_uint8 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2116 ov.internal_rep ()->fast_elem_insert (row(0), vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2117 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2118 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2119 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2120 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2121
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2122 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2123 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2124 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2125 ov = do_cat_op (ov, empty_value, row);
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 // 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
2128 if (return_on_error == 2)
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 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2131 is.clear (); // clear to allow read pointer to advance
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 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2134 scan_string (is, fmt, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2135
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2136 is.setstate (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2137 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2138 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2139
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2140 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2141 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2142 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2143 std::string vv (" "); // initial buffer. Grows as needed
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2144 switch (fmt.type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2145 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2146 case 's':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2147 scan_string (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2148 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2149
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2150 case 'q':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2151 scan_qstring (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2152 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2153
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2154 case 'c':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2155 scan_cstring (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2156 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2157
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2158 case '[':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2159 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
2160 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2161
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2162 case '^':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2163 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
2164 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2165 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2166
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2167 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2168 ov.internal_rep ()->fast_elem_insert (row (0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2169 Cell (octave_value (vv)));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2170
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2171 // FIXME -- why does failbit get set at EOF, instead of eofbit?
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2172 if (vv.length () != 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2173 is.clear (is.rdstate () & ~std::ios_base::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2174 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2175
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2176 is.field_done ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2177 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2178
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2179 // 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
2180 // values in row ROW of retval.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2181
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2182 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2183 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
2184 textscan_format_list& fmt_list,
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
2185 std::list<octave_value>& retval,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2186 Array<octave_idx_type> row, int& done_after)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2187 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2188 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
2189 std::list<octave_value>::iterator out = retval.begin ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2190 bool no_conversions = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2191 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2192 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
2193
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2194 octave_quit ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2195
21486
4581d0d52948 avoid signed/unsigned comparison warnings introduced in previous change
John W. Eaton <jwe@octave.org>
parents: 21485
diff changeset
2196 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
2197 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2198 bool this_conversion_failed = false;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2199
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2200 // Clear fail of previous numeric conversions.
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2201 is.clear ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2202
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2203 switch (elem->type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2204 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2205 case 'C':
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 std::cerr << "textscan: Conversion %" << elem->type
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2208 << " not yet implemented\n";
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2209 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2210
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2211 case 'u':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2212 case 'd':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2213 case 'f':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2214 case 'n':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2215 case 's':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2216 case '[':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2217 case '^':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2218 case 'q':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2219 case 'c':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2220 scan_one (is, *elem, *out, row);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2221 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2222
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2223 case textscan_format_elt::literal_conversion :
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2224 match_literal (is, *elem);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2225 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2226
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2227 default:
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2228 error ("Unknown format element '%c'", elem->type);
21462
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
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2231 if (! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2232 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2233 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2234 no_conversions = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2235 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2236 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2237 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2238 if (return_on_error < 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2239 this_conversion_failed = true;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2240
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2241 is.clear (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2242 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2243
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2244 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2245 out++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2246
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2247 elem = fmt_list.next ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2248 char *pos = is.tellg ();
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2249
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2250 // FIXME -- these conversions "ignore delimiters". Should they include
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2251 // 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
2252 if (elem->type != textscan_format_elt::literal_conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2253 // && elem->type != '[' && elem->type != '^' && elem->type != 'c'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2254 )
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2255 skip_delim (is);
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2256
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2257 if (this_conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2258 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2259 if (is.tellg () == pos && ! conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2260 { // done_after = first failure
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2261 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
2262 conversion_failed = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2263 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2264 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2265 this_conversion_failed = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2266 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2267
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2268 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2269 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2270 if (! done)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2271 done_after = i+1;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2272
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2273 // 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
2274 done = true;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2275 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2276 }
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2277
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2278 if (done)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2279 is.setstate (std::ios::eofbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2280
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2281 // 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
2282 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2283 return (2 + no_conversions);
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 if (conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2286 return (4 + no_conversions);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2287
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2288 return 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2289 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2290
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2291 void
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2292 textscan::parse_options (const octave_value_list& args,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2293 textscan_format_list& fmt_list)
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 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
2296 int n = last;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2297
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2298 if (n & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2299 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
2300
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2301 delim_len = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2302 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
2303 for (int i = 0; i < last; i += 2)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2304 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2305 if (! args(i).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2306 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
2307 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
2308
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2309 std::string param = args(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2310 std::transform (param.begin (), param.end (),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2311 param.begin (), ::tolower);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2312 if (param == "delimiter")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2313 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2314 bool invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2315 if (args(i+1).is_string ())
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 have_delims = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2319 delims = args(i+1).string_value ();
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 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2322 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2323 invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2324 delim_list = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2325 delim_table = " "; // non-empty, to flag non-default delim
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2326
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2327 // 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
2328 for (int j = 0; j < delim_list.numel (); j++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2329 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2330 if (! delim_list(j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2331 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2332 else
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 octave_idx_type len = delim_list(j).string_value ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2335 .length ();
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
2336 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
2337 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2338 }
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 (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2341 error ("textscan: Delimiters must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2342 "cell array of strings");
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 (param == "commentstyle")
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 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2347 { // check here for names like "C++", "C", "shell", ...?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2348 comment_style = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2349 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2350 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2351 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2352 comment_style = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2353 int len = comment_style.numel ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2354 if ((len >= 1 && ! comment_style (0).is_string ())
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2355 || (len >= 2 && ! comment_style (1).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2356 || (len >= 3))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2357 error ("textscan: CommentStyle must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2358 "cell array of one or two strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2359 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2360 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2361 error ("textscan: CommentStyle must be either a string"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2362 " 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
2363 args(i+1).class_name ().c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2364
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2365 // 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
2366 // and which character do we look for?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2367 if (comment_style.numel () >= 1)
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 comment_len = comment_style (0).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2370 comment_char = comment_style (0).string_value ()[0];
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2373 else if (param == "treatasempty")
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 bool invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2376 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2377 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2378 treat_as_empty = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2379 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
2380 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2381 else if (args(i+1).is_cell ())
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 treat_as_empty = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2384 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
2385 if (! treat_as_empty (j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2386 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2387 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2388 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2389 int k = treat_as_empty (j).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2390 if (k > treat_as_empty_len)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2391 treat_as_empty_len = k;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2392 }
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 if (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2395 error ("textscan: TreatAsEmpty must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2396 "cell array of one or two strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2397
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2398 // 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
2399 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2400 else if (param == "collectoutput")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2401 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2402 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2403 collect_output = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2404 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2405 error ("textscan: CollectOutput must be logical or numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2406 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2407 else if (param == "emptyvalue")
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 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2410 empty_value = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2411 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2412 error ("textscan: EmptyValue must be numeric, not <%s>",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2413 args(i+1).class_name ().c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2414 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2415 else if (param == "headerlines")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2416 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2417 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2418 header_lines = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2419 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2420 error ("textscan: HeaderLines must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2421 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2422 else if (param == "bufsize")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2423 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2424 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2425 buffer_size = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2426 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2427 error ("textscan: BufSize must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2428 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2429 else if (param == "multipledelimsasone")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2430 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2431 if (args(i+1).is_numeric_type ())
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 if (args(i+1).bool_value ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2434 multiple_delims_as_one = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2435 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2436 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2437 error ("textscan: MultipleDimsAsOne must be logical or numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2438 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2439 else if (param == "returnonerror")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2440 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2441 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2442 return_on_error = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2443 else if (args(i+1).is_string ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2444 && args(i+1).string_value () == "continue")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2445 return_on_error = 2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2446 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2447 error ("textscan: ReturnOnError must be logical or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2448 "numeric, or 'continue'");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2449 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2450 else if (param == "whitespace")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2451 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2452 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2453 whitespace = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2454 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2455 error ("textscan: Whitespace must be a character string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2456 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2457 else if (param == "expchars")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2458 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2459 if (args(i+1).is_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 exp_chars = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2462 default_exp = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2463 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2464 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2465 error ("textscan: ExpChars must be a character string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2466 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2467 else if (param == "endofline")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2468 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2469 bool valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2470 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2471 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2472 std::string s = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2473 if (args(i+1).is_sq_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2474 s = do_string_escapes (s);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2475 int l = s.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2476 if (l == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2477 eol1 = eol2 = -2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2478 else if (l == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2479 eol1 = eol2 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2480 else if (l == 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2481 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2482 eol1 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2483 eol2 = s.c_str ()[1];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2484 if (eol1 != '\r' || eol2 != '\n') // Why limit it?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2485 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2486 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2487 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2488 valid = false;
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 valid = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2492 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2493 error ("textscan: EndOfLine must be at most one character "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2494 "or '\\r\\n'");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2495 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2496 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2497 error ("textscan: Unrecognised option '%s'", param.c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2498 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2499
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2500 whitespace_table = std::string (256, '\0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2501 for (unsigned int i = 0; i < whitespace.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2502 whitespace_table[whitespace[i]] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2503
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2504 // For Matlab compatibility, add 0x20 to whitespace, unless
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2505 // whitespace is explicitly ignored.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2506 if (! (whitespace.length () == 0 && fmt_list.has_string))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2507 whitespace_table[' '] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2508
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2509 // Create look-up table of delimiters, based on 'delimiter'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2510 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
2511 if (eol1 >= 0 && eol1 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2512 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
2513 if (eol2 >= 0 && eol2 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2514 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
2515 if (! have_delims)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2516 for (unsigned int i = 0; i < 256; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2517 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2518 if (isspace (i))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2519 delim_table[i] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2520 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2521 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2522 for (unsigned int i = 0; i < delims.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2523 delim_table[delims[i]] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2524 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2525
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2526 // 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
2527 // 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
2528
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2529 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2530 textscan::skip_whitespace (delimited_stream& is, bool EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2531 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2532 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2533 bool found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2534
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2535 do
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 found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2538 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
2539 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
2540 && ( ( (c1 == eol1 || c1 == eol2) && ++lines && ! EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2541 || isspace (c1)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2542 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2543 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2544 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2545 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2546 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2547
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2548 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
2549 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2550 // 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
2551 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2552 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2553
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2554 char *look, tmp [comment_len];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2555 look = is.read (tmp, 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
2556 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
2557 .c_str (), look, comment_len-1))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2558 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2559 found_comment = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2560
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2561 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2562 char eol [3] = {static_cast<char> (eol1),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2563 static_cast<char> (eol2),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2564 '\0'};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2565 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
2566 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2567 scan_caret (is, eol, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2568 c1 = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2569 if (c1 == eol1 && eol1 != eol2 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2570 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2571 lines++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2572 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2573 else // matching pair
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2574 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2575 std::string end_c = comment_style(1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2576 // last char of end-comment sequence
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2577 char last[2] = {*(end_c.substr (end_c.length ()-1).c_str ()),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2578 '\0'};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2579 std::string may_match ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2580 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2581 { // find sequence ending with last char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2582 scan_caret (is, last, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2583 is.get_undelim (); // (read last itself)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2584
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2585 may_match = may_match + dummy + *last;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2586 int start = may_match.length () - end_c.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2587 if (start < 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2588 start = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2589 may_match = may_match.substr (start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2590 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2591 while (may_match != end_c && is && ! is.eof ());
21462
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 else // wasn't really a comment; restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2595 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2596 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2597 is.seekg (pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2598 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2599 }
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 while (found_comment);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2602
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2603 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2604 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2605 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2606 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2607
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2608 // 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
2609 // 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
2610 // 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
2611
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2612 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2613 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
2614 bool case_sensitive) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2615 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2616 // target strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2617 // 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
2618 // that matches.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2619
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2620 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2621
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2622 char *look, tmp [max_len + 1];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2623
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2624 memset (tmp, '\0', max_len); // prefill, in case EOF means part-filled.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2625 look = is.read (tmp, max_len, pos);
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 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2628 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2629 // FIXME pos may be corrupted by is.read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2630
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2631 int i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2632 int (*compare)(const char *, const char *, size_t);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2633 compare = case_sensitive ? strncmp : strncasecmp;
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 for (i = 0; i < targets.numel (); i++)
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 std::string s = targets (i).string_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2638 if (! (*compare) (s.c_str (), look, s.size ()))
21462
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 is.read (tmp, s.size (), pos); // read just the right amount
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2641 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2642 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2643 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2644
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2645 if (i == targets.numel ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2646 i = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2647
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2648 return i;
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2651 // Skip delimiters -- multiple if MultipleDelimsAsOne specified.
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2652 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2653 textscan::skip_delim (delimited_stream& is)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2654 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2655 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
2656 if (delim_list.numel () == 0) // single character delimiter
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 if (is_delim (c1) || c1 == eol1 || c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2659 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2660 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2661 if (c1 == eol1 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2662 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
2663
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2664 if (multiple_delims_as_one)
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 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2667 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2668 // 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
2669 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
2670 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2671 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2672 || isspace (c1) || is_delim (c1)))
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 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2675 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2676 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2677 }
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2678 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2679 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2680 }
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2683 else // multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2684 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2685 int first_match;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2686
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2687 if (c1 == eol1 || c1 == eol2
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2688 || (-1 != (first_match = lookahead (is, delim_list, delim_len))))
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 if (c1 == eol1)
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 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2693 if (is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2694 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2695 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2696 else if (c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2697 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2698 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2699 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2700
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2701 if (multiple_delims_as_one)
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 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2704 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2705 // 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
2706 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
2707 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2708 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2709 || -1 != lookahead (is, delim_list, delim_len)))
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 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2712 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2713 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2714 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2715 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2716 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2717 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2718
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2719 return c1;
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2722 // 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
2723 // 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
2724 // false (and set failbit).
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2725
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2726 bool
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2727 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
2728 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2729 // "false" -> treat EOL as normal space
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2730 // 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
2731 skip_whitespace (is, false);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2732
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2733 for (unsigned int i = 0; i < fmt.width; i++)
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 int ch = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2736 if (ch != fmt.text[i])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2737 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2738 if (ch != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2739 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2740 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2741 return false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2742 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2743 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2744 return true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2745 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2746
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2747 DEFUN (textscan, args, ,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2748 "-*- texinfo -*-\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2749 @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
2750 @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
2751 @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
2752 @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
2753 @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
2754 @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
2755 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
2756 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2757 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
2758 parsed according to @var{format}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2759 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
2760 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
2761 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
2762 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2763 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
2764 (such as whitespace) and literals.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2765 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
2766 by the options.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2767 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
2768 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
2769 but is otherwise ignored.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2770 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2771 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
2772 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
2773 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2774 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
2775 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
2776 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
2777 and so forth.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2778 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
2779 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
2780 (see below).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2781 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2782 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
2783 parsed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2784 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
2785 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
2786 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
2787 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
2788 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2789 The following specifiers are valid:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2790 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2791 @table @code\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2792 @item %f\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2793 @itemx %f64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2794 @itemx %n\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2795 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
2796 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2797 @item %f32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2798 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
2799 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2800 @item %d\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2801 @itemx %d8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2802 @itemx %d16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2803 @itemx %d32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2804 @itemx %d64\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 int8, int16, int32 or int64.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2806 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
2807 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2808 @item %u\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2809 @itemx %u8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2810 @itemx %u16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2811 @itemx %u32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2812 @itemx %u64\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 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
2814 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
2815 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2816 @item %s\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2817 The word is parsed as a 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
2818 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
2819 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2820 @item %q\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2821 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
2822 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
2823 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
2824 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
2825 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
2826 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
2827 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
2828 'He said \"Hello\"'.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2829 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2830 @item %c\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2831 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
2832 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
2833 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2834 @item %[...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2835 @itemx %[^...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2836 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
2837 characters between the brackets.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2838 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
2839 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
2840 (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
2841 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
2842 alphanumeric characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2843 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
2844 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
2845 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
2846 NOT listed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2847 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2848 @item %N...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2849 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
2850 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
2851 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
2852 a single character.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2853 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
2854 number of characters read;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2855 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
2856 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
2857 components individually.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2858 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
2859 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
2860 subsequent digits are skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2861 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
2862 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2863 @item %*...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2864 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
2865 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2866 @item literals\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2867 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
2868 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
2869 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
2870 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
2871 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2872 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2873 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
2874 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
2875 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2876 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
2877 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
2878 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
2879 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
2880 line of the input.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2881 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
2882 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2883 For example, the string\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2884 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2885 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2886 @group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2887 @var{str} = \"\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2888 Bunny Bugs 5.5\\n\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2889 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
2890 Penguin Tux 6\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2891 @end group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2892 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2893 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2894 @noindent\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2895 can be read using\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2896 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2897 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2898 @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
2899 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2900 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2901 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
2902 number of items read:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2903 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2904 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2905 @item -1\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2906 (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
2907 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2908 @item N\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2909 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
2910 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
2911 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
2912 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
2913 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
2914 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
2915 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2916 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2917 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
2918 The following properties are recognized:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2919 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2920 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2921 @item @qcode{\"BufSize\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2922 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
2923 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
2924 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
2925 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
2926 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2927 @item @qcode{\"CollectOutput\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2928 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
2929 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
2930 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
2931 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2932 @item @qcode{\"CommentStyle\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2933 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
2934 @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
2935 (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
2936 (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
2937 second strings.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2938 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
2939 delimiters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2940 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2941 @item @qcode{\"Delimiter\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2942 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
2943 split @var{str} into words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2944 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
2945 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
2946 (default value = any whitespace.)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2947 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2948 @item @qcode{\"EmptyValue\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2949 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
2950 The default is NaN@.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2951 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
2952 then default is zero.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2953 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2954 @item @qcode{\"EndOfLine\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2955 @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
2956 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
2957 @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
2958 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
2959 @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
2960 @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
2961 newline.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2962 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
2963 used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2964 @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
2965 @c to whitespace.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2966 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2967 @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
2968 @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
2969 @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
2970 @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
2971 @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
2972 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2973 @item @qcode{\"HeaderLines\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2974 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
2975 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
2976 lines of any type.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2977 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2978 @item @qcode{\"MultipleDelimsAsOne\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2979 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
2980 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
2981 as a single delimiter.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2982 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
2983 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
2984 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
2985 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
2986 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
2987 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2988 @item @qcode{\"TreatAsEmpty\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2989 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
2990 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
2991 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2992 @item @qcode{\"ReturnOnError\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2993 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
2994 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
2995 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
2996 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
2997 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
2998 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2999 @item @qcode{\"Whitespace\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3000 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
3001 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
3002 @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
3003 @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
3004 @qcode{\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3005 @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
3006 (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
3007 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
3008 always part of whitespace.\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 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3011 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3012 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
3013 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
3014 textscan's behavior depends on\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3015 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
3016 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
3017 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3018 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3019 @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
3020 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
3021 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
3022 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3023 @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
3024 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
3025 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3026 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3027 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3028 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
3029 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
3030 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3031 @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
3032 @end deftypefn")
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3033 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3034 octave_value_list retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3035
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3036 if (args.length () < 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3037 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
3038
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3039 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
3040
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3041 textscan tscanner;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3042
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3043 if (args(0).is_string ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3044 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3045 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
3046
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3047 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
3048
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3049 std::ios::iostate state = is.rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3050 is.clear ();
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3051 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
3052 is.setstate (state);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3053 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3054 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3055 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3056 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
3057 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
3058 if (! isp)
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3059 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
3060
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3061 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
3062
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3063 // 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
3064 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
3065 os.input_stream ()->clear ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3066 retval(1) = os.tell ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3067 os.input_stream ()->setstate (state);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3068 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3069
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3070 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3071 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3072
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3073 /*
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3074 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3075 %! 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
3076 %! fmtstr = "%f %d %f %s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3077 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3078 %! assert (isequal (c{1}, [1;5]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3079 %! assert (length (c{1}), 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3080 %! assert (iscellstr (c{4}));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3081 %! assert (isequal (c{3}, [3; -Inf]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3082
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3083 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3084 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3085 %! b = [b; 8*b/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3086 %! 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
3087 %! fmt = "%f miles/hr = %f kilometers/hr";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3088 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3089 %! assert (c{1}, b(1,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3090 %! assert (c{2}, b(2,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3091
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3092 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3093 %! 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
3094 %! a = textscan (str, "%d %n %f %s %n", "delimiter", ",",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3095 %! "treatAsEmpty", {"NA", "na", "-"},"commentStyle", "//");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3096 %! assert (a{1}, int32 ([13; 36]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3097 %! assert (a{2}, [NaN; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3098 %! assert (a{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3099 %! assert (a{4}, {"str1"; "str3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3100 %! assert (a{5}, [-25; 6]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3101
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3102 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3103 %! str = "Km:10 = hhhBjjj miles16hour\r\n";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3104 %! str = [str "Km:15 = hhhJjjj miles241hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3105 %! str = [str "Km:2 = hhhRjjj miles3hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3106 %! str = [str "Km:25 = hhhZ\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3107 %! fmt = "Km:%d = hhh%1sjjj miles%dhour";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3108 %! a = textscan (str, fmt, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3109 %! assert (a{1}', int32 ([10 15 2 25]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3110 %! assert (a{2}', {'B' 'J' 'R' 'Z'});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3111 %! assert (a{3}', int32 ([16 241 3 0]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3112
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3113 ## Test with default endofline parameter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3114 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3115 %! c = textscan ("L1\nL2", "%s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3116 %! assert (c{:}, {"L1"; "L2"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3117
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3118 ## 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
3119 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3120 %! c = textscan ("L1\nL2", "%s", "endofline", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3121 %! assert (int8 ([c{:}{:}]), int8 ([ 76, 49, 10, 76, 50 ]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3122
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3123 ### 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
3124 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3125 #%! ## 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
3126 #%! str = "Text1Text2Text\nTextText4Text\nText57Text";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3127 #%! c = textscan (str, "Text%*dText%dText");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3128 #%! assert (c{1}, int32 ([2; 4; 0]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3129
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3130 ## CollectOutput test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3131 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3132 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3133 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3134 %! 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
3135 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3136 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3137 %! assert (size (c{3}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3138 %! assert (size (c{2}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3139
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3140 ## CollectOutput test with uneven column length files
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3141 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3142 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3143 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3144 %! 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
3145 %! str = [str "110 miles/hr"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3146 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3147 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3148 %! assert (size (c{1}), [11, 1]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3149 %! assert (size (c{3}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3150 %! assert (size (c{2}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3151 %! assert (c{3}(end), NaN);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3152 %! assert (c{2}{11, 1}, "/hr");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3153 %! assert (isempty (c{2}{11, 2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3154
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3155 ## Double quoted string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3156 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3157 %! str = 'First "the second called ""the middle""" third';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3158 %! fmt = "%q";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3159 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3160 %! 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
3161
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3162 ## Arbitrary character
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3163 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3164 %! 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
3165 %! assert (c{1}, {"a"; "ond"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3166 %! assert (c{2}, {"f"; "t"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3167 %! assert (c{3}, {"irst, \n sec"; "hird"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3168
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3169 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3170 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3171 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3172 %! c = textscan (str, "%4d %4d", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3173 %! assert (c, {[12 34; 1234 5678; 9 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3174
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3175 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3176 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3177 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3178 %! c = textscan (str, "%4f %f", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3179 %! assert (c, {[12 34; 1234 56789; 7 NaN]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3180
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3181 ## Ignore trailing delimiter, but use leading one
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3182 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3183 %! 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
3184 %! c = textscan (str, "%10.2f %f", "delimiter", ",", "collectOutput", 1,
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3185 %! "expChars", "e|");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3186 %! 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
3187
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 %! ## Multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3190 %! str = "99end2 space88gap 4564";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3191 %! 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
3192 %! assert (c{1}, int32 ([99; 88]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3193 %! assert (c{2}, {"2 "; "4564"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3194
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3195 ### Delimiters as part of literals, and following literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3196 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3197 #%! str = "12 R&D & 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3198 #%! 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
3199 #%! assert (c, {[12 -99; 7 -99]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3200 #
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3201 ### Delimiters as part of literals, and before literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3202 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3203 #%! str = "12 & R&D 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3204 #%! 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
3205 #%! assert (c, {[12 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3206
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3207 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3208 %! ## 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
3209 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3210 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3211 %! fprintf (fid, "1\n2\n3\n4\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3212 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3213 %! c = textscan (fid, "%f %f", 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3214 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3215 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3216 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3217 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3218 %! assert (!E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3219
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3220 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3221 %! ## 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
3222 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3223 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3224 %! 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
3225 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3226 %! c = textscan (fid, "%f %f", 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3227 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3228 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3229 %! assert (c, {[1;3], [2;4]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3230
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3231 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3232 %! ## Check number of lines read, with multiple delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3233 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3234 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3235 %! 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
3236 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3237 %! c = textscan (fid, "%f %f", 4, "delimiter", "-", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3238 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3239 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3240 %! assert (c, {[1;3], [2;4]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3241
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3242 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3243 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3244 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3245 %! fid = fopen (f, "w+");
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3246 %! 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
3247 %! fprintf (fid, str);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3248 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3249 %! c = textscan (fid, "%f %f %f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3250 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3251 %! d = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3252 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3253 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3254 %! unlink (f);
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3255 %! u = textscan (str, "%f %f %f");
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3256 %! v = textscan (str, "%f %f %f", "ReturnOnError", 1);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3257 %! assert (c, {[1;4], [2;NaN], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3258 %! 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
3259 %! 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
3260 %! assert (v, {[1;4], [2], [3]})
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3261
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3262 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3263 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3264 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3265 %! fid = fopen (f, "w+");
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3266 %! 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
3267 %! fprintf (fid, str);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3268 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3269 %! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3270 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3271 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3272 %! unlink (f);
21493
a41e48ef2b99 textscan: Don't pad columns with ReturnOnError = 1
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21492
diff changeset
3273 %! 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
3274 %! 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
3275 %! assert (u, {[1;4], 2, 3})
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3276
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3277 %!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
3278
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3279 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3280 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3281 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3282 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3283 %! fprintf (fid, "1 s 3\n4 5 6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3284 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3285 %! c = textscan (fid, "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3286 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3287 %! d = textscan (fid, "", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3288 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3289 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3290 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3291 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3292 %! assert (d, {1})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3293
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3294 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3295 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3296 %! 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
3297 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3298
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3299 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3300 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3301 %! 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
3302 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3303
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3304 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3305 %! ## Check ReturnOnError in first column
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3306 %! 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
3307 %! assert (c, {1, 2, 3})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3308
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3309 ## Test input validation
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3310 %!error textscan ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3311 %!error textscan (single (40))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3312 %!error textscan ({40})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3313 %!error <must be a string> textscan ("Hello World", 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3314 #%!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
3315 %!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
3316 %!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
3317 %!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
3318
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3319 ## Test incomplete first data line
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3320 %! R = textscan (['Empty1' char(10)], 'Empty%d %f');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3321 %! assert (R{1}, int32 (1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3322 %! assert (isempty (R{2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3323
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3324 ## bug #37023
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3325 %!test
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3326 %! 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
3327 %! assert (data{1}, [1; 2], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3328 %! assert (data{2}, [1; 3], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3329
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3330 ## Whitespace test (bug #37333) using delimiter ";"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3331 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3332 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3333 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3334 %! tc{1, end+1} = "C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3335 %! tc{1, end+1} = " C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3336 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3337 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3338 %! c = textscan (string, "%s", "delimiter", ";");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3339 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3340 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3341 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3342 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3343 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3344 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3345 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3346
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3347 ## Whitespace test (bug #37333), adding multipleDelimsAsOne true arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3348 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3349 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3350 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3351 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3352 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3353 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3354 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3355 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3356 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3357 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3358 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3359 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3360 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3361 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3362 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3363
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3364 ## Whitespace test (bug #37333), adding multipleDelimsAsOne false arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3365 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3366 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3367 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3368 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3369 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3370 %! tc{1, end+1} = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3371 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3372 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3373 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 0);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3374 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3375 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3376 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3377 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3378 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3379 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3380 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3381
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3382 ## Whitespace test (bug #37333) whitespace "" arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3383 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3384 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3385 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3386 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3387 %! tc{1, end+1} = "C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3388 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3389 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3390 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3391 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3392 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3393 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3394 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3395 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3396 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3397
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3398 ## Whitespace test (bug #37333), whitespace " " arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3399 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3400 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3401 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3402 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3403 %! tc{1, end+1} = "C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3404 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3405 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3406 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3407 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3408 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3409 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3410 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3411 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3412 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3413 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3414
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3415 ## 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
3416 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3417 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3418 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3419 %! 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
3420 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3421 %! A = textscan (fid, "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3422 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3423 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3424 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3425 %! assert (A{1}, [1 ; 5], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3426 %! assert (A{2}, [2 ; 6], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3427 %! assert (A{3}, [3 ; 7], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3428 %! assert (A{4}, [4 ; 8], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3429 %! assert (E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3430
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3431 ## 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
3432 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3433 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3434 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3435 %! fprintf (fid, " ,2,,4\n5,6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3436 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3437 %! A = textscan (fid, "", "delimiter", ",", "EmptyValue", 999, "CollectOutput" , 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3438 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3439 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3440 %! 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
3441
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3442 ## Error message tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3443
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3444 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3445 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3446 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3447 %! msg1 = "textscan: 1 parameters given, but only 0 values";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3448 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3449 %! A = textscan (fid, "", "headerlines");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3450 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3451 %! assert (!feof (fid));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3452 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3453 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3454 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3455
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3456 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3457 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3458 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3459 %! msg1 = "textscan: HeaderLines must be numeric";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3460 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3461 %! A = textscan (fid, "", "headerlines", "hh");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3462 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3463 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3464 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3465 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3466
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3467 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3468 %! ## Skip headerlines
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3469 %! 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
3470 %! assert (A, {[1 2; 3 4]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3471
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3472 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3473 %! ## Skip headerlines with non-default EOL
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3474 %! 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
3475 %! assert (A, {[3 4]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3476
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3477 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3478 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3479 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3480 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3481 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3482 %! 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
3483 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3484 %! A = textscan (fid, "%f", "EndOfLine", "\n\r");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3485 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3486 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3487 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3488 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3489
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3490 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3491 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3492 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3493 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3494 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3495 %! 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
3496 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3497 %! A = textscan (fid, "%f", "EndOfLine", 33);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3498 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3499 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3500 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3501 %! assert (msg1, lasterr);
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 #41824
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3504 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3505 %! assert (textscan ("123", "", "whitespace", " "){:}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3506
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3507 ## Bug #42343-1, just test supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3508 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3509 %! assert (textscan (",NaN", "", "delimiter", "," ,"emptyValue" ,Inf), {Inf, NaN});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3510
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3511 ## Bug #42343-2, test padding with supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3512 %!test
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3513 %! 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
3514 %! 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
3515
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3516 ## Bug #42528
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3517 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3518 %! assert (textscan ("1i", ""){1}, 0+1i);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3519 %! 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
3520
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3521 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3522 %! ## TreatAsEmpty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3523 %! 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
3524 %! assert (C{3}(1), int32 (3));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3525 %! assert (C{4}(1), NaN);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3526
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3527 ## MultipleDelimsAsOne
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3528 %!test
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3529 %! 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
3530 %! 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
3531 %! assert (C{1}', [11, 21, 33]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3532 %! assert (C{2}', [12, 23, 34]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3533 %! assert (C{3}', [13, 24, 35]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3534 %! assert (C{4}', [15, 25, NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3535
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3536 ## Bug #44750
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3537 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3538 %! assert (textscan ("/home/foo/", "%s", "delimiter", "/", "MultipleDelimsAsOne", 1){1}, ...
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3539 %! {"home"; "foo"});
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 ### Allow cuddling %sliteral but warn it is ambiguous
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 #%! C = textscan ("abcxyz51\nxyz83\n##xyz101", "%s xyz %d");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3544 #%! assert (C{1}([1 3]), {"abc"; "##"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3545 #%! assert (isempty (C{1}{2}), true);
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3546 #%! assert (C{2}, int32 ([51; 83; 101]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3547 ### Literals are not delimiters.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3548
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3549 ## 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
3550 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3551 %! 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
3552
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3553 ## Test various forms of string format specifiers (bug #45712)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3554 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3555 %! 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
3556 %! 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
3557 %! assert (C, {14, {"1 z"}, {"3 z"}, 11});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3558
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3559 %% Bit width, fixed width conv. specifiers
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3560 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3561 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3562 %! str2 = [str2 str2 str2 str2 str2 str2 str2 str2];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3563 %! 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
3564 %! 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
3565 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3566 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3567 %! assert (C{2}, uint16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3568 %! assert (C{3}, uint32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3569 %! assert (C{4}, uint64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3570 %! assert (C{5}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3571 %! assert (C{6}, int16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3572 %! assert (C{7}, int32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3573 %! assert (C{8}, int64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3574 %! assert (C{9}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3575 %! assert (C{10}, double (1234567890.123), 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3576 %! assert (C{11}, single (12345.68), 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3577 %! assert (C{12}, double (12345.68), 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3578
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3579 %% 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
3580 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3581 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3582 %! str2 = [str2 str2 "123456789.01234"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3583 %! pttrn = "%3u8 %5u16 %10u32 %3d8 %5d16 %10d32 %9f32 %9f";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3584 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3585 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3586 %! assert (C{2}, uint16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3587 %! assert (C{3}, uint32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3588 %! assert (C{4}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3589 %! assert (C{5}, int16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3590 %! assert (C{6}, int32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3591 %! assert (C{7}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3592 %! assert (C{8}, double (0.01234), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3593
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3594 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3595 %! C = textscan ("123.123", "%2f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3596 %! assert (C{1}, 12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3597 %! assert (C{2}, 3.1, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3598 %! assert (C{3}, 23);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3599
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3600 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3601 %! C = textscan ("123.123", "%3f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3602 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3603 %! assert (C{2}, 0.12, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3604 %! assert (C{3}, 3);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3605
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3606 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3607 %! C = textscan ("123.123", "%4f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3608 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3609 %! assert (C{2}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3610
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3611 %% field width interrupts exponent. (Matlab incorrectly gives [12, 2e12])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3612 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3613 %! assert (textscan ("12e12", "%4f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3614 %! assert (textscan ("12e+12", "%5f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3615 %! assert (textscan ("125e-12","%6f"), {[12.5; 2]});
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 %% %[] tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3618 %% Plain [..] and *[..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3619 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3620 %! ar = "abcdefguvwxAny\nacegxyzTrailing\nJunk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3621 %! C = textscan (ar, "%[abcdefg] %*[uvwxyz] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3622 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3623 %! assert (C{2}, {"Any"; "Trailing"; "Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3624
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3625 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3626 %! 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
3627
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3628 %% [^..] and *[^..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3629 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3630 %! br = "abcdefguvwx1Any\nacegxyz2Trailing\n3Junk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3631 %! C = textscan (br, "%[abcdefg] %*[^0123456789] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3632 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3633 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3634
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3635 %% [..] and [^..] containing delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3636 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3637 %! 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
3638 %! C = textscan (cr, "%[ abcdefg] %*[^0123456789] %s", "delimiter", " \n", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3639 %! assert (C{1}, {"ab cd efg"; "a ce g"; " "});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3640 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3641
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3642 %% Bug #36464
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3643 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3644 %! 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
3645
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3646 %% test %[]] and %[^]]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3647 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3648 %! assert (textscan ('345]', "%*[123456]%[]]"){1}{1}, "]");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3649 %! assert (textscan ('345]', "%*[^]]%s"){1}{1}, "]");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3650
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3651 %% Test that "-i" checks the next two characters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3652 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3653 %! 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
3654 %! assert (C, {-i, -i, {"n"}, -Inf, -Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3655
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3656 %% Again for "+i", this time with custom parser
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3657 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3658 %! 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
3659 %! assert (C, {i, i, {"n"}, Inf, Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3660
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3661 %% Check single quoted format interprets control sequences
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3662 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3663 %! 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
3664 %! assert (C, {1, {"2"}, 3, 4});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3665
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3666 %% Check overflow and underflow of integer types
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3667 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3668 %! a = "-1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3669 %! b = "1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3670 %! fmt = "%d8 %d16 %d32 %d64 %u8 %u16 %u32 %u64 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3671 %! 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
3672 %! assert (C{1}, int8 ([-128; 127]));
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3673 %! assert (C{2}, int16 ([-32768; 32767]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3674 %! assert (C{3}, int32 ([-2147483648; 2147483647]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3675 %! assert (C{4}, int64 ([-9223372036854775808; 9223372036854775807]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3676 %! assert (C{5}, uint8 ([0; 255]));
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3677 %! assert (C{6}, uint16 ([0; 65535]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3678 %! assert (C{7}, uint32 ([0; 4294967295]));
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3679 %! assert (C{8}, uint64 ([0; 18446744073709551615]));
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3680
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3681 %% 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
3682 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3683 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3684 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3685 %! 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
3686 %! 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
3687 %! 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
3688 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3689 %! 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
3690 %! %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
3691 %! assert (C{2}, {"Level1";"Level2";"Level3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3692 %! 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
3693 %! assert (C{4}, [int8(45);int8(60);int8(12)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3694 %! assert (C{5}, [uint32(4294967295);uint32(4294967295);uint32(200000)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3695 %! assert (C{6}, [inf;-inf;10]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3696 %! assert (C{7}, [NaN;0.001;100], eps);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3697 %! assert (C{8}, {"Yes";"No";"No"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3698 %! 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
3699 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3700 %! 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
3701 %! assert (C{2}, [int32(1);int32(2);int32(3)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3702 %! 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
3703 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3704 %! C = textscan (fid,'%s %*[^\n]');
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3705 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3706 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3707 %! 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
3708
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3709 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3710 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3711 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3712 %! fprintf (fid,"1, 2, 3, 4, , 6\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3713 %! fprintf (fid,"7, 8, 9, , 11, 12\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3714 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3715 %! 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
3716 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3717 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3718 %! assert (C{4}, [4; -Inf]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3719 %! assert (C{5}, uint8 ([0; 11]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3720
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3721 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3722 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3723 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3724 %! fprintf (fid,"abc, 2, NA, 3, 4\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3725 %! fprintf (fid,"// Comment Here\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3726 %! fprintf (fid,"def, na, 5, 6, 7\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3727 %! fseek (fid, 0, "bof");
21491
ab0a19882615 * textscan.h, textscan.cc: More style fixes
Mike Miller <mtmiller@octave.org>
parents: 21490
diff changeset
3728 %! 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
3729 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3730 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3731 %! assert (C{1}, {"abc";"def"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3732 %! assert (C{2}, [2; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3733 %! assert (C{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3734 %! assert (C{4}, [3; 6]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3735 %! assert (C{5}, [4; 7]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3736
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 start of comment as string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3739 %! c = textscan ("1 / 2 // 3", "%n %s %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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3743 // 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
3744 #if 0
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3745 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3746 %!## Test unfinished comment
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3747 %! c = textscan ("1 2 /* half comment", "%n %u8", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3748 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3749
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3750 ## Test reading from a real file
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3751 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3752 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3753 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3754 %! d = rand (1, 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3755 %! fprintf (fid, " %f %f /* comment */ %f %f ", d);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3756 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3757 %! A = textscan (fid, "%f %f", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3758 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3759 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3760 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3761 %! assert (A{1}, [d(1); d(3)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3762 %! assert (A{2}, [d(2); d(4)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3763 %! assert (E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3764 #endif