annotate libinterp/corefcn/textscan.cc @ 21490:243b04c97b56

textscan: Use references rather than pointers to stream objects * textscan.cc, textscan.h (textscan::scan, textscan::do_scan): Accept a reference to a std::istream rather than a pointer. (Ftextscan): Pass streams by reference.
author Mike Miller <mtmiller@octave.org>
date Fri, 18 Mar 2016 16:36:40 -0700
parents 9dbc8f8bc2d7
children ab0a19882615
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.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
77 int get (void) { return delimited ? *idx++ : get_undelim (); }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
78
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
79 // 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
80 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
81
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
82 // 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
83 int peek (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
84
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
85 // 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
86 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
87
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
88 // 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
89 // 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
90 // 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
91 // get, get_delim, field_done, refresh_buf, getline, read or seekg.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
92 void putback (char /*ch*/ = 0) { --idx; }
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 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
95
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
96 // 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
97
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
98 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
99
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
100 // 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
101 // 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
102 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
103
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
104 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
105
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
106 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
107 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
108 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
109 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
110
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
111 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
112
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
113 bool 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
114
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
115 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
116
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
117 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
118
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
119 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
120 = (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
121 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
122 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
123 }
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 // 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
126 // (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
127
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
128 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
129
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
130 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
131
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
132 private:
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 // 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
135 int bufsize;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
136
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
137 // 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
138 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
139
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
140 // 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
141 char *buf;
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 // 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
144 char *idx;
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 // 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
147 // 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
148 char *last;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
149
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
150 // 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
151 char *eob;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
152
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
153 // 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
154 bool delimited;
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 // 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
157 int longest;
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 // 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
160 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
161
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
162 // 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
163 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
164
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
165 // 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
166 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
167
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
168 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
169
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
170 // No copying!
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
171
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
172 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
173
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
174 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
175 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
176
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
177 // 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
178 // 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
179 // 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
180
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
181 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
182 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
183 int longest_lookahead,
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
184 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
185 : 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
186 delims (delimiters),
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
187 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
188 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
189 buf = new char[bufsize];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
190 eob = buf + bufsize;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
191 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
192 progress_marker = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
193 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
194 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
195
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
196 // 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
197 // 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
198 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
199 const delimited_stream& ds)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
200 : 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
201 delims (ds.delims),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
202 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
203 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
204 buf = new char[bufsize];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
205 eob = buf + bufsize;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
206 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
207 progress_marker = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
208 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
209 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
210
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
211 delimited_stream::~delimited_stream (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
212 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
213 // Seek to the correct position in i_stream.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
214 if (!eof ())
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 i_stream.clear ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
217 i_stream.seekg (buf_in_file);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
218 i_stream.read (buf, idx - buf);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
219 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
220
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
221 delete [] buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
222 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
223
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
224 // 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
225 // if necessary.
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 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
228 delimited_stream::get_undelim (void)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
229 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
230 int retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
231 if (eof ())
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 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
234 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
235 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
236
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
237 if (idx < eob)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
238 retval = *idx++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
239 else
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
240 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
241 refresh_buf ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
242
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
243 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
244 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
245 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
246 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
247 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
248 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
249 retval = *idx++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
250 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
251
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
252 if (idx >= last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
253 delimited = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
254
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
255 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
256 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
257
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
258 // 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
259 // 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
260
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
261 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
262 delimited_stream::peek_undelim (void)
21467
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 int retval = get_undelim ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
265 putback ();
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 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
268 }
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 // 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
271 // 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
272 // 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
273 // processed.
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 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
276 delimited_stream::refresh_buf (void)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
277 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
278 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
279 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
280
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
281 int retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
282 int old_remaining = eob - idx;
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 (old_remaining < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
285 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
286 idx = eob;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
287 old_remaining = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
288 }
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 octave_quit (); // allow ctrl-C
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 if (old_remaining > 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
293 memmove (buf, idx, old_remaining);
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 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
296 idx = buf;
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 int gcount; // chars read
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
299 if (! i_stream.eof ())
21467
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 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
302 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
303 gcount = i_stream.gcount ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
304 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
305 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
306 gcount = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
307
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
308 eob = buf + old_remaining + gcount;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
309 last = eob;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
310 if (gcount == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
311 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
312 delimited = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
313
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
314 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
315 retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
316 else
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
317 // 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
318 retval = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
319 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
320 else
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
321 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
322 delimited = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
323
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
324 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
325 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
326 if (strchr (delims.c_str (), *last))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
327 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
328 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
329
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
330 if (last - buf < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
331 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
332
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
333 retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
334 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
335
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
336 // 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
337 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
338 *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
339
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
340 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
341 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
342
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
343 // 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
344 // 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
345 // 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
346 // 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
347 // 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
348
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
349 char *
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
350 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
351 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
352 char *retval;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
353
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
354 if (eob - idx > size)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
355 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
356 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
357 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
358 if (idx > last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
359 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
360 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
361 else
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
362 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
363 // 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
364 // 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
365 // 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
366 // 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
367
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
368 if (eob - prior_tell + size < bufsize)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
369 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
370 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
371 idx = prior_tell;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
372 refresh_buf ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
373 idx += gap;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
374 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
375 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
376 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
377 refresh_buf ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
378 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
379
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
380 prior_tell = buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
381
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
382 if (eob - idx > size)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
383 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
384 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
385 idx += size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
386 if (idx > last)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
387 delimited = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
388 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
389 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
390 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
391 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
392 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
393 retval = idx;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
394 memset (eob, 0, size + (idx - buf));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
395 idx += size;
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 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
398 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
399 retval = buffer;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
400 // FIXME -- read bufsize at a time
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
401 int i;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
402 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
403 *buffer++ = get_undelim ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
404 if (eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
405 memset (buffer, 0, size - i);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
406 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
407 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
408 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
409
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
410 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
411 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
412
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
413 // 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
414 // must have length at least 1.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
415
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
416 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
417 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
418 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
419 int len = out.length (), used = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
420 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
421 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
422 && ch != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
423 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
424 out[used++] = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
425 if (used == len)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
426 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
427 len <<= 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
428 out.resize (len);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
429 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
430 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
431 out.resize (used);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
432 field_done ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
433
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
434 return ch;
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
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
437 // 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
438
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
439 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
440 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
441 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
442 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
443
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
444 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
445 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
446 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
447 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
448 };
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 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
451 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
452 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
453 : 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
454 char_class (ch_class), type (typ), discard (dis),
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
455 numeric(typ == 'd' || typ == 'u' || type == 'f' || type == 'n')
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
456 { }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
457
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
458 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
459 : 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
460 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
461 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
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& 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
465 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
466 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
467 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
468 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
469 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
470 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
471 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
472 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
473 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
474 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
475 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
476 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
477
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
478 return *this;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
479 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
480
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
481 ~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
482
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
483 // 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
484 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
485
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
486 // The 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
487 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
488
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
489 // The maximum 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
490 // 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
491 int prec;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
492
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
493 // The 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
494 // 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
495 int bitwidth;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
496
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
497 // The 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
498 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
499
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
500 // 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
501 // -- `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
502 char type;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
503
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
504 // 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
505 bool discard;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
506
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
507 // TRUE if 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
508 bool numeric;
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
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
511 class textscan;
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 // 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
514
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
515 class
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
516 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
517 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
518 public:
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
519
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
520 textscan_format_list (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
521
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 (void);
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 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
525
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
526 // 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
527 // 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
528 // 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
529 // last conversion.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
530
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
531 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
532
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
533 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
534 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
535 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
536 return current ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
537 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
538
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 *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
540 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
541 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
542 }
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 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
545 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
546 curr_idx++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
547
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
548 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
549 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
550 if (cycle)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
551 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
552 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
553 return 0;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
554 }
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 return current ();
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
557 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
558
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
559 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
560
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
561 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
562
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
563 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
564
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
565 // 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
566 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
567
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
568 // 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
569 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
570
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
571 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
572
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
573 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
574
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
575 private:
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 // 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
578 // 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
579 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
580
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
581 // 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
582 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
583
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
584 // 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
585 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
586
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
587 // list holding column arrays of types specified by conversions
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
588 std::list<octave_value > output_container;
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 // Temporary buffer.
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
591 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
592
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
593 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
594 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
595 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
596 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
597
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
598 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
599
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
600 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
601 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
602 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
603 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
604 // No copying!
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 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
607
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
608 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
609 };
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
610
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
611
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
612 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
613 : 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
614 fmt_elts (), buf (0)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
615 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
616 size_t n = s.length ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
617
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
618 size_t i = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
619
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
620 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
621 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
622 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
623 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
624 char type = '\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 bool have_more = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
627
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
628 if (s.length () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
629 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
630 buf = new std::ostringstream ("%f");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
631 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
632 type = 'f';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
633 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
634 discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
635 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
636 set_from_first = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
637 nconv = 1;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
638 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
639 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
640 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
641 set_from_first = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
642
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
643 while (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
644 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
645 have_more = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
646
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
647 if (! buf)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
648 buf = new std::ostringstream ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
649
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
650 if (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
651 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
652 // Process percent-escape conversion type.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
653
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
654 process_conversion (s, i, n);
21467
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 have_more = (buf != 0);
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 else if (isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
659 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
660 while (++i < n && isspace (s[i]))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
661 /* skip whitespace */;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
662
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
663 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
664 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
665 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
666 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
667 type = textscan_format_elt::literal_conversion;
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 width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
670 prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
671 bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
672 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
673
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
674 while (i < n && ! isspace (s[i])
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
675 && (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
676 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
677 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
678 i++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
679 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
680 width++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
681 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
682
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
683 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
684 discard, type);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
685
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
686 have_more = false;
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
689 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
690 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
691 have_more = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
692 break;
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 }
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 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
698 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
699
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
700 delete buf;
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 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
704 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
705 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
706
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
707 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
708 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
709 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
710 delete elt;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
711 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
712 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
713
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
714 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
715 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
716 int bitwidth, octave_value val_type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
717 bool discard, char type,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
718 const std::string& char_class)
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 if (buf)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
721 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
722 std::string text = buf->str ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
723
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
724 if (! text.empty ())
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 textscan_format_elt *elt
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
727 = 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
728 discard, type, char_class);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
729
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
730 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
731 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
732
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
733 fmt_elts.push_back (elt);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
734 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
735
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
736 delete buf;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
737 buf = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
738 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
739 }
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 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
742 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
743 size_t n)
21467
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 unsigned width = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
746 int prec = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
747 int bitwidth = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
748 bool discard = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
749 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
750 char type = '\0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
751
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
752 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
753
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
754 bool have_width = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
755
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
756 while (i < n)
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 switch (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 case '*':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
761 if (discard)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
762 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
763 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
764 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
765 discard = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
766 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
767 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
768 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
769
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
770 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
771 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
772 if (have_width)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
773 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
774 else
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 char c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
777 width = width * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
778 have_width = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
779 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
780 while (i < n && isdigit (s[i]))
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 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 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
785 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
786
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
787 if (i < n && s[i] == '.')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
788 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
789 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
790 prec = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
791 while (i < n && isdigit (s[i]))
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 c = s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
794 prec = prec * 10 + c - '0';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
795 *buf << c;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
796 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
797 }
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 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
800
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
801 case 'd': case 'u':
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 bool done = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
804 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
805 if (i < n)
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 if (s[i] == '8')
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 bitwidth = 8;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
810 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
811 val_type = octave_value (int8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
812 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
813 val_type = octave_value (uint8NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
814 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
815 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
816 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
817 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
818 bitwidth = 16;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
819 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
820 val_type = octave_value (int16NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
821 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
822 val_type = octave_value (uint16NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
823 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
824 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
825 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
826 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
827 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
828 done = false; // use default size below
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] == '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
833 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
834 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
835 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
836 val_type = octave_value (int64NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
837 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
838 val_type = octave_value (uint64NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
839 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
840 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
841 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
842 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
843 done = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
844 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
845 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
846 done = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
847
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
848 if (! done)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
849 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
850 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
851 if (type == 'd')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
852 val_type = octave_value (int32NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
853 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
854 val_type = octave_value (uint32NDArray ());
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 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
857 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
858
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
859 case 'f':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
860 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
861 bitwidth = 64;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
862 if (i < n)
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 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
865 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
866 bitwidth = 32;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
867 val_type = octave_value (FloatNDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
868 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
869 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
870 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
871 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
872 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
873 val_type = octave_value (NDArray ());
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
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
878 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
879 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
880 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
881 val_type = octave_value (NDArray ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
882 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
883
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
884 case 'n':
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
885 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
886 bitwidth = 64;
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 's': case 'q': case '[': case 'c':
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
891 if (! discard)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
892 val_type = octave_value (Cell ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
893 *buf << (type = s[i++]);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
894 has_string = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
895 goto fini;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
896
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
897 fini:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
898 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
899 if (! have_width)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
900 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
901 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
902 width = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
903 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
904 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
905 }
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 (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
908 discard, type) == 0)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
909 return;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
910 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
911 break;
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 default:
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
914 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
915 }
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 if (nconv < 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
918 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
919 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
920
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
921 nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
922 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
923
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
924 // Parse [...] and [^...]
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 // 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
927 // 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
928 // 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
929 // 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
930 // pattern.
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 // 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
933 // 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
934 // The latter is efficient for [^...] patterns.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
935
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
936 static std::string
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
937 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
938 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
939 int len = pattern.length ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
940 if (len == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
941 return "";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
942
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
943 std::string retval (256, '\0');
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
944 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
945
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
946 int in = 0, out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
947 unsigned char ch, prev = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
948 bool flip = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
949
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
950 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
951 if (ch == '^')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
952 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
953 in++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
954 flip = true;
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 mask[pattern[in]] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
957 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
958
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
959 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
960 bool prev_prev_was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
961 for (; in < len; in++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
962 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
963 bool was_range = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
964 ch = pattern[in];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
965 if (ch == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
966 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
967
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
968 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
969 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
970 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
971 if (start_of_range < ch
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
972 && ((isupper (ch) && isupper (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
973 || (islower (ch) && islower (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
974 || (isdigit (ch) && isdigit (start_of_range))
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
975 || mask['-'] > 1)) // not the first '-'
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
976 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
977 was_range = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
978 out--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
979 mask['-']--;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
980 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
981 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
982 if (mask[i] == '\0')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
983 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
984 mask[i] = '\1';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
985 retval[out++] = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
986 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
987 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
988 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
989 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
990 if (! was_range)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
991 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
992 if (mask[ch]++ == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
993 retval[out++] = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
994 else if (ch != '-')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
995 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
996 "textscan: [...] contains two '%c's", ch);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
997
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
998 if (prev == '-' && mask['-'] >= 2)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
999 warning_with_id ("octave:textscan-pattern",
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1000 "textscan: [...] contains two '-'s "
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1001 "outside range expressions");
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1002 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1003 prev = ch;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1004 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
1005 prev_was_range = was_range;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1006 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1007
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1008 if (flip) // [^...]
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1009 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1010 out = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1011 for (int i = 0; i < 256; i++)
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1012 if (! mask[i])
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1013 retval[out++] = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1014 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1015
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1016 retval.resize (out);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1017
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1018 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1019 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1020
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1021 int
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1022 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
1023 size_t n, unsigned int& width,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1024 int& prec, int& bitwidth,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1025 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
1026 char& type)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1027 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1028 int retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1029
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1030 std::string char_class;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1031
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1032 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
1033 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
1034
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1035 if (type != '%')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1036 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1037 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1038 if (type == '[')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1039 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1040 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1041 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1042 beg_idx = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1043
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1044 if (s[i] == '^')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1045 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1046 type = '^';
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1047 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1048
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1049 if (i < n)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1050 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1051 beg_idx = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1052
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1053 if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1054 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1055 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1056 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1057 else if (s[i] == ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1058 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1059 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1060
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1061 while (i < n && s[i] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1062 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1063
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1064 if (i < n && 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 end_idx = i-1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1067 *buf << s[i++];
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1068 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1069
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1070 if (s[i-1] != ']')
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1071 retval = nconv = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1072 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1073 }
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 if (nconv >= 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1076 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1077 if (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
1078 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
1079 end_idx - beg_idx + 1));
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 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
1082 char_class);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1083 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1084
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1085 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1086 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1087
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1088 void
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1089 textscan_format_list::printme (void) const
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1090 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1091 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
1092
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1093 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
1094 {
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1095 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
1096
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1097 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1098 << "width: " << elt->width << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1099 << "digits " << elt->prec << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1100 << "bitwidth: " << elt->bitwidth << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1101 << "discard: " << elt->discard << "\n"
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1102 << "type: ";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1103
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1104 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
1105 std::cerr << "literal text\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1106 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
1107 std::cerr << "whitespace\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1108 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1109 std::cerr << elt->type << "\n";
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1110
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1111 std::cerr
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1112 << "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
1113 << "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
1114 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1115 }
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 // 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
1118 // 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
1119
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1120 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1121 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
1122 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1123 // 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
1124 std::string first_line (20, ' ');
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1125
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1126 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
1127
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1128 if (first_line.length () > 0
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1129 && 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
1130 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
1131
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1132 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
1133 delimited_stream ds (strstr, is);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1134
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1135 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
1136 Complex val;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1137 octave_value val_type;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1138 nconv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1139 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
1140 int retval = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1141
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1142 // 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
1143 while (! ds.eof ())
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1144 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1145 bool already_skipped_delim = false;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1146 ts.skip_whitespace (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1147 ds.progress_benchmark ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1148 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
1149 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
1150 if (ds.fail ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1151 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1152 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
1153
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1154 if (ds.eof ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1155 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1156
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1157 // If 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
1158 // 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
1159 // return with an error status.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1160 if (ts.return_on_error < 2)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1161 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1162 ts.skip_delim (ds);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1163 if (ds.no_progress ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1164 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1165 retval = 4;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1166 break;
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 already_skipped_delim = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1169 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1170 else // skip offending field
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1171 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1172 std::ios::iostate state = ds.rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1173 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
1174
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1175 std::string dummy;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1176 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
1177 ts.scan_string (ds, fe, dummy);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1178
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1179 progress = (dummy.length ());
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1180 ds.setstate (state);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1181 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1182
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1183 val = ts.empty_value.scalar_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1184
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1185 if (! --max_empty)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1186 break;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1187 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1188
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1189 if (val.imag () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1190 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
1191 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1192 val_type = octave_value (ComplexNDArray (dv, val));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1193
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1194 output_container.push_back (val_type);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1195
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1196 if (! already_skipped_delim)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1197 ts.skip_delim (ds);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1198
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1199 if (! progress && ds.no_progress ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1200 break;
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 nconv++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1203 }
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 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
1206
21485
0dc6140cab3d use std::deque instead of Array to hold textscan format elements
John W. Eaton <jwe@octave.org>
parents: 21484
diff changeset
1207 // 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
1208 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
1209 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
1210
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1211 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
1212 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1213
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1214 // 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
1215
21488
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1216 static Cell
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1217 init_inf_nan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1218 {
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1219 Cell retval (dim_vector (1, 2));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1220
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1221 retval(0) = Cell (octave_value ("inf"));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1222 retval(1) = Cell (octave_value ("nan"));
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1223
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1224 return retval;
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1225 }
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1226
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1227 textscan::textscan (void)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1228 : buf (), whitespace_table (), delim_table (), delims (),
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1229 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
1230 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
1231 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
1232 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
1233 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
1234 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
1235 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
1236 numeric_delim (false), lines (0)
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1237 { }
9dbc8f8bc2d7 move textscan constructor out of header file
John W. Eaton <jwe@octave.org>
parents: 21487
diff changeset
1238
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1239 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1240 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
1241 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1242 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
1243 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
1244
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1245 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
1246 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
1247 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
1248 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1249 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
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(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
1252 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
1253
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1254 params++;
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1255 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1256 else
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1257 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
1258 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
1259
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1260 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
1261
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1262 if (args.length () > 1)
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1263 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1264 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
1265 {
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1266 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
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 (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
1269 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
1270 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
1271
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1272 params++;
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 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1275
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1276 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
1277
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1278 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
1279
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1280 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
1281
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1282 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
1283 }
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1284
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1285 octave_value
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1286 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
1287 octave_idx_type ntimes)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1288 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1289 octave_value retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1290
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1291 if (fmt_list.num_conversions () == -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1292 error ("textscan: invalid format specified");
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1293
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1294 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1295 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
1296
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1297 // skip the first header_lines
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1298 std::string dummy;
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1299 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
1300 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
1301
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1302 // 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
1303
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1304 // 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
1305 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
1306 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
1307
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1308 // 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
1309 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
1310 if (buffer_size)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1311 buf_size = buffer_size;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1312 else if (ntimes > 0)
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 // 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
1315 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
1316 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
1317 }
21471
57dea7b5ca46 textscan.cc: avoid integer constants, raising compile time issues,
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 21469
diff changeset
1318 // Finally, create the stream.
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1319 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
1320
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1321 // 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
1322 // (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
1323 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
1324 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
1325 row_idx(1) = 0;
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 int err = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1328 octave_idx_type row = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1329
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1330 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
1331 skip_delim (is);
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 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
1334
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1335 // 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
1336 if (fmt_list.set_from_first)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1337 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1338 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
1339 lines = 1;
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 done_after = fmt_list.numel () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1342 if (! err)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1343 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
1344 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1345 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1346 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
1347
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1348 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
1349
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1350 // 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
1351 // Check now which columns to merge.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1352 // 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
1353 // after reading in data.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1354 // 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
1355 // 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
1356 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
1357 int conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1358 if (collect_output)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1359 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1360 int prev_type = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1361 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
1362 col != out.end (); col++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1363 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1364 if (col->type_id () == prev_type
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1365 || (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
1366 merge_with_prev [conv++] = true;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1367 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1368 merge_with_prev [conv++] = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1369
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1370 prev_type = col->type_id ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1371 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1372 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1373
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1374 // 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
1375 // infinite loop below.
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1376 if (fmt_list.num_conversions () == 0)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1377 error ("textscan: No conversions specified");
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 // 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
1380 if (! err)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1381 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1382 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
1383 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1384 if (row == 0 || row >= size)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1385 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1386 size += size+1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1387 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
1388 col != out.end (); col++)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1389 *col = (*col).resize (dim_vector (size, 1), 0);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1390 }
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 row_idx(0) = row;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1393 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
1394
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1395 if (err > 0 || ! is || (lines >= ntimes && ntimes > -1))
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1396 break;
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 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1399
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1400 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
1401 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
1402 done_after + 1, row + 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1403
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1404 // 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
1405 bool uneven_columns = false;
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1406 if (isp.eof () || (err & 4))
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1407 {
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1408 isp.clear ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1409 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
1410 int last_char = isp.get ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
1411 isp.setstate (isp.eofbit);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1412 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
1413 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1414
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1415 // convert return value to Cell array
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1416 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
1417
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1418 // (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
1419 // 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
1420 if (err & 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1421 done_after = out.size () + 1;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1422
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1423 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
1424 dim_vector dv (valid_rows, 1);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1425
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1426 ra_idx(0) = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1427 int i = 0;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1428 if (! collect_output)
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1429 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1430 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
1431 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
1432 col != out.end (); col++, i++)
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 // 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
1435 if (i == done_after && uneven_columns)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1436 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
1437
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1438 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1439 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
1440 ra_idx);
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 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1443 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
1444 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1445 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
1446 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
1447 int prev_type = -1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1448
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1449 conv = 0;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1450 retval = Cell ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1451 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
1452 col != out.end (); col++)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1453 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1454 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
1455 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1456 if (prev_type != -1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1457 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1458 ra_idx(1) = i++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1459 retval = do_cat_op (retval, octave_value (Cell(cur)),
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1460 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1461 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1462 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
1463 group_size = 1;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1464 prev_type = col->type_id ();
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 else
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1467 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1468 ra_idx(1) = group_size++;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1469 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
1470 ra_idx);
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1471 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1472 }
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1473 ra_idx(1) = i;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1474 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
1475 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1476
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1477 return retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1478 }
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1479
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1480 // 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
1481 // 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
1482
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1483 static double
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
1484 pown (double x, unsigned int n)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1485 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1486 double retval = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1487
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1488 for (unsigned int d = n; d; d >>= 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1489 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1490 if (d & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1491 retval *= x;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1492 x *= x;
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1495 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1496 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1497
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1498 // 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
1499 // exp_chars option of options.
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1500
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1501 double
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1502 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
1503 const textscan_format_elt& fmt) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1504 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1505 int sign = 1;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1506 unsigned int width_left = fmt.width;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1507 double retval = 0;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1508 bool valid = false; // syntactically correct double?
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1509
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1510 int ch = is.peek ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1511
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1512 if (ch == '+')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1513 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1514 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1515 ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1516 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1517 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1518 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1519 else if (ch == '-')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1520 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1521 sign = -1;
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1528 // Read integer part
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1529 if (ch != '.')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1530 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1531 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
1532 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1533 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
1534 retval = retval * 10 + (ch - '0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1535 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1536 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1537
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1538 // Read fractional part, up to specified precision
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1539 if (ch == '.' && width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1540 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1541 double multiplier = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1542 int precision = fmt.prec;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1543 int i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1544
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1545 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1546 width_left--; // Consider width of '.'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1547
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1548 if (precision == -1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1549 precision = 1<<30; // FIXME Should be MAXINT
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1550
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1551 if (! valid) // if there was nothing before '.'...
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1552 is.get (); // ...ch was a "peek", not "get".
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1553
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1554 for (i = 0; i < precision; i++)
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 (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1557 retval += (ch - '0') * (multiplier *= 0.1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1558 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1559 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1560 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1561 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1562 }
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1565 // 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
1566 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
1567 && ch <= '9')
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1568 retval += multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1569
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1570 if (i > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1571 valid = true; // valid if at least one digit after '.'
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 // 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
1574 if (i == precision)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1575 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
1576 ; // discard
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1577
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1578 width_left++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1579 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1580
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1581 // 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
1582 const char *ec = exp_chars.c_str ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1583 bool used_exp = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1584 if (valid && width_left > 1 && strchr (ec, ch))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1585 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1586 int ch1 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1587 if (ch1 == '-' || ch1 == '+' || (ch1 >= '0' && ch1 <= '9'))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1588 { // 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
1589 width_left--; // count "E"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1590 int exp = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1591 int exp_sign = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1592 if (ch1 == '+')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1593 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1594 if (width_left)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1595 width_left--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1596 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1597 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1598 else if (ch1 == '-')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1599 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1600 exp_sign = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1601 is.get ();
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1605 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1606 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
1607 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1608 exp = exp*10 + ch - '0';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1609 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1610 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1611 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
1612 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
1613 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1614
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1615 double multiplier = pown (10, exp);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1616 if (exp_sign > 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1617 retval *= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1618 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1619 retval /= multiplier;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1620
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1621 used_exp = true;
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1624 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
1625 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
1626 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1627
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1628 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1629 if (! valid && width_left >= 3)
21462
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 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
1632 if (i == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1633 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1634 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1635 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1636 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1637 else if (i == 1)
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 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1640 valid = true;
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1643
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1644 // Check for +/- inf and NaN
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1645 if (! valid && width_left >= 3)
21462
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 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
1648 if (i == 0)
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 retval = octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1651 valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1652 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1653 else if (i == 1)
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 retval = octave_NaN;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1656 valid = true;
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1659
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1660 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1661 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1662 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1663 is.setstate (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1664
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1665 return retval * sign;
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1668 // 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
1669 // 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
1670 // 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
1671
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1672 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1673 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
1674 Complex& val) const
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1675 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1676 double im = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1677 double re = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1678 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
1679 bool inf = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1680
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1681 int ch = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1682 if (ch == '+' || ch == '-') // check for [+-][ij] with no coefficients
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 ch = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1685 int ch2 = is.peek ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1686 if (ch2 == 'i' || ch2 == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1687 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1688 double value = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1689 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1690 // Check not -inf
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1691 if (is.peek () == 'n')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1692 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1693 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1694 std::ios::iostate state = is.rdstate ();
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 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1697 ch2 = is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1698 if (ch2 == 'f')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1699 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1700 inf = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1701 re = (ch == '+') ? octave_Inf : -octave_Inf;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1702 value = 0;
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 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1705 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1706 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1707 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1708 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1709 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1710
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1711 im = (ch == '+') ? value : -value;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1712 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1713 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1714 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1715 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1716
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1717 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
1718 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1719 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1720 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1721 //re = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1722 re = read_double (is, fmt);
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 // check for "treat as empty" string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1725 if (treat_as_empty.numel ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1726 && (is.fail () || octave_is_NaN_or_NA (Complex (re))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1727 || re == octave_Inf))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1728 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1729
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1730 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
1731 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1732 if (ch == treat_as_empty (i).string_value ()[0])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1733 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1734 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
1735 break;
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 if (as_empty) // if first char matched...
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 as_empty = false; // ...look for the whole string
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 is.clear (state); // treat_as_empty "-" causes partial read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1743 is.seekg (pos); // reset to position before failed read
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 // treat_as_empty strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1746 // 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
1747 // that matches.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1748 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
1749 // prefill, in case EOF means part-filled.
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1750 memset (look_buf, '\0', treat_as_empty_len);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1751 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
1752
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1753 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1754 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1755 // FIXME -- is.read could invalidate pos
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1756
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1757 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
1758 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1759 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
1760 if (! strncmp (s.c_str (), look, s.size ()))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1761 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1762 as_empty = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1763 // read just the right amount
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1764 is.read (look_buf, s.size (), pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1765 break;
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1768 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1769 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1770
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1771 if (! is.eof () && ! as_empty)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1772 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1773 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
1774 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1775 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
1776 if (ch == 'i' || ch == 'j') // pure imaginary
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 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1779 im = re;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1780 re = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1781 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1782 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
1783 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1784 // 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
1785 pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1786 state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1787
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1788 //im = octave_read_value<double> (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1789 im = read_double (is, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1790 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1791 im = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1792
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1793 if (is.peek () == 'i' || is.peek () == 'j')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1794 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1795 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1796 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1797 im = 0; // no valid imaginary part. Restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1798 is.clear (state); // eof shouldn't cause fail.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1799 is.seekg (pos);
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1802 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
1803 is.clear (state); // failed peek shouldn't cause fail
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1806 if (as_empty)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1807 val = empty_value.scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1808 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1809 val = Complex (re, im);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1810 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1811
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
1812 // 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
1813
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1814 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1815 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
1816 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1817 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1818 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1819 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1820
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1821 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
1822 ? 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
1823 : 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
1824 != std::istream::traits_type::eof ())
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1825 && ! strchr (pattern, c1))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1826 obuf << static_cast<char> (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1827
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1828 val = obuf.str ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1829
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1830 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1831 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1832
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1833 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1834 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1835
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1836 // 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
1837 // 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
1838
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1839 std::string
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1840 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
1841 const std::string& ends) const
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 std::string retval ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1844 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1845 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1846 { // find sequence ending with an ending char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1847 std::string next;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1848 scan_caret (is, ends.c_str (), next);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1849 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
1850
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1851 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
1852 ? 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
1853
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1854 if (last != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1855 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1856 retval = retval + static_cast<char> (last);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1857 for (int i = 0; i < delimiters.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1858 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1859 std::string delim = delimiters(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1860 int start = retval.length () - delim.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1861 if (start < 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1862 start = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1863 std::string may_match = retval.substr (start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1864 if (may_match == delim)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1865 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1866 done = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1867 retval = retval.substr (0, start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1868 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1869 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1870 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1871 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1872 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1873 while (! done && is && ! is.eof ());
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1874
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1875 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1876 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1877
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1878
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1879 // 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
1880 // 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
1881 // 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
1882
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1883 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1884 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
1885 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1886 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1887 if (delim_list.numel () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1888 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1889 unsigned int i = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1890 unsigned int width = fmt.width;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1891
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1892 for (i = 0; i < width; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1893 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1894 if (i+1 > val.length ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1895 val = val + val + ' '; // grow even if empty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1896 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
1897 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
1898 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1899 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1900 break;
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 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1903 val[i] = ch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1904 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1905 val = val.substr (0, i); // trim pre-allocation
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 else // Cell array of multi-character delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1908 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1909 std::string ends ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1910 for (int i = 0; i < delim_list.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1911 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1912 std::string tmp = delim_list(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1913 ends += tmp.substr (tmp.length () - 1);
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 val = textscan::read_until (is, delim_list, ends);
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1918
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1919 // 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
1920
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1921 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1922 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
1923 std::string& val) const
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1924 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
1925 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1926 std::ostringstream obuf; // Is this optimised for growing?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1927
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1928 while (is && strchr (pattern, (c1 = is.get_undelim ())))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1929 obuf << static_cast<char> (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1930
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1931 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
1932 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1933 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1934 return c1;
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
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1937 // 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
1938 // 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
1939 // 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
1940
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1941 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1942 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
1943 std::string& val)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1944 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1945 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1946
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1947 if (is.peek () != '\"')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1948 scan_string (is, fmt, val);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1949 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1950 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1951 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1952 scan_caret (is, "\"", val); // read everything until "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1953 is.get (); // swallow "
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 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
1956 { // and keep looking for single "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1957 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1958 std::string val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1959 scan_caret (is, "\"", val1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1960 val = val + "\"" + val1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1961 is.get_undelim ();
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1964 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1965
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1966 // 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
1967 // including any whitespace or delimiters.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1968
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1969 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1970 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
1971 std::string& val) const
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 val.resize (fmt.width);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1974
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1975 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
1976 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1977 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
1978 if (ch != std::istream::traits_type::eof ())
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1979 val[i] = ch;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1980 else
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1981 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1982 val.resize (i);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
1983 break;
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 }
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1986 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1987
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1988
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
1989 // 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
1990
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1991 void
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
1992 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
1993 octave_value& ov, Array<octave_idx_type> row)
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 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1996
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1997 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1998
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
1999 octave_value val;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2000 if (fmt.numeric)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2001 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2002 if (fmt.type == 'f' || fmt.type == 'n')
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2003 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2004 Complex v;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2005 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2006 scan_complex (is, fmt, v);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2007
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2008 if (! fmt.discard && ! is.fail ())
21462
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.bitwidth == 64)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2011 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2012 if (ov.is_real_type () && v.imag () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2013 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
2014 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2015 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2016 if (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2017 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2018 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2019 ov.internal_rep ()->fast_elem_insert (row(0), v);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2020 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2021 }
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 () && v.imag () == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2025 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2026 float (v.real ()));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2027 else
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 if (ov.is_real_type ()) // cat does type conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2030 ov = do_cat_op (ov, octave_value (v), row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2031 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2032 ov.internal_rep ()->fast_elem_insert (row(0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2033 FloatComplex (v));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2034 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2035 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2036 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2037 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2038 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2039 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2040 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
2041 // 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
2042 // Some loss of precision for d64 and u64.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2043 skip_whitespace (is);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2044 v = read_double (is, fmt);
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2045 if (! fmt.discard && ! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2046 switch (fmt.bitwidth)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2047 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2048 case 64:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2049 switch (fmt.type)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2050 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2051 case 'd':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2052 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2053 octave_int64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2054 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
2055 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2056 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2057
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2058 case 'u':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2059 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2060 octave_uint64 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2061 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
2062 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2063 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2064 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2065 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2066
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2067 case 32:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2068 switch (fmt.type)
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2069 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2070 case 'd':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2071 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2072 octave_int32 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2073 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
2074 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2075 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2076
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2077 case 'u':
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2078 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2079 octave_uint32 vv = v;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2080 ov.internal_rep ()->fast_elem_insert (row(0), vv);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2081 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2082 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2083 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2084 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2085
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2086 case 16:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2087 if (fmt.type == 'd')
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2088 {
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2089 octave_int16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2090 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
2091 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2092 else
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 octave_uint16 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2095 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
2096 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2097 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2098
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2099 case 8:
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2100 if (fmt.type == 'd')
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_int8 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 else
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 octave_uint8 vv = v;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2108 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
2109 }
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2110 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2111 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2112 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2113
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2114 if (is.fail ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2115 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2116 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2117 ov = do_cat_op (ov, empty_value, row);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2118
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2119 // 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
2120 if (return_on_error == 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2121 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2122 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2123 is.clear (); // clear to allow read pointer to advance
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2124
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2125 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2126 scan_string (is, fmt, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2127
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2128 is.setstate (state);
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2131
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2132 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2133 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2134 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2135 std::string vv (" "); // initial buffer. Grows as needed
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2136 switch (fmt.type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2137 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2138 case 's':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2139 scan_string (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2140 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2141
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2142 case 'q':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2143 scan_qstring (is, fmt, vv);
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2144 break;
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2145
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2146 case 'c':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2147 scan_cstring (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 '[':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2151 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
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 '^':
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2155 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
2156 break;
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2157 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2158
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2159 if (! fmt.discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2160 ov.internal_rep ()->fast_elem_insert (row (0),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2161 Cell (octave_value (vv)));
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2162
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2163 // 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
2164 if (vv.length () != 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2165 is.clear (is.rdstate () & ~std::ios_base::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2166 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2167
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2168 is.field_done ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2169 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2170
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2171 // 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
2172 // values in row ROW of retval.
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2173
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2174 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2175 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
2176 textscan_format_list& fmt_list,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2177 std::list<octave_value> & retval,
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2178 Array<octave_idx_type> row, int& done_after)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2179 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2180 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
2181 std::list<octave_value>::iterator out = retval.begin ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2182 bool no_conversions = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2183 bool done = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2184 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
2185
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2186 octave_quit ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2187
21486
4581d0d52948 avoid signed/unsigned comparison warnings introduced in previous change
John W. Eaton <jwe@octave.org>
parents: 21485
diff changeset
2188 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
2189 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2190 bool this_conversion_failed = false;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2191
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2192 // Clear fail of previous numeric conversions.
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2193 is.clear ();
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2194
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2195 switch (elem->type)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2196 {
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2197 case 'C':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2198 case 'D':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2199 std::cerr << "textscan: Conversion %" << elem->type
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2200 << " not yet implemented\n";
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2201 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2202
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2203 case 'u':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2204 case 'd':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2205 case 'f':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2206 case 'n':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2207 case 's':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2208 case '[':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2209 case '^':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2210 case 'q':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2211 case 'c':
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2212 scan_one (is, *elem, *out, row);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2213 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2214
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2215 case textscan_format_elt::literal_conversion :
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2216 match_literal (is, *elem);
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2217 break;
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2218
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2219 default:
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2220 error ("Unknown format element '%c'", elem->type);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2221 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2222
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2223 if (! is.fail ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2224 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2225 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2226 no_conversions = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2227 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2228 else
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 if (return_on_error < 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2231 this_conversion_failed = true;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2232
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2233 is.clear (is.rdstate () & ~std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2234 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2235
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2236 if (! elem->discard)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2237 out++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2238
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2239 elem = fmt_list.next ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2240 char *pos = is.tellg ();
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2241
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2242 // FIXME -- these conversions "ignore delimiters". Should they include
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2243 // 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
2244 if (elem->type != textscan_format_elt::literal_conversion
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2245 // && elem->type != '[' && elem->type != '^' && elem->type != 'c'
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 skip_delim (is);
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2248
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2249 if (this_conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2250 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2251 if (is.tellg () == pos && ! conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2252 { // done_after = first failure
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2253 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
2254 conversion_failed = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2255 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2256 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2257 this_conversion_failed = false;
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
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2260 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2261 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2262 if (! done)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2263 done_after = i+1;
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2264
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2265 // 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
2266 done = true;
21462
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 }
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2269
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2270 if (done)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2271 is.setstate (std::ios::eofbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2272
21468
7d06afd924be * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21467
diff changeset
2273 // 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
2274 if (is.eof ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2275 return (2 + no_conversions);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2276
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2277 if (conversion_failed)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2278 return (4 + no_conversions);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2279
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2280 return 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2281 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2282
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2283 void
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
2284 textscan::parse_options (const octave_value_list& args,
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2285 textscan_format_list& fmt_list)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2286 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2287 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
2288 int n = last;
21462
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 if (n & 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2291 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
2292
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2293 delim_len = 1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2294 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
2295 for (int i = 0; i < last; i += 2)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2296 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2297 if (! args(i).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2298 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
2299 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
2300
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2301 std::string param = args(i).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2302 std::transform (param.begin (), param.end (),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2303 param.begin (), ::tolower);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2304 if (param == "delimiter")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2305 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2306 bool invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2307 if (args(i+1).is_string ())
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 invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2310 have_delims = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2311 delims = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2312 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2313 else if (args(i+1).is_cell ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2314 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2315 invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2316 delim_list = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2317 delim_table = " "; // non-empty, to flag non-default delim
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2318
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2319 // 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
2320 for (int j = 0; j < delim_list.numel (); j++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2321 {
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2322 if (! delim_list(j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2323 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2324 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2325 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2326 octave_idx_type len = delim_list(j).string_value ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2327 .length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2328 delim_len = std::max (static_cast<int>(len), delim_len);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2329 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2330 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2331 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2332 if (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2333 error ("textscan: Delimiters must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2334 "cell array of strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2335 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2336 else if (param == "commentstyle")
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 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2339 { // check here for names like "C++", "C", "shell", ...?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2340 comment_style = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2341 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2342 else if (args(i+1).is_cell ())
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 comment_style = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2345 int len = comment_style.numel ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2346 if ((len >= 1 && ! comment_style (0).is_string ())
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2347 || (len >= 2 && ! comment_style (1).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2348 || (len >= 3))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2349 error ("textscan: CommentStyle must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2350 "cell array of one or two strings");
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 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2353 error ("textscan: CommentStyle must be either a string"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2354 " 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
2355 args(i+1).class_name ().c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2356
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2357 // 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
2358 // and which character do we look for?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2359 if (comment_style.numel () >= 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2360 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2361 comment_len = comment_style (0).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2362 comment_char = comment_style (0).string_value ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2363 }
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 else if (param == "treatasempty")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2366 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2367 bool invalid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2368 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2369 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2370 treat_as_empty = Cell (args(i+1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2371 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
2372 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2373 else if (args(i+1).is_cell ())
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 treat_as_empty = args(i+1).cell_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2376 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
2377 if (! treat_as_empty (j).is_string ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2378 invalid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2379 else
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 int k = treat_as_empty (j).string_value ().size ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2382 if (k > treat_as_empty_len)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2383 treat_as_empty_len = k;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2384 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2385 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2386 if (invalid)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2387 error ("textscan: TreatAsEmpty must be either a string or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2388 "cell array of one or two strings");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2389
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2390 // 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
2391 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2392 else if (param == "collectoutput")
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 (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2395 collect_output = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2396 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2397 error ("textscan: CollectOutput must be logical or numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2398 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2399 else if (param == "emptyvalue")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2400 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2401 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2402 empty_value = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2403 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2404 error ("textscan: EmptyValue must be numeric, not <%s>",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2405 args(i+1).class_name ().c_str ());
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 == "headerlines")
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 header_lines = 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: HeaderLines must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2413 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2414 else if (param == "bufsize")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2415 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2416 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2417 buffer_size = args(i+1).scalar_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2418 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2419 error ("textscan: BufSize must be numeric");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2420 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2421 else if (param == "multipledelimsasone")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2422 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2423 if (args(i+1).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2424 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2425 if (args(i+1).bool_value ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2426 multiple_delims_as_one = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2427 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2428 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2429 error ("textscan: MultipleDimsAsOne must be logical or numeric");
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 else if (param == "returnonerror")
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).is_numeric_type ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2434 return_on_error = args(i+1).bool_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2435 else if (args(i+1).is_string ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2436 && args(i+1).string_value () == "continue")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2437 return_on_error = 2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2438 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2439 error ("textscan: ReturnOnError must be logical or "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2440 "numeric, or 'continue'");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2441 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2442 else if (param == "whitespace")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2443 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2444 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2445 whitespace = args(i+1).string_value ();
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: Whitespace must be a character string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2448 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2449 else if (param == "expchars")
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2450 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2451 if (args(i+1).is_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2452 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2453 exp_chars = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2454 default_exp = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2455 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2456 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2457 error ("textscan: ExpChars must be a character string");
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 else if (param == "endofline")
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 bool valid = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2462 if (args(i+1).is_string ())
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 std::string s = args(i+1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2465 if (args(i+1).is_sq_string ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2466 s = do_string_escapes (s);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2467 int l = s.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2468 if (l == 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2469 eol1 = eol2 = -2;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2470 else if (l == 1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2471 eol1 = eol2 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2472 else if (l == 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2473 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2474 eol1 = s.c_str ()[0];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2475 eol2 = s.c_str ()[1];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2476 if (eol1 != '\r' || eol2 != '\n') // Why limit it?
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2477 valid = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2478 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2479 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2480 valid = false;
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 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2483 valid = false;
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2484 if (! valid)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2485 error ("textscan: EndOfLine must be at most one character "
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2486 "or '\\r\\n'");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2487 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2488 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2489 error ("textscan: Unrecognised option '%s'", param.c_str ());
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2490 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2491
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2492 whitespace_table = std::string (256, '\0');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2493 for (unsigned int i = 0; i < whitespace.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2494 whitespace_table[whitespace[i]] = '1';
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 // For Matlab compatibility, add 0x20 to whitespace, unless
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2497 // whitespace is explicitly ignored.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2498 if (! (whitespace.length () == 0 && fmt_list.has_string))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2499 whitespace_table[' '] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2500
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2501 // Create look-up table of delimiters, based on 'delimiter'
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2502 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
2503 if (eol1 >= 0 && eol1 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2504 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
2505 if (eol2 >= 0 && eol2 < 256)
a4c411681e25 avoid out of bounds indexed assignment in textscan
John W. Eaton <jwe@octave.org>
parents: 21462
diff changeset
2506 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
2507 if (! have_delims)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2508 for (unsigned int i = 0; i < 256; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2509 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2510 if (isspace (i))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2511 delim_table[i] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2512 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2513 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2514 for (unsigned int i = 0; i < delims.length (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2515 delim_table[delims[i]] = '1';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2516 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2517
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2518 // 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
2519 // 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
2520
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2521 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2522 textscan::skip_whitespace (delimited_stream& is, bool EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2523 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2524 int c1 = std::istream::traits_type::eof ();
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2525 bool found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2526
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2527 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2528 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2529 found_comment = false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2530 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
2531 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
2532 && ( ( (c1 == eol1 || c1 == eol2) && ++lines && ! EOLstop)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2533 || isspace (c1)))
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 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2536 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2537 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2538 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2539
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2540 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
2541 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2542 // 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
2543 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2544 std::ios::iostate state = is.rdstate ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2545
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2546 char *look, tmp [comment_len];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2547 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
2548 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
2549 .c_str (), look, comment_len-1))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2550 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2551 found_comment = true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2552
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2553 std::string dummy;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2554 char eol [3] = {static_cast<char> (eol1),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2555 static_cast<char> (eol2),
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2556 '\0'};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2557 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
2558 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2559 scan_caret (is, eol, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2560 c1 = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2561 if (c1 == eol1 && eol1 != eol2 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2562 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2563 lines++;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2564 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2565 else // matching pair
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 std::string end_c = comment_style(1).string_value ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2568 // last char of end-comment sequence
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2569 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
2570 '\0'};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2571 std::string may_match ("");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2572 do
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2573 { // find sequence ending with last char
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2574 scan_caret (is, last, dummy);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2575 is.get_undelim (); // (read last itself)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2576
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2577 may_match = may_match + dummy + *last;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2578 int start = may_match.length () - end_c.length ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2579 if (start < 0)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2580 start = 0;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2581 may_match = may_match.substr (start);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2582 }
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2583 while (may_match != end_c && is && ! is.eof ());
21462
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 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2586 else // wasn't really a comment; restore state
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2587 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2588 is.clear (state);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2589 is.seekg (pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2590 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2591 }
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 while (found_comment);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2594
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2595 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2596 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2597 return c1;
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 // 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
2601 // 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
2602 // 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
2603
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2604 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2605 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
2606 bool case_sensitive) const
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 // target strings may be different sizes.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2609 // 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
2610 // that matches.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2611
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2612 char *pos = is.tellg ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2613
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2614 char *look, tmp [max_len + 1];
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 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
2617 look = is.read (tmp, max_len, pos);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2618
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2619 is.clear ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2620 is.seekg (pos); // reset to position before look-ahead
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2621 // FIXME pos may be corrupted by is.read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2622
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2623 int i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2624 int (*compare)(const char *, const char *, size_t);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2625 compare = case_sensitive ? strncmp : strncasecmp;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2626
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2627 for (i = 0; i < targets.numel (); i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2628 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2629 std::string s = targets (i).string_value ();
21469
29e2523c99c2 * textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21468
diff changeset
2630 if (! (*compare) (s.c_str (), look, s.size ()))
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2631 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2632 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
2633 break;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2634 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2635 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2636
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2637 if (i == targets.numel ())
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2638 i = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2639
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2640 return i;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2641 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2642
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2643 // Skip delimiters -- multiple if MultipleDelimsAsOne specified.
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2644 int
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2645 textscan::skip_delim (delimited_stream& is)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2646 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2647 int c1 = skip_whitespace (is, true); // 'true': stop once EOL is read
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2648 if (delim_list.numel () == 0) // single character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2649 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2650 if (is_delim (c1) || c1 == eol1 || c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2651 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2652 is.get ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2653 if (c1 == eol1 && is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2654 is.get_undelim (); // if \r\n, skip the \n too.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2655
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2656 if (multiple_delims_as_one)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2657 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2658 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2659 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2660 // Increment lines for each end-of-line seen; for \r\n, decrement
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2661 while (is && ((c1 = is.get_undelim ())
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2662 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2663 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2664 || isspace (c1) || is_delim (c1)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2665 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2666 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2667 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2668 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2669 }
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2670 if (c1 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2671 is.putback (c1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2672 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2673 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2674 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2675 else // multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2676 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2677 int first_match;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2678
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2679 if (c1 == eol1 || c1 == eol2
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2680 || (-1 != (first_match = lookahead (is, delim_list, delim_len))))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2681 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2682 if (c1 == eol1)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2683 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2684 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2685 if (is.peek_undelim () == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2686 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2687 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2688 else if (c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2689 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2690 is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2691 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2692
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2693 if (multiple_delims_as_one)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2694 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2695 int prev = -1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2696 // skip multiple delims.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2697 // Increment lines for each end-of-line seen; for \r\n, decrement
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2698 while (is && ((c1 = skip_whitespace (is, true))
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2699 != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2700 && (((c1 == eol1 || c1 == eol2) && ++lines)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2701 || -1 != lookahead (is, delim_list, delim_len)))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2702 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2703 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2704 lines--;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2705 prev = c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2706 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2707 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2708 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2709 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2710
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2711 return c1;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2712 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2713
21465
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2714 // Read in as much of the input as coincides with the literal in the
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2715 // format string. Return "true" if the entire literal is matched, else
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2716 // false (and set failbit).
bca9aaef907a * textscan.h, textscan.cc: Style fixes.
John W. Eaton <jwe@octave.org>
parents: 21464
diff changeset
2717
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2718 bool
21473
e9978387898a * textscan.h, textscan.cc: Rename dstr class to delimited_stream.
John W. Eaton <jwe@octave.org>
parents: 21472
diff changeset
2719 textscan::match_literal (delimited_stream& is, const textscan_format_elt& fmt)
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2720 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2721 // "false" -> treat EOL as normal space
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2722 // since a delimiter at the start of a line is a mismatch, not empty field
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2723 skip_whitespace (is, false);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2724
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2725 for (unsigned int i = 0; i < fmt.width; i++)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2726 {
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2727 int ch = is.get_undelim ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2728 if (ch != fmt.text[i])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2729 {
21487
71cd12d22804 * textscan.cc: Use std::istream::traits_type::eof () instead of EOF.
John W. Eaton <jwe@octave.org>
parents: 21486
diff changeset
2730 if (ch != std::istream::traits_type::eof ())
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2731 is.putback (ch);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2732 is.setstate (std::ios::failbit);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2733 return false;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2734 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2735 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2736 return true;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2737 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
2738
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2739 DEFUN (textscan, args, ,
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2740 "-*- texinfo -*-\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2741 @deftypefn {} {@var{C} =} textscan (@var{fid}, @var{format})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2742 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2743 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{param}, @var{value}, @dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2744 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat}, @var{param}, @var{value}, @dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2745 @deftypefnx {} {@var{C} =} textscan (@var{str}, @dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2746 @deftypefnx {} {[@var{C}, @var{position}] =} textscan (@dots{})\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2747 Read data from a text file or string.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2748 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2749 The string @var{str} or file associated with @var{fid} is read from and\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2750 parsed according to @var{format}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2751 The function is an extension of @code{strread} and @code{textread}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2752 Differences include: the ability to read from either a file or a string,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2753 additional options, and additional format specifiers.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2754 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2755 The input is interpreted as a sequence of \"words\", delimiters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2756 (such as whitespace) and literals.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2757 The characters that form delimiters and whitespace are determined\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2758 by the options.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2759 The format consists of format specifiers interspersed between literals.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2760 In the format, whitespace forms a delimiter between consecutive literals,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2761 but is otherwise ignored.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2762 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2763 The output @var{C} is a cell array whose second dimension is determined\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2764 by the number of format specifiers.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2765 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2766 The first word of the input is matched to the first specifier of the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2767 format and placed in the first column of the output;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2768 the second is matched to the second specifier and placed in the second column\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2769 and so forth.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2770 If there are more words than specifiers, the process is repeated until all\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2771 words have been processed or the limit imposed by @var{repeat} has been met\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2772 (see below).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2773 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2774 The string @var{format} describes how the words in @var{str} should be\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2775 parsed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2776 As in @var{fscanf}, any (non-whitespace) text in the format that is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2777 not one of these specifiers is considered a literal;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2778 if there is a literal between two format specifiers then that same literal\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2779 must appear in the input stream between the matching words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2780 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2781 The following specifiers are valid:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2782 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2783 @table @code\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2784 @item %f\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2785 @itemx %f64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2786 @itemx %n\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2787 The word is parsed as a number and converted to double.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2788 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2789 @item %f32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2790 The word is parsed as a number and converted to single (float).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2791 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2792 @item %d\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2793 @itemx %d8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2794 @itemx %d16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2795 @itemx %d32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2796 @itemx %d64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2797 The word is parsed as a number and converted to int8, int16, int32 or int64.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2798 If not size is specified, int32 is used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2799 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2800 @item %u\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2801 @itemx %u8\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2802 @itemx %u16\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2803 @itemx %u32\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2804 @itemx %u64\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2805 The word is parsed as a number and converted to uint8, uint16, uint32 or\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2806 uint64. If not size is specified, uint32 is used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2807 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2808 @item %s\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2809 The word is parsed as a string, ending at the last character before\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2810 whitespace, an end-of-line or a delimiter specified in the options.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2811 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2812 @item %q\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2813 The word is parsed as a \"quoted string\".\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2814 If the first character of the string is a double quote (\") then the string\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2815 includes everything until a matching double quote, including whitespace,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2816 delimiters and end of line characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2817 If a pair of consecutive double quotes appears in the input,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2818 it is replaced in the output by a single double quote.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2819 That is, the input \"He said \"\"Hello\"\"\" would return the value\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2820 'He said \"Hello\"'.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2821 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2822 @item %c\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2823 The next character of the input is read.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2824 This includes delimiters, whitespace and end of line characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2825 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2826 @item %[...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2827 @itemx %[^...]\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2828 In the first form, the word consists of the longest run consisting of only\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2829 characters between the brackets.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2830 Ranges of characters can be specified by a hyphen;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2831 for example, %[0-9a-zA-Z] matches all alphanumeric characters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2832 (if the underlying character set is ASCII).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2833 Since Matlab treats hyphens literally, this expansion only applies to\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2834 alphanumeric characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2835 To include '-' in the set, it should appear first or last in the brackets;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2836 to include ']', it should be the first character.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2837 If the first character is '^' then the word consists of characters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2838 NOT listed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2839 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2840 @item %N...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2841 For %s, %c %d, %f, %n, %u, an optional width can be specified as %Ns etc.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2842 where N is an integer > 1.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2843 For %c, this causes exactly the next N characters to be read instead of\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2844 a single character.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2845 For the other specifiers, it is an upper bound on the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2846 number of characters read;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2847 normal delimiters can cause fewer characters to be read.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2848 For complex numbers, this limit applies to the real and imaginary\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2849 components individually.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2850 For %f and %n, format specifiers like %N.Mf are allowed, where M is an upper\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2851 bound on number of characters after the decimal point to be considered;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2852 subsequent digits are skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2853 For example, the specifier %8.2f would read 12.345e6 as 1.234e7.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2854 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2855 @item %*...\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2856 The word specified by the remainder of the conversion specifier is skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2857 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2858 @item literals\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2859 In addition the format may contain literal character strings;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2860 these will be skipped during reading.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2861 If the input string does not match this literal, the processing terminates,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2862 unless \"ReturnOnError\" is set to \"continue\".\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2863 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2864 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2865 Parsed words corresponding to the first specifier are returned in the first\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2866 output argument and likewise for the rest of the specifiers.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2867 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2868 By default, if there is only one input argument, @var{format} is @t{\"%f\"}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2869 This means that numbers are read from @var{str} into a single column vector.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2870 If @var{format} is explicitly empty, \"\", then textscan will return data\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2871 in a number of columns matching the number of fields on the first data\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2872 line of the input.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2873 Either of these is suitable only if @var{str} contains only numeric fields.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2874 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2875 For example, the string\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2876 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2877 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2878 @group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2879 @var{str} = \"\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2880 Bunny Bugs 5.5\\n\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2881 Duck Daffy -7.5e-5\\n\\\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2882 Penguin Tux 6\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2883 @end group\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2884 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2885 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2886 @noindent\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2887 can be read using\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2888 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2889 @example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2890 @var{a} = textscan (@var{str}, \"%s %s %f\");\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2891 @end example\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2892 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2893 The optional numeric argument @var{repeat} can be used for limiting the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2894 number of items read:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2895 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2896 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2897 @item -1\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2898 (default) read all of the string or file until the end.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2899 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2900 @item N\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2901 Read until the first of two conditions occurs: the format has been processed\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2902 N times, or N lines of the input have been processed.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2903 Zero (0) is an acceptable value for @var{repeat}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2904 Currently, end-of-line characters inside %q, %c, and %[...]$ conversions\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2905 do not contribute to the line count.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2906 This is incompatible with Matlab and may change in future.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2907 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2908 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2909 The behavior of @code{textscan} can be changed via property-value pairs.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2910 The following properties are recognized:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2911 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2912 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2913 @item @qcode{\"BufSize\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2914 This specifies the number of bytes to use for the internal buffer.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2915 A modest speed improvement is obtained by setting this to a large value\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2916 when reading a large file, especially the input contains long strings.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2917 The default is 4096, or a value dependent on @var{n} is that is specified.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2918 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2919 @item @qcode{\"CollectOutput\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2920 A value of 1 or true instructs textscan to concatenate consecutive columns\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2921 of the same class in the output cell array.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2922 A value of 0 or false (default) leaves output in distinct columns.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2923 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2924 @item @qcode{\"CommentStyle\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2925 Parts of @var{str} are considered comments and will be skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2926 @var{value} is the comment style and can be either\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2927 (1) One string, or 1x1 cell string, to skip everything to the right of it;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2928 (2) A cell array of two strings, to skip everything between the first and\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2929 second strings.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2930 Comments are only parsed where whitespace is accepted, and do not act as\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2931 delimiters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2932 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2933 @item @qcode{\"Delimiter\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2934 If @var{value} is a string, any character in @var{value} will be used to\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2935 split @var{str} into words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2936 If @var{value} is a cell array of strings,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2937 any string in the array will be used to split @var{str} into words.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2938 (default value = any whitespace.)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2939 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2940 @item @qcode{\"EmptyValue\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2941 Value to return for empty numeric values in non-whitespace delimited data.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2942 The default is NaN@.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2943 When the data type does not support NaN (int32 for example),\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2944 then default is zero.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2945 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2946 @item @qcode{\"EndOfLine\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2947 @var{value} can be either a emtpy or one character specifying the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2948 end of line character, or the pair\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2949 @qcode{\"@xbackslashchar{}r@xbackslashchar{}n\"} (CRLF).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2950 In the latter case, any of\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2951 @qcode{\"@xbackslashchar{}r\"}, @qcode{\"@xbackslashchar{}n\"} or\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2952 @qcode{\"@xbackslashchar{}r@xbackslashchar{}n\"} is counted as a (single)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2953 newline.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2954 If no value is given, @qcode{\"@xbackslashchar{}r@xbackslashchar{}n\"} is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2955 used.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2956 @c If set to \"\" (empty string) EOLs are ignored as delimiters and added\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2957 @c to whitespace.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2958 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2959 @c When reading from a character string, optional input argument @var{n}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2960 @c specifies the number of times @var{format} should be used (i.e., to limit\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2961 @c the amount of data read).\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2962 @c When reading from file, @var{n} specifies the number of data lines to read;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2963 @c in this sense it differs slightly from the format repeat count in strread.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2964 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2965 @item @qcode{\"HeaderLines\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2966 The first @var{value} number of lines of @var{fid} are skipped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2967 Note that this does not refer to the first non-comment lines, but the first\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2968 lines of any type.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2969 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2970 @item @qcode{\"MultipleDelimsAsOne\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2971 If @var{value} is non-zero,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2972 treat a series of consecutive delimiters, without whitespace in between,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2973 as a single delimiter.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2974 Consecutive delimiter series need not be vertically @qcode{\"aligned\"}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2975 Without this option, a single delimiter before the end of the line does\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2976 not cause the line to be considered to end with an empty value,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2977 but a single delimiter at the start of a line causes the line\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2978 to be considered to start with an empty value.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2979 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2980 @item @qcode{\"TreatAsEmpty\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2981 Treat single occurrences (surrounded by delimiters or whitespace) of the\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2982 string(s) in @var{value} as missing values.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2983 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2984 @item @qcode{\"ReturnOnError\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2985 If set to numerical 1 or true, return normally as soon as an error\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2986 is encountered, such as trying to read a string using @qcode{%f}.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2987 If set to 0 or false, return an error and no data.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2988 If set to \"continue\" (default), textscan attempts to continue reading\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2989 beyond the location; however, this may cause the parsing to get out of sync.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2990 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2991 @item @qcode{\"Whitespace\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2992 Any character in @var{value} will be interpreted as whitespace and trimmed;\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2993 The default value for whitespace is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2994 @c Note: the next line specifically has a newline which generates a space\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2995 @c in the output of qcode, but keeps the next line < 80 characters.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2996 @qcode{\"\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2997 @xbackslashchar{}b@xbackslashchar{}r@xbackslashchar{}n@xbackslashchar{}t\"}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2998 (note the space). Unless whitespace is set to @qcode{\"\"} (empty) AND at\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
2999 least one @qcode{\"%s\"} format conversion specifier is supplied, a space is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3000 always part of whitespace.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3001 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3002 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3003 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3004 When the number of words in @var{str} or @var{fid} doesn't match an exact\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3005 multiple of the number of format conversion specifiers,\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3006 textscan's behavior depends on\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3007 whether the last character of the string or file is\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3008 an end-of-line as specified by the EndOfLine option:\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3009 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3010 @table @asis\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3011 @item last character = end-of-line\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3012 Data columns are padded with empty fields, NaN or 0 (for integer fields)\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3013 so that all columns have equal length\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3014 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3015 @item last character is not end-of-line\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3016 Data columns are not padded; textscan returns columns of unequal length\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3017 @end table\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3018 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3019 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3020 The second output, @var{position}, provides the position, in characters\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3021 from the beginning of the file or string, at which the processing stopped.\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3022 \n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3023 @seealso{dlmread, fscanf, load, strread, textread}\n\
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3024 @end deftypefn")
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3025 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3026 octave_value_list retval;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3027
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3028 if (args.length () < 1)
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3029 print_usage ();
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3030
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3031 octave_value_list tmp_args = args.splice (0, 1);
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3032
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3033 textscan tscanner;
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3034
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3035 if (args(0).is_string ())
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3036 {
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3037 std::istringstream is (args(0).string_value ());
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3038
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3039 retval(0) = tscanner.scan (is, tmp_args);
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3040
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3041 std::ios::iostate state = is.rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3042 is.clear ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3043 retval(1) = octave_value (static_cast<long>(is.tellg ()));
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3044 is.setstate (state);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3045 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3046 else
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3047 {
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3048 octave_stream os = octave_stream_list::lookup (args(0), "textscan");
21490
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3049 std::istream *isp = os.input_stream ();
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3050 if (! isp)
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3051 error ("internal error: textscan called with invalid istream");
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3052
243b04c97b56 textscan: Use references rather than pointers to stream objects
Mike Miller <mtmiller@octave.org>
parents: 21488
diff changeset
3053 retval(0) = tscanner.scan (*isp, tmp_args);
21484
a6717373a0cd make delimited_stream, textscan_format_elt, and textscan_format_list private
John W. Eaton <jwe@octave.org>
parents: 21473
diff changeset
3054
21467
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3055 // FIXME -- warn if stream is not opened in binary mode?
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3056 std::ios::iostate state = os.input_stream ()->rdstate ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3057 os.input_stream ()->clear ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3058 retval(1) = os.tell ();
3aa83b8ce94b * textscan.h, textscan.cc: More style fixes.
John W. Eaton <jwe@octave.org>
parents: 21465
diff changeset
3059 os.input_stream ()->setstate (state);
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3060 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3061
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3062 return retval;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3063 }
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3064
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3065 /*
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3066 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3067 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3068 %! fmtstr = "%f %d %f %s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3069 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3070 %! assert (isequal (c{1}, [1;5]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3071 %! assert (length (c{1}), 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3072 %! assert (iscellstr (c{4}));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3073 %! assert (isequal (c{3}, [3; -Inf]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3074
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3075 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3076 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3077 %! b = [b; 8*b/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3078 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3079 %! fmt = "%f miles/hr = %f kilometers/hr";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3080 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3081 %! assert (c{1}, b(1,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3082 %! assert (c{2}, b(2,:)', 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3083
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3084 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3085 %! str = "13, -, NA, str1, -25\r\n// Middle line\r\n36, na, 05, str3, 6";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3086 %! a = textscan (str, "%d %n %f %s %n", "delimiter", ",",
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3087 %! "treatAsEmpty", {"NA", "na", "-"},"commentStyle", "//");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3088 %! assert (a{1}, int32 ([13; 36]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3089 %! assert (a{2}, [NaN; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3090 %! assert (a{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3091 %! assert (a{4}, {"str1"; "str3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3092 %! assert (a{5}, [-25; 6]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3093
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3094 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3095 %! str = "Km:10 = hhhBjjj miles16hour\r\n";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3096 %! str = [str "Km:15 = hhhJjjj miles241hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3097 %! str = [str "Km:2 = hhhRjjj miles3hour\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3098 %! str = [str "Km:25 = hhhZ\r\n"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3099 %! fmt = "Km:%d = hhh%1sjjj miles%dhour";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3100 %! a = textscan (str, fmt, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3101 %! assert (a{1}', int32 ([10 15 2 25]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3102 %! assert (a{2}', {'B' 'J' 'R' 'Z'});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3103 %! assert (a{3}', int32 ([16 241 3 0]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3104
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3105 ## Test with default endofline parameter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3106 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3107 %! c = textscan ("L1\nL2", "%s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3108 %! assert (c{:}, {"L1"; "L2"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3109
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3110 ## Test with endofline parameter set to "" (empty) - newline should be in word
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3111 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3112 %! c = textscan ("L1\nL2", "%s", "endofline", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3113 %! assert (int8 ([c{:}{:}]), int8 ([ 76, 49, 10, 76, 50 ]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3114
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3115 ### Matlab fails this test. A literal after a conversion is not a delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3116 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3117 #%! ## No delimiters at all besides EOL. Skip fields, even empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3118 #%! str = "Text1Text2Text\nTextText4Text\nText57Text";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3119 #%! c = textscan (str, "Text%*dText%dText");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3120 #%! assert (c{1}, int32 ([2; 4; 0]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3121
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3122 ## CollectOutput test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3123 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3124 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3125 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3126 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3127 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3128 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3129 %! assert (size (c{3}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3130 %! assert (size (c{2}), [10, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3131
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3132 ## CollectOutput test with uneven column length files
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3133 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3134 %! b = [10:10:100];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3135 %! b = [b; 8*b/5; 8*b*1000/5];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3136 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3137 %! str = [str "110 miles/hr"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3138 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3139 %! c = textscan (str, fmt, "collectoutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3140 %! assert (size (c{1}), [11, 1]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3141 %! assert (size (c{3}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3142 %! assert (size (c{2}), [11, 2]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3143 %! assert (c{3}(end), NaN);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3144 %! assert (c{2}{11, 1}, "/hr");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3145 %! assert (isempty (c{2}{11, 2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3146
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3147 ## Double quoted string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3148 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3149 %! str = 'First "the second called ""the middle""" third';
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3150 %! fmt = "%q";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3151 %! c = textscan (str, fmt);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3152 %! assert (c{1}, {"First"; 'the second called "the middle"'; "third"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3153
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3154 ## Arbitrary character
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3155 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3156 %! c = textscan ("a first, \n second, third", "%s %c %11c", 'delimiter', ' ,');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3157 %! assert (c{1}, {"a"; "ond"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3158 %! assert (c{2}, {"f"; "t"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3159 %! assert (c{3}, {"irst, \n sec"; "hird"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3160
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3161 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3162 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3163 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3164 %! c = textscan (str, "%4d %4d", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3165 %! assert (c, {[12 34; 1234 5678; 9 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3166
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3167 ## Field width and non-standard delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3168 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3169 %! str = "12;34;123456789;7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3170 %! c = textscan (str, "%4f %f", "delimiter", ";", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3171 %! assert (c, {[12 34; 1234 56789; 7 NaN]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3172
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3173 ## Ignore trailing delimiter, but use leading one
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3174 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3175 %! str = "12.234e+2,34, \n12345.789-9876j,78\n,10|3";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3176 %! c = textscan (str, "%10.2f %f", "delimiter", ",", "collectOutput", 1,
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3177 %! "expChars", "e|");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3178 %! assert (c, {[1223 34; 12345.79-9876j 78; NaN 10000]}, 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3179
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3180 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3181 %! ## Multi-character delimiter
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3182 %! str = "99end2 space88gap 4564";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3183 %! c = textscan (str, "%d %s", "delimiter", {"end", "gap", "space"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3184 %! assert (c{1}, int32([99; 88]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3185 %! assert (c{2}, {"2 "; "4564"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3186
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3187 ### Delimiters as part of literals, and following literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3188 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3189 #%! str = "12 R&D & 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3190 #%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1, "EmptyValue", -99);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3191 #%! assert (c, {[12 -99; 7 -99]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3192 #
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3193 ### Delimiters as part of literals, and before literals
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3194 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3195 #%! str = "12 & R&D 7";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3196 #%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3197 #%! assert (c, {[12 7]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3198
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3199 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3200 %! ## Check number of lines read, not number of passes through format string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3201 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3202 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3203 %! fprintf (fid, "1\n2\n3\n4\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3204 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3205 %! c = textscan (fid, "%f %f", 2);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3206 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3207 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3208 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3209 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3210 %! assert (!E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3211
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3212 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3213 %! ## Check number of lines read, not number of passes through format string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3214 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3215 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3216 %! fprintf (fid, "1\r\n2\r3\n4\r\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3217 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3218 %! c = textscan (fid, "%f %f", 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3219 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3220 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3221 %! assert (c, {[1;3], [2;4]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3222
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3223 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3224 %! ## Check number of lines read, with multiple delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3225 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3226 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3227 %! fprintf (fid, "1-\r\n-2\r3-\n-4\r\n5\n6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3228 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3229 %! c = textscan (fid, "%f %f", 4, "delimiter", "-", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3230 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3231 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3232 %! assert (c, {[1;3], [2;4]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3233
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3234 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3235 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3236 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3237 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3238 %! fprintf (fid, "1 2 3\n4 s 6");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3239 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3240 %! c = textscan (fid, "%f %f %f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3241 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3242 %! d = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3243 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3244 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3245 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3246 %! assert (c, {[1;4], [2;NaN], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3247 %! assert (d, {[1;4], [2], [3]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3248
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3249 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3250 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3251 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3252 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3253 %! fprintf (fid, "1 2 3\n4 s 6\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3254 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3255 %! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3256 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3257 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3258 %! unlink (f);
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3259 %! assert (c, {[1;4], 2, 3})
21462
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3260
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3261 %!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
3262
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3263 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3264 %! ## Check ReturnOnError
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3265 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3266 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3267 %! fprintf (fid, "1 s 3\n4 5 6");
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, "");
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 %! d = textscan (fid, "", "ReturnOnError", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3272 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3273 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3274 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3275 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3276 %! assert (d, {1})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3277
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3278 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3279 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3280 %! 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
3281 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3282
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3283 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3284 %! ## Check ReturnOnError with empty fields
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3285 %! 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
3286 %! assert (c, {[1;4], [NaN;5], [3;6]})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3287
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3288 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3289 %! ## Check ReturnOnError in first column
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3290 %! 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
3291 %! assert (c, {1, 2, 3})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3292
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3293 ## Test input validation
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3294 %!error textscan ()
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3295 %!error textscan (single (40))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3296 %!error textscan ({40})
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3297 %!error <must be a string> textscan ("Hello World", 2)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3298 #%!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
3299 %!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
3300 %!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
3301 %!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
3302
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3303 ## Test incomplete first data line
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3304 %! R = textscan (['Empty1' char(10)], 'Empty%d %f');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3305 %! assert (R{1}, int32 (1));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3306 %! assert (isempty (R{2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3307
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3308 ## bug #37023
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3309 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3310 %! data = textscan(" 1. 1 \n 2 3\n", '%f %f');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3311 %! assert (data{1}, [1; 2], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3312 %! assert (data{2}, [1; 3], 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3313
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3314 ## Whitespace test (bug #37333) using delimiter ";"
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3315 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3316 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3317 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3318 %! tc{1, end+1} = "C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3319 %! tc{1, end+1} = " C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3320 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3321 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3322 %! c = textscan (string, "%s", "delimiter", ";");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3323 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3324 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3325 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3326 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3327 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3328 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3329 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3330
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3331 ## Whitespace test (bug #37333), adding multipleDelimsAsOne true arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3332 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3333 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3334 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3335 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3336 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3337 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3338 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3339 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3340 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3341 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3342 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3343 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3344 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3345 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3346 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3347
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3348 ## Whitespace test (bug #37333), adding multipleDelimsAsOne false arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3349 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3350 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3351 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3352 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3353 %! tc{1, end+1} = "C:/code/sim;;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3354 %! tc{1, end+1} = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3355 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3356 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3357 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 0);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3358 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3359 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3360 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3361 %! rh(rh == ";") = "";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3362 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3363 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3364 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3365
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3366 ## Whitespace test (bug #37333) whitespace "" arg
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3367 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3368 %! tc = [];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3369 %! tc{1, 1} = "C:/code;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3370 %! tc{1, end+1} = " C:/code/meas;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3371 %! tc{1, end+1} = "C:/code/sim;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3372 %! tc{1, end+1} = "C:/code/utils;";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3373 %! string = [tc{:}];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3374 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3375 %! for k = 1:max (numel (c{1}), numel (tc))
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3376 %! lh = c{1}{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3377 %! rh = tc{k};
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3378 %! rh(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 %! rh = strtrim (rh);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3396 %! assert (strcmp (lh, rh));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3397 %! end
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3398
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3399 ## 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
3400 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3401 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3402 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3403 %! 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
3404 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3405 %! A = textscan (fid, "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3406 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3407 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3408 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3409 %! assert (A{1}, [1 ; 5], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3410 %! assert (A{2}, [2 ; 6], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3411 %! assert (A{3}, [3 ; 7], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3412 %! assert (A{4}, [4 ; 8], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3413 %! assert (E);
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; empty fields & incomplete lower row
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, " ,2,,4\n5,6");
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, "", "delimiter", ",", "EmptyValue", 999, "CollectOutput" , 1);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3422 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3423 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3424 %! 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
3425
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3426 ## Error message tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3427
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3428 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3429 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3430 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3431 %! msg1 = "textscan: 1 parameters given, but only 0 values";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3432 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3433 %! A = textscan (fid, "", "headerlines");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3434 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3435 %! assert (!feof (fid));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3436 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3437 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3438 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3439
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3440 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3441 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3442 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3443 %! msg1 = "textscan: HeaderLines must be numeric";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3444 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3445 %! A = textscan (fid, "", "headerlines", "hh");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3446 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3447 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3448 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3449 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3450
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3451 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3452 %! ## Skip headerlines
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3453 %! 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
3454 %! assert (A, {[1 2; 3 4]});
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 %! ## Skip headerlines with non-default EOL
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3458 %! 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
3459 %! assert (A, {[3 4]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3460
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3461 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3462 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3463 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3464 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3465 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3466 %! 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
3467 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3468 %! A = textscan (fid, "%f", "EndOfLine", "\n\r");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3469 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3470 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3471 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3472 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3473
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3474 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3475 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3476 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3477 %! fprintf (fid,"some_string");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3478 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3479 %! 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
3480 %! try
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3481 %! A = textscan (fid, "%f", "EndOfLine", 33);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3482 %! end_try_catch;
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3483 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3484 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3485 %! assert (msg1, lasterr);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3486
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3487 ## Bug #41824
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3488 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3489 %! assert (textscan ("123", "", "whitespace", " "){:}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3490
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3491 ## Bug #42343-1, just test supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3492 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3493 %! assert (textscan (",NaN", "", "delimiter", "," ,"emptyValue" ,Inf), {Inf, NaN});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3494
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3495 ## Bug #42343-2, test padding with supplied emptyvalue
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3496 %!test
21472
1b48a5d04fb1 textscan.cc: adapt 3 FAILing tests for Matlab compatibility
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 21471
diff changeset
3497 %! 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
3498 %! 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
3499
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3500 ## Bug #42528
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3501 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3502 %! assert (textscan ("1i", ""){1}, 0+1i);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3503 %! 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
3504
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3505 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3506 %! ## TreatAsEmpty
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3507 %! 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
3508 %! assert (C{3}(1), int32 (3));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3509 %! assert (C{4}(1), 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 ## MultipleDelimsAsOne
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 %! 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
3514 %! 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
3515 %! assert (C{1}', [11, 21, 33]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3516 %! assert (C{2}', [12, 23, 34]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3517 %! assert (C{3}', [13, 24, 35]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3518 %! assert (C{4}', [15, 25, NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3519
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3520 ## Bug #44750
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 %! assert (textscan ("/home/foo/", "%s", "delimiter", "/", "MultipleDelimsAsOne", 1){1}, ...
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3523 %! {"home"; "foo"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3524
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3525 ### Allow cuddling %sliteral but warn it is ambiguous
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3526 #%!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3527 #%! C = textscan ("abcxyz51\nxyz83\n##xyz101", "%s xyz %d");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3528 #%! assert (C{1}([1 3]), {"abc"; "##"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3529 #%! assert (isempty (C{1}{2}), true);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3530 #%! assert (C{2}, int32([51; 83; 101]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3531 ### Literals are not delimiters.
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3532
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3533 ## 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
3534 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3535 %! 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
3536
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3537 ## Test various forms of string format specifiers (bug #45712)
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3538 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3539 %! 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
3540 %! 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
3541 %! assert (C, {14, {"1 z"}, {"3 z"}, 11});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3542
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3543 %% Bit width, fixed width conv. specifiers
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3544 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3545 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3546 %! str2 = [str2 str2 str2 str2 str2 str2 str2 str2];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3547 %! 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
3548 %! 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
3549 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3550 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3551 %! assert (C{2}, uint16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3552 %! assert (C{3}, uint32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3553 %! assert (C{4}, uint64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3554 %! assert (C{5}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3555 %! assert (C{6}, int16 (12345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3556 %! assert (C{7}, int32 (1234567890));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3557 %! assert (C{8}, int64 (123456789012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3558 %! assert (C{9}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3559 %! assert (C{10}, double (1234567890.123), 1e-15);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3560 %! assert (C{11}, single (12345.68), 1e-5);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3561 %! assert (C{12}, double (12345.68), 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3562
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3563 %% 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
3564 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3565 %! str2 = "123456789012345 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3566 %! str2 = [str2 str2 "123456789.01234"];
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3567 %! pttrn = "%3u8 %5u16 %10u32 %3d8 %5d16 %10d32 %9f32 %9f";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3568 %! C = textscan (str2, pttrn, "delimiter", " ");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3569 %! assert (C{1}, uint8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3570 %! assert (C{2}, uint16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3571 %! assert (C{3}, uint32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3572 %! assert (C{4}, int8 (123));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3573 %! assert (C{5}, int16 (45678));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3574 %! assert (C{6}, int32 (9012345));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3575 %! assert (C{7}, single (123456789), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3576 %! assert (C{8}, double (0.01234), 1e-12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3577
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3578 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3579 %! C = textscan ("123.123", "%2f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3580 %! assert (C{1}, 12);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3581 %! assert (C{2}, 3.1, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3582 %! assert (C{3}, 23);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3583
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3584 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3585 %! C = textscan ("123.123", "%3f %3f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3586 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3587 %! assert (C{2}, 0.12, 1e-11);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3588 %! assert (C{3}, 3);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3589
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3590 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3591 %! C = textscan ("123.123", "%4f %3f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3592 %! assert (C{1}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3593 %! assert (C{2}, 123);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3594
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3595 %% field width interrupts exponent. (Matlab incorrectly gives [12, 2e12])
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3596 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3597 %! assert (textscan ("12e12", "%4f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3598 %! assert (textscan ("12e+12", "%5f"), {[120; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3599 %! assert (textscan ("125e-12","%6f"), {[12.5; 2]});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3600
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3601 %% %[] tests
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3602 %% Plain [..] and *[..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3603 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3604 %! ar = "abcdefguvwxAny\nacegxyzTrailing\nJunk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3605 %! C = textscan (ar, "%[abcdefg] %*[uvwxyz] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3606 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3607 %! assert (C{2}, {"Any"; "Trailing"; "Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3608
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3609 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3610 %! 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
3611
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3612 %% [^..] and *[^..]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3613 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3614 %! br = "abcdefguvwx1Any\nacegxyz2Trailing\n3Junk";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3615 %! C = textscan (br, "%[abcdefg] %*[^0123456789] %s");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3616 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3617 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3618
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3619 %% [..] and [^..] containing delimiters
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3620 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3621 %! 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
3622 %! C = textscan (cr, "%[ abcdefg] %*[^0123456789] %s", "delimiter", " \n", "whitespace", "");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3623 %! assert (C{1}, {"ab cd efg"; "a ce g"; " "});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3624 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3625
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3626 %% Bug #36464
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3627 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3628 %! 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
3629
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3630 %% test %[]] and %[^]]
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3631 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3632 %! assert (textscan ('345]', "%*[123456]%[]]"){1}{1}, "]");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3633 %! assert (textscan ('345]', "%*[^]]%s"){1}{1}, "]");
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 %% Test that "-i" checks the next two characters
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 %! 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
3638 %! assert (C, {-i, -i, {"n"}, -Inf, -Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3639
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3640 %% Again for "+i", this time with custom parser
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3641 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3642 %! 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
3643 %! assert (C, {i, i, {"n"}, Inf, Inf, {"inity"}});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3644
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3645 %% Check single quoted format interprets control sequences
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3646 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3647 %! 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
3648 %! assert (C, {1, {"2"}, 3, 4});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3649
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3650 %% Check overflow and underflow of integer types
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3651 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3652 %! a = "-1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3653 %! b = "1e90 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3654 %! fmt = "%d8 %d16 %d32 %d64 %u8 %u16 %u32 %u64 ";
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3655 %! 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
3656 %! assert (C{1}, int8 ([-128; 127]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3657 %! assert (C{2}, int16([-32768; 32767]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3658 %! assert (C{3}, int32([-2147483648; 2147483647]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3659 %! assert (C{4}, int64([-9223372036854775808; 9223372036854775807]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3660 %! assert (C{5}, uint8 ([0; 255]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3661 %! assert (C{6}, uint16([0; 65535]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3662 %! assert (C{7}, uint32([0; 4294967295]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3663 %! assert (C{8}, uint64([0; 18446744073709551615]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3664
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3665 %% 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
3666 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3667 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3668 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3669 %! 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
3670 %! 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
3671 %! 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
3672 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3673 %! C = textscan(fid,"%s %s %f32 %d8 %u %f %f %s %f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3674 %! %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
3675 %! assert (C{2}, {"Level1";"Level2";"Level3"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3676 %! 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
3677 %! assert (C{4}, [int8(45);int8(60);int8(12)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3678 %! assert (C{5}, [uint32(4294967295);uint32(4294967295);uint32(200000)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3679 %! assert (C{6}, [inf;-inf;10]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3680 %! assert (C{7}, [NaN;0.001;100], eps);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3681 %! assert (C{8}, {"Yes";"No";"No"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3682 %! 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
3683 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3684 %! C = textscan(fid,"%s Level%d %f32 %d8 %u %f %f %s %f");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3685 %! assert (C{2}, [int32(1);int32(2);int32(3)]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3686 %! 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
3687 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3688 %! C = textscan(fid,'%s %*[^\n]');
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3689 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3690 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3691 %! 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
3692
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3693 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3694 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3695 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3696 %! fprintf (fid,"1, 2, 3, 4, , 6\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3697 %! fprintf (fid,"7, 8, 9, , 11, 12\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3698 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3699 %! C = textscan(fid,"%f %f %f %f %u8 %f", "Delimiter",",","EmptyValue",-Inf);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3700 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3701 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3702 %! assert (C{4}, [4; -Inf]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3703 %! assert (C{5}, uint8 ([0; 11]));
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3704
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3705 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3706 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3707 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3708 %! fprintf (fid,"abc, 2, NA, 3, 4\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3709 %! fprintf (fid,"// Comment Here\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3710 %! fprintf (fid,"def, na, 5, 6, 7\n");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3711 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3712 %! C = textscan(fid,"%s %n %n %n %n","Delimiter",",","TreatAsEmpty",{"NA","na"},"CommentStyle","//");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3713 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3714 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3715 %! assert (C{1}, {"abc";"def"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3716 %! assert (C{2}, [2; NaN]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3717 %! assert (C{3}, [NaN; 5]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3718 %! assert (C{4}, [3; 6]);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3719 %! assert (C{5}, [4; 7]);
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 %!## Test start of comment as string
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3723 %! c = textscan ("1 / 2 // 3", "%n %s %u8", "CommentStyle", {"//"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3724 %! assert (c, {1, "/", 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3725 */
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3726
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3727 // 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
3728 #if 0
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3729 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3730 %!## Test unfinished comment
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3731 %! c = textscan ("1 2 /* half comment", "%n %u8", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3732 %! assert (c, {1, 2});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3733
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3734 ## Test reading from a real file
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3735 %!test
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3736 %! f = tempname ();
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3737 %! fid = fopen (f, "w+");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3738 %! d = rand (1, 4);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3739 %! fprintf (fid, " %f %f /* comment */ %f %f ", d);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3740 %! fseek (fid, 0, "bof");
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3741 %! A = textscan (fid, "%f %f", "CommentStyle", {"/*", "*/"});
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3742 %! E = feof (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3743 %! fclose (fid);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3744 %! unlink (f);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3745 %! assert (A{1}, [d(1); d(3)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3746 %! assert (A{2}, [d(2); d(4)], 1e-6);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3747 %! assert (E);
b7d1e93c0702 initial implementation of textscan in C++
Lachlan Andrew <lachlanbis@gmail.com>
parents:
diff changeset
3748 #endif